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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

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

-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

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'

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

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

Flowing Fount of Yak Knowledge

2242 Posts

Posted - 11/21/2012 :  07:08:52  Show Profile  Reply with 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


Edited by - bandi on 11/21/2012 07:10:01
Go to Top of Page
  Previous Topic Topic Next 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.03 seconds. Powered By: Snitz Forums 2000