2

I'm new to SQL Server and struggling with this.

I have two select statements

SELECT 
    SUM(Amount) AS "MONDAY" 
FROM 
    Transaction 
WHERE 
    Transaction_Date = '2015-12-21 00:00:00.000'

SELECT 
    SUM(Amount) AS "TUESDAY" 
FROM 
    Transaction 
WHERE 
    Transaction_Date = '2015-12-22 00:00:00.000'

Their result is 2,00,000 and 3,00,000 respectively

I want the output to be shown as two columns

(Column 1 + Monday) and (Column 2 = Tuesday) on SQL Server screen

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Arav
  • 23
  • 2

3 Answers3

6

One option is to use conditional aggregation for that:

SELECT SUM(CASE WHEN Transaction_Date = '2015-12-21 00:00:00.000'  
           THEN Amount END) AS "MONDAY" ,
       SUM(CASE WHEN Transaction_Date = '2015-12-22 00:00:00.000' 
           THEN Amount END) AS "TUESDAY" 
FROM Transaction 
sgeddes
  • 62,311
  • 6
  • 61
  • 83
1
select datepart(dw, transactiondate),
    sum(amount) [sum]
from transaction
where transaction_date between '2015-12-22' and '2015-12-23'
group by datepart(dayofweek, transactiondate)

keep in mind, this is pseudo-code, I'm looking up the "dayofweek" attribute...

Lookup datepart here: https://msdn.microsoft.com/en-us/library/ms174420.aspx

ganders
  • 7,285
  • 17
  • 66
  • 114
  • Nice approach but this will create multiple results versus a single row with multiple columns... – sgeddes Jun 28 '16 at 19:36
0

I think you need to use MSSQL Pivot function to transform rows into columns. Check this other question. Maybe this can help you: Convert Rows to columns using 'Pivot' in SQL Server

Community
  • 1
  • 1