SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Case Statement subtract query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 11/19/2012 :  10:28:50  Show Profile  Reply with Quote
Hi guys ~


This is a really basic/stupid question to ask, but i am really struggling with it

I want to a produce a coloumn called DI. To work out the DI is is MSC - [Intchg_Assess]

would appreciate any help available

My query is

SELECT
d.[FDMSAccountNo],
d.[Intchg_Assess],
SUM(CASE WHEN merch_purch_fees < 0 THEN [Merch_Purch_Fees]
WHEN merch_purch_fees > = 0 THEN [Merch_Purch_Fees] + [Per_Tran_Fees] END) AS MSC
into #DI
FROM [FDMS].[dbo].[Fact_Omnipay_Profitability] d
where (convert(datetime,Period,103) BETWEEN @Rolling12tempFROM and @Rolling12tempTO) and d.FDMSAccountNo in (select FDMSAccountNo from #Accounts)
GROUP BY d.FDMSAccountNo

masond
Constraint Violating Yak Guru

447 Posts

Posted - 11/19/2012 :  10:31:18  Show Profile  Reply with Quote
is it as simple as

SELECT
d.[FDMSAccountNo],
d.[Intchg_Assess],
SUM(CASE WHEN merch_purch_fees < 0 THEN [Merch_Purch_Fees] WHEN merch_purch_fees > = 0 THEN [Merch_Purch_Fees] + [Per_Tran_Fees] END) AS 'MSC',
Sum([MSC])-sum(-[Intchg_Assess]) as [DI]
into #DI
FROM [FDMS].[dbo].[Fact_Omnipay_Profitability] d
where (convert(datetime,Period,103) BETWEEN @Rolling12tempFROM and @Rolling12tempTO) and d.FDMSAccountNo in (select FDMSAccountNo from #Accounts)
GROUP BY d.FDMSAccountNo
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 11/19/2012 :  13:00:08  Show Profile  Reply with Quote

SELECT [FDMSAccountNo],[Intchg_Assess],
ISNULL(MSC,0) - ISNULL([Intchg_Assess],0) as DI
from
(SELECT 
d.[FDMSAccountNo],
d.[Intchg_Assess],
SUM(CASE WHEN merch_purch_fees < 0 THEN [Merch_Purch_Fees] WHEN merch_purch_fees > = 0 THEN [Merch_Purch_Fees] + [Per_Tran_Fees] END) AS 'MSC'
FROM [FDMS].[dbo].[Fact_Omnipay_Profitability] d
where (convert(datetime,Period,103) BETWEEN @Rolling12tempFROM  and @Rolling12tempTO) and d.FDMSAccountNo in (select FDMSAccountNo from #Accounts)
GROUP BY d.FDMSAccountNo,d.[Intchg_Assess]
)G

Edited by - sodeep on 11/19/2012 13:01:10
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000