SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Getting Total Count for each month based on the Start and End Date


Getting Total Count for each month based on the Start and End Date

Author
Message
sql1411
sql1411
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 96
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
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17006 Visits: 19122
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


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26668 Visits: 17557
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17006 Visits: 19122
Assuming also that both dates will be on the same year and months won't overlap. Otherwise a year column is also necessary.;-)


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
LutzM
LutzM
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10591 Visits: 13559
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....



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26668 Visits: 17557
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. :-D

_______________________________________________________________

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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
sql1411
sql1411
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 96
Thanks much to all of you for your help and comments.
J Livingston SQL
J Livingston SQL
SSCertifiable
SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)

Group: General Forum Members
Points: 5561 Visits: 35465


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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search