| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2004-07-09 : 00:18:04
|
| I have two tablestblUserDetailsuserID,datetblMembershipsuserID,membershipType,dateendEach user has a corresponding row in tblMemberships. It's possible that some users have more than one. There are 4 different types of membership.I want to run a query that returns the number of each types of memberships currently. By "current" I mean where the enddate of their membership is greater than today's date. Something to the effect of what is below.level1, level2, level3, level4123, 232, 366, 474Any help on this is appreciated. I think i've done something similar to this in the past but I can't remember the approach.Thanks againmike123 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-09 : 00:35:25
|
| SELECTCOUNT(CASE WHEN membershipType = 1 THEN 1 ELSE 0 END) AS Type1,COUNT(CASE WHEN membershipType = 2 THEN 1 ELSE 0 END) AS Type2,COUNT(CASE WHEN membershipType = 3 THEN 1 ELSE 0 END) AS Type3,COUNT(CASE WHEN membershipType = 4 THEN 1 ELSE 0 END) AS Type4FROM tblMemberships WHERE dateend > GETDATE()MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2004-07-09 : 01:01:00
|
| Hi derek,I'm running that code across a table with just 1 row so far.I am getting results that say 1 for each membership type. What could be causing this data to be off ?Note: Now that I have 2 rows in my data its bringing 2's across the board for each columnThanks againmike123 |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-09 : 01:14:44
|
I'm an idiot. Replace COUNT with SUM. GHEESH!!!! We need to get one of these where he's shooting himself.I really need a vacation.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2004-07-09 : 01:53:33
|
| Hi Derek,Closer but still wrong data :) .. It's pretty late here I usually just blame it on that. If its early wherever you are then its just too early! lol I have 5 rows now and the data should come back1,2,1,1however I have0,1,1,1 |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2004-07-09 : 09:30:24
|
| How about some DDL and DML with your data? |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2004-07-09 : 18:10:11
|
| Here's all the info I have(TBLMEMBERSHIPS)userID / dateStart / dateEnd / membershipLevel 500 2001-01-01 2005-05-05 10500 2001-11-11 2005-05-05 15500 2001-01-01 2005-05-05 20500 2002-01-01 2004-05-05 20500 2002-01-01 2003-05-05 5This SPROC CREATE PROCEDURE dbo.select_membershipBreakDown AS SET NOCOUNT ONSELECTSUM(CASE WHEN membershipType = 5 THEN 1 ELSE 0 END) AS 5,SUM(CASE WHEN membershipType = 10 THEN 1 ELSE 0 END) AS 10,SUM(CASE WHEN membershipType = 15 THEN 1 ELSE 0 END) AS 15,SUM(CASE WHEN membershipType = 20 THEN 1 ELSE 0 END) AS 20FROM tblMemberships WHERE dateEnd > GETDATE()Returns this data5 / 10 / 15/ 200,1,1,1 Thanks againmike123 |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-07-09 : 18:38:26
|
Your filter criteria WHERE dateEnd > GETDATE() is preventing 2 records:500 2002-01-01 2004-05-05 20500 2002-01-01 2003-05-05 5 from being included. The query is correct. |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2004-07-09 : 18:44:36
|
silly me   thanks Ehorn and derrick! |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-07-09 : 19:16:39
|
No worries.. |
 |
|
|
|