Solved

Aggregate functions

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now