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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with SELECT query with COUNT

Author  Topic 

Apples
Posting Yak Master

146 Posts

Posted - 2008-06-11 : 11:25:27
Here's my tables:

-------------------------------------------------------
tblMembers
-------------------------------------------------------
MemberID | CountryID
-------------------------------------------------------


-------------------------------------------------------
tblCountries
-------------------------------------------------------
CountryID | CountryName
-------------------------------------------------------


-------------------------------------------------------
tblOrders
-------------------------------------------------------
OrderID | MemberID | OrderTypeID
-------------------------------------------------------


-------------------------------------------------------
tblSubscriptionOrders
-------------------------------------------------------
SubscriptionOrderID | OrderID | SubscriptionPackID
-------------------------------------------------------


-------------------------------------------------------
tblSubscriptionPacks
-------------------------------------------------------
SubscriptionPackID | TypeID
-------------------------------------------------------



Here's what I'm trying to do:
1. Output each country in one column
2. Output the number of subscriptions made from a member of that country where tblOrders.orderTypeID = 3 and tblSubscriptionPacks.TypeID = 1 in the next column
3. Output the number of subscriptions made from a member of that country where tblOrders.orderTypeID = 3 and tblSubscriptionPacks.TypeID = 2 in the next column


My problem was that I was doing joins, and I was somehow ending up with orders where the OrderTypeID was NOT equal to 3, even though I declared it specifically in the WHERE clause.

Can someone help me with this query?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-11 : 11:33:09
[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 Type2Count
FROM tblOrders o
INNER JOIN tblMembers m
ON m.MemberID=o.MemberID
AND o.OrderTypeID=3
INNER JOIN tblCountries c
ON c.CountryID=m.CountryID
INNER JOIN tblSubscriptionOrders so
ON so.OrderID=o.OrderID
INNER JOIN tblSubscriptionPacks sp
ON sp.SubscriptionPackID=so.SubscriptionPackID
GROUP BY c.CountryName
[/code]
If this is not what you expected, post some sample data and show us what output you want out of them
Go to Top of Page

Apples
Posting Yak Master

146 Posts

Posted - 2008-06-11 : 11:44:24
That looks good, except that's not counting orders where OrderTypeID = 3. It's also counting the other orders that a member has made if the member has made an order where the OrderTypeID = 3.

That's the same problem I was having.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-11 : 11:48:49
quote:
Originally posted by Apples

That looks good, except that's not counting orders where OrderTypeID = 3. It's also counting the other orders that a member has made if the member has made an order where the OrderTypeID = 3.

That's the same problem I was having.


Can you provide some sample data and illustrate?
b/w i had a small mistake in earlier query. It should have been GROUP BY not ORDER BY
Go to Top of Page

Apples
Posting Yak Master

146 Posts

Posted - 2008-06-11 : 12:12:23
quote:
Originally posted by visakh16

quote:
Originally posted by Apples

That looks good, except that's not counting orders where OrderTypeID = 3. It's also counting the other orders that a member has made if the member has made an order where the OrderTypeID = 3.

That's the same problem I was having.


Can you provide some sample data and illustrate?
b/w i had a small mistake in earlier query. It should have been GROUP BY not ORDER BY



Actually, now that I look closer at the data, what you're doing is right. Your query works perfectly, thank you.
Go to Top of Page
   

- Advertisement -