Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 117
  • Last Modified:

Aggregate functions

Hi,

I have the following table and data. This is a list of stores together with the product code held at the store. So there are multiple entries for each store equivalent to the number of products they hold.
I would like to be able to return a list of the StoreNo that have a quantity of products broken down by those that have 0-200 products, 201-400, 401-600, etc
StoreNo       ProductNo
3456            234
3456            456
3456            765
3456            101
3456            201
3456            112
4687            123
4687               990
4687            234

Any help would be appreciated
Many thanks
0
Morpheus7
Asked:
Morpheus7
  • 3
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>...that have a quantity of products broken down by those that have 0-200 products, 201-400, 401-600, etc
Give us a quickie mockup of how you'd like the return set to look like.
Chance are there's plenty of ways to pull this off.
0
 
Morpheus7Author Commented:
Hi,
Thanks for the reply.
I would like to return the list of stores using the 0-200, etc  products as a heading.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
A data mockup would have been better.  Either way, for starters give this a whirl..

SELECT StoreNo,
   Sum(CASE WHEN ProductNo >=0 AND ProductNo <=200 THEN ProductNo ELSE 0 END)  as ProductNo_0_200,
   Sum(CASE WHEN ProductNo > 200 AND ProductNo <= 400 THEN ProductNo ELSE 0 END)  as ProductNo_200_400,
   Sum(CASE WHEN ProductNo > 400 AND ProductNo <= 600 THEN ProductNo ELSE 0 END)  as ProductNo_400_600
FROM YourTable
GROUP BY StoreNo

Couple articles that may help you (yes this is a shameless plug)
   SQL Server GROUP BY Solutions
   Beware of Between Note the use of > and <=
0
 
Morpheus7Author Commented:
Many thanks, that's great.
0
 
Morpheus7Author Commented:
That's great, many thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now