Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've had similar problems as this before and while scouring my own questions at ask-a-question sites I only come part to a solution.

I have the following query here:

SQL
select fcontainer.id, FlaggId, sum(FContainerRow.Units) as units ,fcontainerplats.x,FContainerPlats.y,FContainerHylla.Name,
tempo.temponr,produkter.Artikelnummer,produkter.Produktnamn

from fcontainer inner join fcontainerrow on fcontainer.id = FContainerRow.FContainerId 
inner join fcontainerplats on fcontainer.PlatsId = FContainerPlats.id
inner join FContainerHylla on fcontainerplats.HyllId = FContainerHylla.Id
inner join tempo on fcontainerrow.ExtKeyId = tempo.lagernr
inner join produkter on produkter.Produktnr = tempo.produktnr

where ExtKeyName = 'Lager' and fcontainer.PlatsId > 0
group by fcontainer.id, flaggid, fcontainerplats.x,FContainerPlats.y, FContainerHylla.Name,tempo.temponr,produkter.Artikelnummer,produkter.Produktnamn


My problem is that multiple entries in tempo can have the same value in tempo.lagernr which means that for one fcontainer.id I get multiple rows.

I don't care which row of these duplicates I select as long as there is only one row per fcontainer.id in my final query. I've been tin kering with row_number() over partion as well as using CTE but I just cant get it right.

When "counting" the rows using rownumber I'm stuck at selecting rn = 1 which only returns those rows where there is no duplicate.
Posted
Comments
Tomas Takac 26-Oct-15 4:01am    
Remove tempo.temponr from GROUP BY and use MAX(tempo.temponr) in SELECT clause.
Member 11683251 26-Oct-15 4:07am    
Still the same results, I realized that it's not tempo.temponr that exists duplicates from but it's the tempo.lagernr. Multiple entries in tempo can have the same lagernr. But select max(tempo.lagernr) instead still generates duplicates.
Tomas Takac 26-Oct-15 6:21am    
The reason most probably being the join chain fcontainerrow -> tempo -> produkter. So if tempo duplicates the rows then this "duplication" is propagated to produkter. I don't know enough about you data structure to help here. I guess you need to prepare (pre-aggregate) data in tempo & produkter so it has only one row per a tempo.lagernr and then use this data in you join.
Member 11683251 26-Oct-15 4:12am    
After thinking this might help better: Muliple rows in fcontainerrow can have the same fcontainerid. Which Is why I sum fcontainerrow.units. Fcontainerrow.fcontainerid = fcontainer.id.

Each of these rows contain a column called extkeyid. The value here maps to tempo.lagernr in which multiple entries in tempo can have the same value here.

1 solution

This should work.
SQL
WITH cte AS (
    SELECT
            fcontainer.id
           ,FlaggId
           ,Sum(FContainerRow.Units) AS units
           ,fcontainerplats.x
           ,FContainerPlats.y
           ,FContainerHylla.Name
           ,tempo.temponr
           ,produkter.Artikelnummer
           ,produkter.Produktnamn
           ,ROW_NUMBER() OVER (PARTITION BY fcontainer.id) rn
    FROM    fcontainerrow
    JOIN    fcontainer ON  FContainerRow.FContainerId = fcontainer.id 
    JOIN    fcontainerplats ON  fcontainer.PlatsId = FContainerPlats.id
    JOIN    FContainerHylla ON  fcontainerplats.HyllId = FContainerHylla.Id
    JOIN    Tempo ON  fcontainerrow.ExtKeyId = tempo.lagernr
    JOIN    produkter ON  produkter.Produktnr = tempo.produktnr
    WHERE   ExtKeyName = 'Lager'
        AND fcontainer.PlatsId > 0
    GROUP BY fcontainer.id
            ,flaggid
            ,fcontainerplats.x
            ,FContainerPlats.y
            ,FContainerHylla.Name
            ,tempo.temponr
            ,produkter.Artikelnummer
            ,produkter.Produktnamn
    )
SELECT   
        fcontainer.id
       ,FlaggId
       ,units
       ,x
       ,y
       ,Name
       ,temponr
       ,Artikelnummer
       ,Produktnamn
FROM    cte
WHERE   rn = 1
 
Share this answer
 
v2
Comments
Richard Deeming 28-Oct-15 13:27pm    
In English, please. :)
Jörgen Andersson 28-Oct-15 14:30pm    
Sorry 'bout that. I realized he/she is Swedish and my brain swapped languages.
Updated
phil.o 28-Oct-15 14:54pm    
5'd
CTEs are really elegant solutions to complex issues.
Jörgen Andersson 29-Oct-15 3:59am    
Thanks
Maciej Los 28-Oct-15 15:51pm    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900