Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

UNION statement and duplicate rows issue Expand / Collapse
Author
Message
Posted Monday, March 4, 2013 1:09 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 10:48 AM
Points: 35, Visits: 323
I'm guessing I'm misunderstanding how to use UNION or its results. But here's what I have and the results:

SELECT
a.PLANT_NO AS PlantNumber
,SUM(a.APPLIED_VOL) AS AppliedVolume

FROM [EdwStaging].[RAW].[FactSettleFee_TIPS_QRMTIPS_QPOST_SETTLE_FEE] a
GROUP BY a.PLANT_NO

UNION

SELECT
CASE
WHEN a.plant_no = 'ALL' THEN e.[PlantNumber]
ELSE a.plant_no
END AS ResolvedPlantNumber
,SUM(TRANS_VOL) AS AppliedVolume

FROM [EdwStaging].[RAW].[FactSettleFee_TIPS_QRMTIPS_QPOST_RPTS_INVOICE_DTL] a

LEFT OUTER JOIN [EdwStaging].[PSTG].[ContractToPlantMapping] e
ON a.ctr_no = e.[ContractNumber]
AND a.PROD_DT BETWEEN e.[PlantEffectiveFromDate] AND e.[PlantEffectiveToDate]
AND a.PROD_DT BETWEEN e.[ContractEffectiveFromDate] AND e.[ContractEffectiveToDate]
AND a.plant_no = 'ALL'
GROUP BY CASE
WHEN a.plant_no = 'ALL' THEN e.[PlantNumber]
ELSE a.plant_no
END


Example result:

PlantNumber --- AppliedVolume
002 --- 324334.00
002 --- 215943448.81
043 --- 3513440.00
043 --- 407522014.65

Is there a way for me to have the plant numbers grouped together so there aren't duplicates?
Such as:
PlantNumber --- AppliedVolume
002 ---- 216,267,782.81
043 --- 411,035,454.65
Post #1426427
Posted Monday, March 4, 2013 1:13 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:51 AM
Points: 5,446, Visits: 7,616
You're good with what you're using UNION for, but you have to keep in mind that UNION vs. UNION ALL is the equivalent of using a DISTINCT or not on the query. It's not grouping/aggregating.

So, for starters, you want to use UNION ALL there because you might otherwise be losing data that you need to feed into your aggregation, unless you can straight dupe rows between the two queries.

Next, what you'll want to do is aggregate the union. It'll basically look like this:

SELECT
SiteCode,
SUM( SomeStuff) AS SumOfStuff
FROM
(SELECT SiteCode, SomeStuff FROM table1
UNION ALL
SELECT SiteCode, SomeStuff From Table2
) AS drv
GROUP By
SiteCode




- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1426430
Posted Monday, March 4, 2013 1:36 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:09 AM
Points: 20,806, Visits: 32,738
Based on your code, something like this will work:


WITH BaseData as (
SELECT
a.PLANT_NO AS PlantNumber,
SUM(a.APPLIED_VOL) AS AppliedVolume
FROM
[EdwStaging].[RAW].[FactSettleFee_TIPS_QRMTIPS_QPOST_SETTLE_FEE] a
GROUP BY
a.PLANT_NO
UNION ALL
SELECT
CASE WHEN a.plant_no = 'ALL'
THEN e.[PlantNumber]
ELSE a.plant_no
END AS ResolvedPlantNumber,
SUM(TRANS_VOL) AS AppliedVolume
FROM
[EdwStaging].[RAW].[FactSettleFee_TIPS_QRMTIPS_QPOST_RPTS_INVOICE_DTL] a
LEFT OUTER JOIN [EdwStaging].[PSTG].[ContractToPlantMapping] e
ON a.ctr_no = e.[ContractNumber]
AND a.PROD_DT BETWEEN e.[PlantEffectiveFromDate] AND e.[PlantEffectiveToDate]
AND a.PROD_DT BETWEEN e.[ContractEffectiveFromDate] AND e.[ContractEffectiveToDate]
AND a.plant_no = 'ALL'
GROUP BY
CASE WHEN a.plant_no = 'ALL'
THEN e.[PlantNumber]
ELSE a.plant_no
END
)
select
PlantNumber,
sum(AppliedVolume)
from
BaseData
group by
PlantNumber;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1426443
Posted Monday, March 4, 2013 1:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 10:48 AM
Points: 35, Visits: 323
Thanks Evil Kraig F! Exactly what I needed.
Post #1426452
Posted Monday, March 4, 2013 1:54 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 10:48 AM
Points: 35, Visits: 323
And thanks Lynn Pettis! Just saw your post.
Post #1426453
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse