Please start any new threads on our new site at http://forums.sqlteam.com. 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
 
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 help
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 09/10/2013 :  08:37:34  Show Profile  Reply with Quote
Hey Guys

Hope your well, can you advise me what i am doing wrong
I trying to create a basic case statement
This is my query
Declare @FROM varchar(10)
Declare @TO varchar(10)
set @FROM = (select dateadd(MM,-1,max([Month_end_date]))
from [FDMS].[dbo].Fact_Fee_History)
set @TO = (select (max([Month_end_date]))
from Fact_Fee_History)

-- Products identifying Merchants who Have Clientline and Virtual Terminals ---
SELECT
FDMSAccountNo,
(max([Month_end_date])) as [Month_end_date],
case WHEN [Fee_Sequence_Number] = '32R' THEN 'Yes' ELSE 'No' END as Clientline,
case WHEN [Fee_Sequence_Number] = '6JT' THEN 'Yes' ELSE 'No' END as [VT]
FROM Fact_Fee_History
where (month_end_date BETWEEN @FROM and @TO)
and fdmsaccountno = '878755671889'
and Fee_Sequence_Number in ('32r', '6jt')
group by fdmsaccountno,[Fee_Sequence_Number]
order by FDMSAccountNo desc

But its returning the following results
FDMSAccountNo Month_end_date Clientline VT
878755671889 2013-08-01 Yes No
878755671889 2013-08-01 No Yes

Can anyone explain why ??

bandi
Flowing Fount of Yak Knowledge

India
2242 Posts

Posted - 09/10/2013 :  08:45:28  Show Profile  Reply with Quote
For FDMSAccountNo = 878755671889 and Month_end_date = 2013-08-01 there are both sequence numbers '32r', '6jt'....

If you wish to have sinle line for each combination FDMSAccountNo you can query as follows:
SELECT
FDMSAccountNo,
max([Month_end_date]) as [Month_end_date],
MAX(case WHEN [Fee_Sequence_Number] = '32R' THEN 'Yes' ELSE 'No' END) as Clientline,
MAX(case WHEN [Fee_Sequence_Number] = '6JT' THEN 'Yes' ELSE 'No' END) as [VT]
FROM Fact_Fee_History
where (month_end_date BETWEEN @FROM and @TO)
and fdmsaccountno = '878755671889'
and Fee_Sequence_Number in ('32r', '6jt')
group by fdmsaccountno
order by FDMSAccountNo desc

--
Chandu
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.06 seconds. Powered By: Snitz Forums 2000