Solved

Aggregate functions

Posted on 2014-09-04
5
111 Views
Last Modified: 2014-09-04
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
Comment
Question by:Morpheus7
  • 3
  • 2
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40303527
>...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
 

Author Comment

by:Morpheus7
ID: 40303535
Hi,
Thanks for the reply.
I would like to return the list of stores using the 0-200, etc  products as a heading.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40303610
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
 

Author Comment

by:Morpheus7
ID: 40303832
Many thanks, that's great.
0
 

Author Closing Comment

by:Morpheus7
ID: 40303838
That's great, many thanks
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question