Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UNION statement and duplicate rows issue


UNION statement and duplicate rows issue

Author
Message
hogpen
hogpen
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
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
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5697 Visits: 7660
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
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
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;




Cool
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)
hogpen
hogpen
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 323
Thanks Evil Kraig F! Exactly what I needed.
hogpen
hogpen
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 323
And thanks Lynn Pettis! Just saw your post.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search