Home Adding rows from case statement - SQL Server
Reply: 0

Adding rows from case statement - SQL Server

user1372
1#
user1372 Published in July 18, 2018, 10:30 pm

The current results I have are this

I am comparing the totals from the two tables on a monthly basis. When the total is different between the 2 tables i want to add the difference to another column

| MonthYear | Person | Table1 Amount | Table2 Amount | Unknown |
+-----------+--------+---------------+---------------+---------+
|    Jun-17 |    Tom |           100 |           125 |      25 |
|    Nov-17 |        |            50 |           150 |     100 |
|    Sep-17 |    Ben |            50 |            50 |       0 |

Which i have achieved but how do i add the case statement as a row instead instead of a column e.g output like this. I can then group via the 'unknown' category.

| MonthYear | Person | Table1 Amount | Table2 Amount |  Difference |
+-----------+--------+---------------+---------------+-------------+
|    Jun-17 |    Tom |           100 |           125 |          25 |
|    Nov-17 |        |            50 |           150 |         100 |
|    Sep-17 |    Ben |            50 |            50 |           0 |
|    Jun-17 |Unknown |            25 |          Null |           0 |
|    Nov-17 |Unknown |           100 |          Null |           0 |

Can this be done? Any help would be appreciated. Thanks

SQL FIDDLE - http://sqlfiddle.com/#!6/8843b/24

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311657 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO