Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
scullee
Posting Yak Master
103 Posts |
Posted - 2003-03-25 : 19:23:26
|
| Well it seems im asking a question a day to this place now.I am currently writing a report that needs to show the number of customers that were active at the end of each month for each subscription and channel in our system (channel = who it is sold through and subscription is what they sell). The data for this is going to be retrieved daily but i want to only get 1 record for each criteria.egEach day there is an extration that calculates the number of active customer for each subscription type. For one month we would have lots of records. EgDate Collected Subscription Channel Number 01/Jan/2003 Basic Melbourne 20001/Jan/2003 Luxury Melbourne 5001/Jan/2003 Basic Sydney 30002/Jan/2003 Basic Melbourne 20502/Jan/2003 Luxury Melbourne 4902/Jan/2003 Basic Sydney 32002/Jan/2003 Luxury Sydney 1.....31/Jan/2003 Basic Melbourne 28031/Jan/2003 Luxury Melbourne 7531/Jan/2003 Basic Sydney 26001/Feb/2003 Basic Melbourne 28101/Feb/2003 Luxury Melbourne 6501/Feb/2003 Basic Sydney 28002/Feb/2003 Basic Melbourne 26502/Feb/2003 Luxury Melbourne 7002/Feb/2003 Basic Sydney 29002/Feb/2003 Luxury Sydney 1.....28/Feb/2003 Basic Melbourne 28028/Feb/2003 Luxury Melbourne 7528/Feb/2003 Basic Sydney 260I need to write a query that will only return the last record for each month. For each subscription and channeleg Just these records.31/Jan/2003 Basic Melbourne 28031/Jan/2003 Luxury Melbourne 7531/Jan/2003 Basic Sydney 26028/Feb/2003 Basic Melbourne 28028/Feb/2003 Luxury Melbourne 7528/Feb/2003 Basic Sydney 260Because the list of channels and subscriptions change is there any way of writing this dynamically?The only thing i could think of is to query the dates first to get a max date with the group by, then use this to select the records. Any other ideas?Edited by - scullee on 03/25/2003 20:42:12 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2003-03-25 : 20:36:50
|
Sculle,That is a very confusing question..Based on your given result set..SELECT * FROM Table SWHERE EXISTS(SELECT 1FROM Table GROUP BY DATENAME(yyyy,Dates) + DATENAME(mm,Dates)HAVING MAX(Dates) = S.DATES) 31st Feb????? What calendar is that!!!DavidM"SQL-3 is an abomination.." |
 |
|
|
scullee
Posting Yak Master
103 Posts |
Posted - 2003-03-25 : 20:45:28
|
| hehehe, yeah well i copied and pasted the data and forgot to update the dates back to 28 :)Sorry if its confusing but its not a simple thing to explain.From the way i read the sp it will limit the query "SELECT * FROM Table S" to only return the records when they are for the last day in the month.If thats what it does then its perfect.THanks :) |
 |
|
|
byrmol
Shed Building SQL Farmer
1591 Posts |
Posted - 2003-03-25 : 21:00:07
|
| Scullee,It is not the last day of the month.. it is the last entered day of the month... If you need the last day then you will need a helper function or calendar tableDavidM"SQL-3 is an abomination.." |
 |
|
|
scullee
Posting Yak Master
103 Posts |
Posted - 2003-03-25 : 21:29:44
|
| Its perfect then. I was looking for the last day entered for the month anyway.I would buy you a beer to thank you for the help but i got no idea who you are or where you live :) |
 |
|
|
|
|
|