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 |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-01-13 : 09:23:49
|
| Hi GuysMy mind has gone blank on this!Basically I have two tables:(Sample Data)Customer:CustomerID, Name, DOB, Email1,Jimmy B, 1980-01-03, jimbo@yahoo.com2,Henry V, 1982-03-23, henryv@yahoo.co.ukReceipt:ReceiptID, CustomerID, Date, Status22,1,2008-01-13,V23,1,2008-01-13,V24,1,2008-01-13,V25,1,2008-01-13,VThese are just very basic tables just for explanation purposes.I want to do a count of individual customers who have Receipt.Status of V.So if I use the following query:SELECT COUNT(c.CustomerID)FROM Customer AS cLEFT JOIN Receipt AS r ON c.CustomerID = r.CustomerIDWHERE r.Status = VI want the result to show 1, but instead get 4.Any help???Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-13 : 09:27:36
|
| [code]SELECT COUNT(CustomerID)FROM(SELECT c.CustomerIDFROM Customer cJOIN Receipt rON r.CustomerID=c.CustomerIDGROUP BY CustomerIDHAVING SUM(CASE WHEN r.Status='V' THEN 1 ELSE 0 END)>0)t[/code] |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2009-01-13 : 09:34:23
|
| Thats Great!!!Thanks Yet Again!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-13 : 10:18:00
|
welcome |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-16 : 07:08:12
|
| SELECT COUNT(CustomerID)FROM Customer AS cwhere CustomerID IN (select CustomerID from receipt WHERE Status = 'V') |
 |
|
|
|
|
|
|
|