Solved

How to group query by x unit

Posted on 2012-04-13
2
141 Views
Last Modified: 2012-04-17
Hi,

I'm using this little query :
SELECT Sum(PageCount) as NbPage, UserLogon FROM AuditTrail
where CostCentreName = 'Etudiant'
and Timestamp >= convert(datetime, '2011/04/13')
group by UserLogon order by NbPage desc

Open in new window


It show's me how page is printed by user. My SQL is quite old and I can't remember how to "summarize" those data.

I want to know how many users have for exemple :
1 to 500 copies
501 to 1000 copies

etc...

Thx for help.
0
Comment
Question by:Sybux
2 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
Comment Utility
try

select  case when NbPage between 1 and 500 then '1-500'
                       when NbPage between 501 and 1000 then '501-1000'
                        end + ' Copies' , count(*)
from (
 SELECT Sum(PageCount) as NbPage, UserLogon FROM AuditTrail
 where CostCentreName = 'Etudiant'
 and Timestamp >= convert(datetime, '2011/04/13')
 group by UserLogon order by NbPage desc
) t
group by case when NbPage between 1 and 500 then '1-500'
                       when NbPage between 501 and 1000 then '501-1000'
                        end
0
 

Author Closing Comment

by:Sybux
Comment Utility
Perfect query. Just a little thing, SQL ask for a TOP in the inside query :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

6 Experts available now in Live!

Get 1:1 Help Now