x

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, 2011 at 08:43 AM in Default

zadien gravatar image

zadien
5 3 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, 2011 at 08:49 AM

Kev Riley gravatar image

Kev Riley ♦♦
54k 47 49 76

(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.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

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

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x24

asked: Nov 02, 2011 at 08:43 AM

Seen: 603 times

Last Updated: Nov 02, 2011 at 08:43 AM