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

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Nice approach but this will create multiple results versus a single row with multiple columns...
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.