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
 Development Tools
 Other Development Tools
 Adding a filter to a count

Author  Topic 

sprocker
Starting Member

2 Posts

Posted - 2010-09-17 : 06:23:02
Hi,

I need to create a report summing up how many orders have been delivered to the different regions with in a period of time - the time is stored as DelDate in the OrderDetails table.

I have used

SELECT ShipRegion, Count(Shipregion) as Regioncount FROM Orders Inner Join Customers on Orders.CustomerID=Customers.CustomerID Group by ShipRegion ORDER BY ShipRegion

The SQL above does give me the number, but does not allow me to use any other fields for the Where statement.

Thanks in advance

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-17 : 07:57:24
Sure you can:

SELECT
ShipRegion,
Count(Shipregion) as Regioncount
FROM Orders
Inner Join Customers on Orders.CustomerID=Customers.CustomerID
WHERE ...
Group by ShipRegion
ORDER BY ShipRegion


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sprocker
Starting Member

2 Posts

Posted - 2010-09-17 : 11:52:12
thanks for that - but I had pasted the wrong query!... and I have sussed it now - this is the one that worked.....the thing I was missing was adding the categories to the group by that were in the Select.

SELECT Product, count(Product) as ProdCount, Category, ProductWeight
FROM WeightSummary
WHERE DateDiff('d','param2',CollectionDate)>=0 AND DateDiff('d','param3',CollectionDate)<=0 AND (CollectionName <> 'Halesfield Recycling Centre' OR ISNull(CollectionName)) Group by Product, Category, ProductWeight

Deb
Go to Top of Page
   

- Advertisement -