October 16, 2013 at 11:00 am
I've a simple TSQL query as follows
SELECT COUNT (PaxNum) as TotalPassengersCount
FROM dbo.FlightDetails
WHERE FlightDate BETWEEN '1/1/2013' and '1/31/2013';
Here, I'm getting the TotalPassengercount for the month of Jan 2013.
Now, if I change my end date to '10/15/2013', I want to get the passenger count for each month up to Oct 15.
For eg, my desired result should be
Jan - 2000
Feb - 2500
-
-
Oct (month to date up to 15) - 1000
Instead of changing the date and running it for every month manually, I wanted to know if we can do this in one single query without having to run it manually for each month.
Thanks for your help in advance
October 16, 2013 at 11:21 am
You could add the MONTH and YEAR functions or a CONVERT to group by.
SELECT RIGHT( CONVERT(char(11), FlightDate, 113), 8) Month,
COUNT (PaxNum) as TotalPassengersCount
FROM dbo.FlightDetails
WHERE FlightDate BETWEEN '1/1/2013' and '1/31/2013'
GROUP BY RIGHT( CONVERT(char(11), FlightDate, 113), 8);
PS. You can change the Month format
October 16, 2013 at 12:06 pm
Assuming FlightDate is a datetime data type you could use this.
SELECT Month(FlightDate) as Month,
COUNT (PaxNum) as TotalPassengersCount
FROM dbo.FlightDetails
WHERE FlightDate BETWEEN '1/1/2013' and '10/15/2013'
GROUP BY Month(FlightDate);
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 16, 2013 at 12:12 pm
Assuming also that both dates will be on the same year and months won't overlap. Otherwise a year column is also necessary.;-)
October 16, 2013 at 12:16 pm
Just as a side note:
Whenever dealing with a date type in character format, I'd prefer the ISO format 'YYYYMMDD', so '20131015' instead of '10/15/2013'
Just use SET DATEFORMAT DMY in a query window and try Sean's query again....
October 16, 2013 at 12:20 pm
LutzM (10/16/2013)
Just as a side note:Whenever dealing with a date type in character format, I'd prefer the ISO format 'YYYYMMDD', so '20131015' instead of '10/15/2013'
+10000 to that.
I should have reformatted the query and stated the same but apparently I was being lazy this time. 😀
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 16, 2013 at 12:25 pm
Thanks much to all of you for your help and comments.
October 16, 2013 at 12:26 pm
SELECT CONVERT(VARCHAR(10), DATEADD(mm, DATEDIFF(mm, 0, FlightDate), 0), 102) AS FlightMonth,
COUNT(PaxNum) AS TotalPassengersCount
FROM FlightDetails
GROUP BY DATEADD(mm, DATEDIFF(mm, 0, FlightDate), 0)
ORDER BY DATEADD(mm, DATEDIFF(mm, 0, FlightDate), 0)
another idea 🙂
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply