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

masond
Constraint Violating Yak Guru

447 Posts

Posted - 10/31/2012 :  07:46:17  Show Profile  Reply with Quote
Hey guys

I have a little snag and i am hoping you will be able to help me
my query is producing duplicate hst_merchnum and i dont know why

any solutions

Declare @date varchar(10)
set @Date = (select dateadd(MM,-11,max(hst_date_processed))
from FDMS.dbo.Fact_Financial_History)


SELECT DISTINCT (hst_merchnum) as MerchantName,
[Standard],
[CP],
[Contactless],
[Secure eCom],
[TBA],
[CHIP],
[MOTO],
[Non Secure eCom],
ISNULL([Standard],0)
+ISNULL([CP],0)
+ISNULL([Secure eCom],0)
+ISNULL([TBA],0)
+ISNULL([CHIP],0)
+ISNULL([MOTO],0)
+ISNULL([Non Secure eCom],0) as 'Grand Total'


FROM
(SELECT distinct (hst_merchnum),
Fact_Financial_History.hst_date_processed,
Dim_Interchange_Tier_2.Qualification_2,
SUM(Fact_Financial_History.Net_Sales) AS [Sum Of Net Sales]
FROM Fact_Financial_History INNER JOIN Dim_Interchange_Tier_2 ON
Fact_Financial_History.hst_prod_code + '-' + Fact_Financial_History.hst_plan_code = Dim_Interchange_Tier_2.Plan_Code

WHERE
(Fact_Financial_History.hst_date_processed >= @date)
--and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')
GROUP BY
Fact_Financial_History.hst_date_processed,
Dim_Interchange_Tier_2.Qualification_2,
hst_merchnum

) as p
PIVOT
( MAX( [Sum Of Net Sales]) FOR Qualification_2 IN ([Standard],
[CP],
[Contactless],
[Secure eCom],
[TBA],
[CHIP],
[MOTO],
[Non Secure eCom]) ) as pvt

Edited by - masond on 10/31/2012 09:01:01

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 10/31/2012 :  09:13:54  Show Profile  Reply with Quote
In the subquery you are grouping result set by 3 columns(Fact_Financial_History.hst_date_processed,
Dim_Interchange_Tier_2.Qualification_2,
hst_merchnum)

So you Obviously get combination of those 3 columns.
That is the reason you are getting duplicate values for hst_merchnum





--
Chandu

Edited by - bandi on 10/31/2012 09:14:33
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 10/31/2012 :  09:24:49  Show Profile  Reply with Quote
if you want a solution you need to post us some sample data and then show output you're looking at

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 10/31/2012 :  09:38:27  Show Profile  Reply with Quote
HI guys

required results
I want a one liner, which gives me 12months of data for every hst_merchnum
For eg

MerchantName,[Standard],[CP],[Contactless],[Secure eCom],[TBA],[CHIP],[MOTO],[Non Secure eCom], Grand Total

test1 1000 2000 3000 4000 5000 6000 7000 8000 36000
test2 8000 7000 6000 5000 4000 3000 2000 1000 36000



Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 10/31/2012 :  09:46:20  Show Profile  Reply with Quote
Try this once.......

FROM
(SELECT distinct (hst_merchnum)
,Fact_Financial_History.hst_date_processed,
,Dim_Interchange_Tier_2.Qualification_2,
SUM(Fact_Financial_History.Net_Sales) AS [Sum Of Net Sales]
FROM Fact_Financial_History INNER JOIN Dim_Interchange_Tier_2 ON
Fact_Financial_History.hst_prod_code + '-' + Fact_Financial_History.hst_plan_code = Dim_Interchange_Tier_2.Plan_Code

WHERE
(Fact_Financial_History.hst_date_processed >= @date)
--and Dim_Interchange_Tier_2.Qualification_2 in ('Secure eCom','Non Secure eCom')
GROUP BY
Fact_Financial_History.hst_date_processed,
Dim_Interchange_Tier_2.Qualification_2,
hst_merchnum



--
Chandu
Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 10/31/2012 :  10:34:03  Show Profile  Reply with Quote
Hi Bandi

Although i have put isnul into my query, the results are still producing a null, any ideas ? why that would be ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 10/31/2012 :  12:41:57  Show Profile  Reply with Quote
quote:
Originally posted by masond

Hi Bandi

Although i have put isnul into my query, the results are still producing a null, any ideas ? why that would be ?


you mean NULL values in PIVOT result? thats because it doesnt have a row with value specified in PIVOT. for that you've to add ISNULL check in final select list

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

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