Query to return 1 instance of a record with duplicates

前端 未结 6 1288
耶瑟儿~
耶瑟儿~ 2020-12-11 22:36

INFO: I am working with Microsoft SQL.
Ok the title is confusing but here is an example of the table I\'m working with:

ID    Value    Signal    Read             


        
相关标签:
6条回答
  • 2020-12-11 23:06

    The records are distinct, there are different Signal, Read and Time values. How would you expect the SQL server to guess which one you'd like?

    Your example suggests that you're interested in the most recent record of a given Id. If that's true, this query should work for you:

    SELECT table.*
    FROM table
    JOIN (SELECT Id, Date, MIN(Time) FROM Table GROUP BY Id, Date) AS selector
    ON (table.Id = selector.Id AND table.Date = selector.Date and table.Time = selector.Time)
    
    0 讨论(0)
  • 2020-12-11 23:11

    Have you tried this?

    SELECT id, value, MIN(Signal), MIN(Read), MIN(Firmware), MIN(Date), MIN(Time)
    FROM
      ...
    GROUP BY
      ID, Value
    
    0 讨论(0)
  • 2020-12-11 23:12

    use TOP 1 WITH TIES

    0 讨论(0)
  • 2020-12-11 23:19
    SELECT Id, Value, Signal, Read, Firmware, Date, Time FROM table_name t1 WHERE t1.Id = (SELECT DISTINCT t2.Id FROM table_name t2)
    

    Assuming that there is a check so that records with the same Id also have same Value

    0 讨论(0)
  • 2020-12-11 23:23
    SELECT
      ID, Value, Signal, Read, Firmware, Date, Time
    FROM
      ...
    GROUP BY
      ID, Value
    
    0 讨论(0)
  • 2020-12-11 23:24

    Similar query (uses CROSS APPLY and correlated subquery w/ TOP WITH TIES):

    SELECT
    
            a.CustodianAccountNum,
    
            a.AccountName,
    
            a.AccountName2,
    
            a.AccountName3,
    
            a.AccountStartDate,
    
            a.AccountClosedDate,
    
            a.TaxableFederal,
    
            a.TaxableState,
    
            qq.ValuationDate,
    
            qq.MarketValue,
    
            cg.ClientGroupGUID as ClientGUID,
    
            c.ClientGUID as EntityGUID,
    
            convert (bit, case when b.cnt > 1 then 1 else 0 end) as IsDuplicate 
    
    
    
      FROM (
    
            SELECT      
    
                  a.CustodianAccountNum,
    
                  MIN(a.AccountID) as AccountID,
    
                  count(*) as cnt
    
            FROM Accounts a
    
    
    
            WHERE
    
                  a.AccountID in (SELECT AccountID from dbo.FnGetFilteredAccountIDs(@CurrentUserID)) -- apply permisssions
    
                  and a.DeletedDate is null
    
    
    
            group by a.CustodianAccountNum
    
      ) b
    
      INNER JOIN Accounts a
    
      ON
    
            a.AccountID = b.AccountID
    
    
    
      INNER JOIN ClientAccounts ca
    
      ON
    
            a.AccountID = ca.AccountID
    
            and ca.DeletedDate is null
    
      INNER JOIN Clients c
    
      ON
    
            ca.ClientID = c.ClientID
    
            and c.DeletedDate is null
    
    
    
      INNER JOIN ClientGroups cg
    
      ON
    
            c.ClientGroupID = cg.ClientGroupID 
    
            and cg.DeletedDate is null
    
      CROSS APPLY 
    
      (
    
            SELECT 
    
                  SUM(MarketValue) as MarketValue, 
    
                  MIN(ValuationDate) as ValuationDate 
    
            FROM
    
                  (SELECT TOP 1 WITH TIES arv.MarketValue, arv.ValuationDate 
    
                  FROM AccountReturnValues arv 
    
                  where 
    
                        arv.AccountId = a.AccountId 
    
                        and a.AccountClosedDate is null
    
                  order by ValuationDate desc
    
                  ) q
    
      ) qq
    
    0 讨论(0)
提交回复
热议问题