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
 query Problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 11/21/2012 :  06:22:09  Show Profile  Reply with Quote
Hey guys

I have a problem

can you tell me what the difference between these two queries are ? i only ask, as both queries producing different set of results

query 1

select
o.ParentID,
s.[FDMSAccountNo]
-sUM(Case WHEN year(period) = '2011' and merch_purch_fees < 0 THEN [Merch_Purch_Fees] WHEN [Merch_Purch_Fees] > = 0 THEN ISNULL([Merch_Purch_Fees],0) + ISNULL([Per_Tran_Fees],0) END) AS [MSC2011],

FROM [FDMS].[dbo].[Fact_Omnipay_Profitability]s
inner join fdms.dbo.Dim_Outlet o on o.FDMSAccountNo = s.FDMSAccountNo
where year(period) > 2010
and s.FDMSAccountNo in (select FDMSAccountNo from #Accounts)
group by
s.FDMSAccountNo,
o.ParentID


query 2

SELECT m.[FDMSAccountNo],
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 MSCtest2011
into #MSC
FROM [FDMS].[dbo].[Fact_Omnipay_Profitability] m
where m.FDMSAccountNo in (select FDMSAccountNo from #Accounts)
and year(period) = '2011'
GROUP BY m.FDMSAccountNo

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/21/2012 :  06:40:10  Show Profile  Reply with Quote
They seem like very different queries, the most obvious one being that in case one is grouped by o.ParentID and the other is not. Also, year = 2011 applies only to the first clause in the case expression in one case whereas it would apply to the entire query in the other case because of the year=2011 filter being in the where clause.
Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 11/21/2012 :  06:49:05  Show Profile  Reply with Quote
Hi sunitabeck

I just want to work out the MSC For 2011
and group by the fdmsaccountno, what query do you think is best suited ?
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 11/21/2012 :  07:08:52  Show Profile  Reply with Quote
quote:
Originally posted by masond


I just want to work out the MSC For 2011 and group by the fdmsaccountno



Second case is more suitable for your requirement

--
Chandu

Edited by - bandi on 11/21/2012 07:10:01
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.05 seconds. Powered By: Snitz Forums 2000