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

avatar image

zadien
5 3 3 4

(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

avatar image

Kev Riley ♦♦
64k 48 61 81

(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

SQL Server Central

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

Topics:

x43

asked: Nov 02, 2011 at 08:43 AM

Seen: 687 times

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

Copyright 2016 Redgate Software. Privacy Policy