Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 case statement

Author  Topic 

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2013-08-01 : 05:57:36
Hey Guys

Sorry to bother you

I just need some help with a case statement

Aim =
IF scheme = “Mastercard”and the card_type = “Debit” then sum the sales, and refunds
If scheme = “Mastercard”and the card_type = “Credit” then sum the sales, and refunds
If scheme = “Visa ”and the card_type = “Debit” then sum the sales, and refunds
If scheme = “Visa ”and the card_type = “credit” then sum the sales, and refunds

If scheme = “Visa ”and “Card Type” = “Visa Electrion” & card_type = “Debit” then sum the sales, and refunds

These are the columns within my table
Scheme Card Type
Sales
Refunds
Card_Type

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-08-01 : 06:12:13
based on your explanation you want either of these

SELECT SUM(CASE WHEN SchemeCardType = 'Mastercard' AND card_type = 'Debit' THEN sales + refunds END) AS MCDebit,
SUM(CASE WHEN SchemeCardType = 'Mastercard' AND card_type = 'Credit' THEN sales + refunds END) AS MCCredit,
SUM(CASE WHEN SchemeCardType = 'Visa' AND card_type = 'Debit' THEN sales + refunds END) AS VisaDebit,
SUM(CASE WHEN SchemeCardType = 'Visa' AND card_type = 'Credit' THEN sales + refunds END) AS VisaCredit
FROM Table


if you want them in separate columns

and

SELECT SchemeCardType,
Card_Type,
SUM(sales + refunds)
FROM Table
GROUP BY SchemeCardType,
Card_Type


if you want them in rows

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-08-01 : 06:13:57
[code]SELECT Scheme,
Card_Type,
SUM(Sales) AS Sales,
SUM(Refunds) AS Refunds
FROM dbo.Table
GROUP BY Scheme,
Card_Type;[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -