x
login about faq Site discussion (meta-askssc)

SUM multiple columnns

Hi,

So I have this query and I need to display the count of each query but also the rows.

 select *
    from derpy
    where paymentdays <=15
    UNION
    select *
    from derpy
    where paymentdays >15
    AND paymentdays <=30
    UNION
    select *
    from derpy
    where paymentdays >30

I'd like get the output like the below report sample..basically matrix like?

alt text

more ▼

asked Nov 02 '11 at 08:43 AM in Default

zadien gravatar image

zadien
5 1 3 3

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first
select
 sum(1) as 'No of Payments made',
 sum(case when paymentdays < 15 then 1 else 0 end ) as '<15days',
 sum(case when paymentdays > 15 and paymentdays <= 30 then 1 else 0 end ) as '16-30days',
 sum(case when paymentdays > 30 then 1 else 0 end ) as '>30days',
from derpy
more ▼

answered Nov 02 '11 at 08:49 AM

Kev Riley gravatar image

Kev Riley ♦♦
46.1k 38 43 69

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x18

asked: Nov 02 '11 at 08:43 AM

Seen: 325 times

Last Updated: Nov 02 '11 at 08:43 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.