Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want a simple query to get some output. Records are
ID Count
215 1
215 1
215 1
215 1
Output must be
ID Count
215 4
SELECT ID, COUNT(*) AS Count
FROM tablename
GROUP BY ID
Very simple,
SELECT ID ,COUNT(*) AS CNT
FROM TABLE_NAME
GROUP BY ID
SELECT ID, sum(Count) AS Count
FROM tablename
GROUP BY ID
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Count of players who have scored within 500 runs of the leader from particular country. For example if from India A scores 1500 runs then how many players have scored more than 1000 runs.
I have run this code but this is not giving me my desired result.
select COUNT( [Player ] )as no_of_players,country from [dbo].[Sheet1$]
where runs >=
All (select max(runs)-500 from [dbo].[Sheet1$] group by country )
group by country
You can use a window function for this
SELECT
COUNT(*) AS no_of_players,
country
FROM (
SELECT *,
MAX(runs) OVER (PARTITION BY country) AS MaxRuns
FROM [dbo].[Sheet1$] s1
) s1
WHERE runs >= MaxRuns - 500;
Note that COUNT(nonNullValue) is the same as COUNT(*) or COUNT(1)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table like this..
ID City
-----------------
1 Jakarta
1 Bogor
2 Bandung
2 Surabaya
3 Solo
3 Null
I want the query would returns:
ID City
---------------
1 Bogor
2 Surabaya
3 Null
Any ideas? I need your help.. I used some methods such as rank , distinct, max.. but the result is not what I expected..
You can use a CTE with the ROW_NUMBER-function:
WITH CTE AS
(
SELECT Id, City, Rn = ROW_NUMBER() OVER (Partition By ID
Order by Id DESC)
FROM dbo.Cities
)
SELECT Id, City
FROM CTE
WHERE RN = 1
Altough i don't know which column you want to use to determine the "latest" record. Use that in Order by Id DESC.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have some tables following
Room(CodeR, NameR)
Title(CodeT,NameT)
Member(ID,Name, Salary, Sex, CodeR, CodeT)
I want write the sql to select Roooms that member less than 5 and print all member of this room.
Please help me solve this problem
pls help me remove this topic. i can not ask any question.
select R.CodeR, NameR, m.Id, m.Name FROM
Room R INNER JOIN Member m on
R.CodeR = m.CodeR
WHERE R.CodeR in
(SELECT iM.CodeR FROM Member iM GROUP BY iM.CodeR HAVING COUNT(*) <5)
First of all pull all the rooms where no of members are less than 5
SELECT iM.CodeR FROM Member iM GROUP BY iM.CodeR HAVING COUNT(*) <5
Then pull out room and member information as required using above result.
select R.CodeR, NameR, m.Id, m.Name FROM
Room R INNER JOIN Member m on
R.CodeR = m.CodeR
WHERE R.CodeR in (X, Y, Z) //X, Y, Z will be replaced by subquery
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Here is pics of Relationship and Pasenger_detail form and i wrote query to select seat_no but its not working properly. it is only showing all seats as per bus reserved id. but requirement were to show only those which are not yet selected. here is query
"Select seat_no.seat_no FROM Seat_No Where seat_no.seat_no <= (select br_info.Seats_Reserved from br_info where ((Br_info.br_id)=[forms]![pasenger_detail]![br_id]) AND (Seat_No.seat_no) NOT IN (SELECT Pasenger_Detail.Seat_No FROM Pasenger_Detail WHERE (((Pasenger_Detail.Group_ID)=[forms]![Pasenger_Detail]![Group_ID]) AND ((Pasenger_Detail.BR_ID)=[forms]![Pasenger_Detail]![BR_ID]))));"
SELECT S.Seat_No
FROM Seat_No AS S
WHERE S.Seat_No Not In
(
SELECT P.Seat_No
FROM BR_Info AS B INNER JOIN Pasenger_Detail AS P ON B.BR_ID = P.BR_ID
WHERE B.BR_ID = Forms!pasenger_detail!BR_ID
AND B.Group_ID = Forms!Pasenger_Detail!Group_ID
)
In the above query I am joining BR_Info and Passenger_Detail together. It is an INNER JOIN which means that the record has to exist in both BR_Info and Passenger_Detail in order to be considered. Then I am using Not In to get all the Seat_No that are not contained in the sub query.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help me with the script which will delete the data older than the particular date.
Thanks
delete from YOUR_TABLE where your_date_column < '2009-01-01';
This will delete rows from YOUR_TABLE where the date in your_date_column is older than January 1st, 2009. i.e. a date with 2008-12-31 would be deleted.
Delete data that is 30 days and older
DELETE FROM Table
WHERE DateColumn < GETDATE()- 30
You could use:
DELETE FROM tableName
where your_date_column < '2009-01-01';
but Keep in mind that the above is really
DELETE FROM tableName
where your_date_column < '2009-01-01 00:00:00';
Not
DELETE FROM tableName
where your_date_column < '2009-01-01 11:59';
This is pretty vague.
Do you mean like in SQL:
DELETE FROM myTable
WHERE dateColumn < '2007'
or an ORACLE version:
delete
from table_name
where trunc(table_name.date) > to_date('01/01/2009','mm/dd/yyyy')