Solved

Aggregate functions

Posted on 2014-09-04
5
112 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL server performance baseline 19 45
query analyser in sql server 2016 express edition 2 23
T-SQL to copy a database 37 64
SQL Select Query help 1 34
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

679 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