0

I've found myself running in to this on multiple occasions now where I got a query and would need too further refine it. Where one column might contain duplicates and I need to evaluate which row to keep and which rows needs to be discarded.

So far I've had trouble to apply what I've found on my queries as most examples contains selections from one or two tables where my queries are three or more.

I've tried using group by and having count as these examples show but I just get a lot of aggregate errors.

I think what I'd like to do would be to use the table created from my query and then query that one if it's possible and basically cull rows which contains duplicates in one of the columns depending on some criteria. For example if one row contains a datetime remove all except the newest.

SELECT
    stamps.stampsnr AS 'stampsnr' ,
    stamps.time AS 'time' ,
    stamps.amount AS 'amount' ,
    products.productnamn AS 'productnamn' ,
    products.Artikelnummer AS 'Artikelnummer' ,
    FContainer.id AS 'FlaggId' ,
    FContainer.FlaggId AS 'Flagga' ,
    Tempo.tempo AS 'nuTempoNr' ,
    Tempo.amounttempo AS 'amountTempon'
FROM
    stamps
INNER JOIN Tempo
ON  stamps.temponr = Tempo.temponr
INNER JOIN products
ON  Tempo.productnr = products.productnr
INNER JOIN FContainer
ON  stamps.ID = FContainer.id
WHERE
    ( stamps.time > '" & dtmYesterday & "' + ' 06:00:00' )
    AND ( stamps.time < '" & dtmNow & "' + ' 06:00:00' )
    AND FContainer.flaggid = 5
ORDER BY
    time;

Here is an example query. Now on the table created I'd like to check the column renamed to flaggID for duplicates if any found remove every row except the newest and the time is stored in stamps.time.

4
  • You can add a column Row_Number , order by datetime DESC and then filter it by this new field .Like this : WHERE RowNumber = 1
    – Sabin B
    Commented Jun 2, 2015 at 9:00
  • Could you add some examples? I'm afraid I don't fully understand the problem. Commented Jun 2, 2015 at 9:03
  • Updated to show an example query. - dezso
    – Yo444
    Commented Jun 2, 2015 at 9:33
  • As an aside, your time predicate will miss any values which are exactly 06:00:00. Better to have time >= xxx and time < yyy. Commented Jun 2, 2015 at 10:53

1 Answer 1

0

As @sabinbio commented, you need to add a ROW_NUMBER:

SELECT *
FROM 
 (
   SELECT
      stamps.stampsnr AS 'stampsnr'
     ,stamps.time AS 'time'
     ,stamps.amount AS 'amount'
     ,products.productnamn AS 'productnamn'
     ,products.Artikelnummer AS 'Artikelnummer'
     ,FContainer.id AS 'FlaggId'
     ,FContainer.FlaggId AS 'Flagga'
     ,Tempo.tempo AS 'nuTempoNr'
     ,Tempo.amounttempo AS 'amountTempon'
     ,ROW_NUMBER()  
      OVER (PARTITION BY FContainer.FlaggId  -- for each FlaggId
            ORDER BY stamps.time DESC) AS rn -- assign #1 to latest time
   FROM stamps
   INNER JOIN Tempo
     ON stamps.temponr = Tempo.temponr 
   INNER JOIN products
     ON Tempo.productnr = products.productnr
   INNER JOIN FContainer
     ON stamps.ID = FContainer.id

   WHERE  (stamps.TIME > '" & dtmYesterday & "' + ' 06:00:00') 
     AND (stamps.TIME < '" & dtmNow & "' + ' 06:00:00') 
     AND fcontainer.flaggid = 5 
 ) AS dt
WHERE rn = 1
ORDER BY time
3
  • This query run fine but doesn't do what I want. It seems to only select the newest row out of all rows, not out of each duplicate. I might not have been clear enough in my questions. I want to keep all non duplicate rows but when there is a duplicate I want to remove the oldest of these and just keep the newest in my selection with all the non duplicates.
    – Yo444
    Commented Jun 2, 2015 at 10:46
  • @Yo444: You didn't tell exactly based on which column(s) you define duplicates, I assumed FContainer.FlaggId. Simply put the duplicate columns into the PARTITION BY clause.
    – dnoeth
    Commented Jun 2, 2015 at 10:49
  • Saw that I misspelled when I edited the question. What Fcontainer.id I wanted to check but I had written that I renamed it as flagged instead of Flaggid. Easy to change. I think I actually understand what you did and am greatful for your help. :)
    – Yo444
    Commented Jun 2, 2015 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.