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 |
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-06-25 : 12:42:18
|
| Here's my schema:-------------------------------------------------------tblMembers-------------------------------------------------------MemberID | CountryID--------------------------------------------------------------------------------------------------------------tblCountries-------------------------------------------------------CountryID | CountryName--------------------------------------------------------------------------------------------------------------tblOrders-------------------------------------------------------OrderID | MemberID | OrderTypeID--------------------------------------------------------------------------------------------------------------tblSubscriptionOrders-------------------------------------------------------SubscriptionOrderID | OrderID | SubscriptionPackID--------------------------------------------------------------------------------------------------------------tblSubscriptionPacks-------------------------------------------------------SubscriptionPackID | TypeID-------------------------------------------------------Here's what I'm doing:1. Output each country in one column2. Output the number of subscriptions made from a member of that country where tblSubscriptionPacks.TypeID = 1 in the next column3. Output the number of subscriptions made from a member of that country where tblSubscriptionPacks.TypeID = 2 in the next columnHere's my query:SELECT c.CountryName,SUM(CASE WHEN sp.TypeID=1 THEN 1 ELSE 0 END) AS Type1Count,SUM(CASE WHEN sp.TypeID=2 THEN 1 ELSE 0 END) AS Type2CountFROM tblOrders oINNER JOIN tblMembers mON m.MemberID=o.MemberIDINNER JOIN tblCountries cON c.CountryID=m.CountryIDINNER JOIN tblSubscriptionOrders soON so.OrderID=o.OrderIDINNER JOIN tblSubscriptionPacks spON sp.SubscriptionPackID=so.SubscriptionPackIDGROUP BY c.CountryNameBut now I want to tweak something. In tblSubscriptionOrders, sometimes there are duplicate OrderID's. This isn't a bug, there are supposed to be duplicates sometimes, but I won't explain that. Right now, my query will count both OrderID's if there's a duplicate. I'd like to only count one if there's a duplicate.How can I do this? All these joins are confusing me. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-25 : 13:07:47
|
| [code]SELECT c.CountryName,SUM(CASE WHEN sp.TypeID=1 THEN 1 ELSE 0 END) AS Type1Count,SUM(CASE WHEN sp.TypeID=2 THEN 1 ELSE 0 END) AS Type2CountFROM tblOrders oINNER JOIN tblMembers mON m.MemberID=o.MemberIDINNER JOIN tblCountries cON c.CountryID=m.CountryIDINNER JOIN (SELECT DISTINCT OrderID FROM tblSubscriptionOrders) soON so.OrderID=o.OrderIDINNER JOIN tblSubscriptionPacks spON sp.SubscriptionPackID=so.SubscriptionPackIDGROUP BY c.CountryName[/code] |
 |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-06-25 : 14:02:17
|
quote: Originally posted by visakh16
SELECT c.CountryName,SUM(CASE WHEN sp.TypeID=1 THEN 1 ELSE 0 END) AS Type1Count,SUM(CASE WHEN sp.TypeID=2 THEN 1 ELSE 0 END) AS Type2CountFROM tblOrders oINNER JOIN tblMembers mON m.MemberID=o.MemberIDINNER JOIN tblCountries cON c.CountryID=m.CountryIDINNER JOIN (SELECT DISTINCT OrderID FROM tblSubscriptionOrders) soON so.OrderID=o.OrderIDINNER JOIN tblSubscriptionPacks spON sp.SubscriptionPackID=so.SubscriptionPackIDGROUP BY c.CountryName
That didn't quite work, I'm trying to get SubscriptionPackID out of tblSubscriptionOrders as well, so there's an error thrown saying that SubscriptionPackID is an invalid column name.I tried (SELECT DISTINCT OrderID, SubscriptionPackID FROM tblSubscriptionOrders), but that didn't select distinct OrderID's |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-25 : 14:08:33
|
| <<I tried (SELECT DISTINCT OrderID, SubscriptionPackID FROM tblSubscriptionOrders), but that didn't select distinct OrderID's>>Not neccessarilyIf you want to distinct OrderId, you have to pick either minimum or maximum of SubscriptionPackID SELECT OrderID, Max(SubscriptionPackID) as SubscriptionPackID FROM tblSubscriptionOrdersGROUP BY OrderIDMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|