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 |
|
tjbarr
Starting Member
8 Posts |
Posted - 2008-10-23 : 12:11:24
|
| id giftdate1 9/1/20081 9/1/20071 5/4/20042 8/6/20082 8/6/20073 4/3/20033 4/3/20024 4/3/20085 4/3/2007I want a query with a sub selet to give me all id that gave more than 1 gift with at least one gift within 2007 or 2008. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-10-23 : 12:38:33
|
| SELECT IDFROM MyTableWHERE GiftDate > '20070101' AND GiftDate < '20090101'GROUP BY IDHAVING COUNT(ID) > 0 |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-23 : 12:41:16
|
FIXED:quote: Originally posted by Lamprey SELECT IDFROM MyTableWHERE GiftDate >= '20070101' AND GiftDate < '20090101'GROUP BY IDHAVING COUNT(ID) > 1
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-23 : 12:46:08
|
or is it this?SELECT IDFROM MyTableGROUP BY IDHAVING COUNT(CASE WHEN GiftDate > '20070101' AND GiftDate < '20090101' THEN ID ELSE NULL END) >= 1 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-24 : 03:12:10
|
quote: Originally posted by visakh16 or is it this?SELECT IDFROM MyTableGROUP BY IDHAVING COUNT(CASE WHEN GiftDate > '20070101' AND GiftDate < '20090101' THEN ID ELSE NULL END) >= 1
GiftDate > '20070101' should beGiftDate >= '20070101' MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-24 : 03:41:09
|
Won't work.What if one gift is made 2006 and one gift is made 2008?SELECT IDFROM MyTableGROUP BY IDHAVING SUM(CASE WHEN GiftDate >= '20070101' AND GiftDate < '20090101' THEN 1 ELSE 0 END) > 0 -- Át least one gift during 2007 and 2008 AND COUNT(*) > 1 -- And at least two gifts in total, all according to OP spec E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-24 : 03:59:59
|
quote: Originally posted by Peso Won't work.What if one gift is made 2006 and one gift is made 2008?SELECT IDFROM MyTableGROUP BY IDHAVING SUM(CASE WHEN GiftDate >= '20070101' AND GiftDate < '20090101' THEN 1 ELSE 0 END) > 0 -- Át least one gift during 2007 and 2008 AND COUNT(*) > 1 -- And at least two gifts in total, all according to OP spec E 12°55'05.63"N 56°04'39.26"
yup thats right. I had missed that. thanks for spotting it |
 |
|
|
|
|
|