查询返回具有重复项的记录的1个实例

时间:2009-04-03 21:29:55

标签: sql database

信息:我正在使用Microsoft SQL 好吧标题令人困惑,但这是我正在使用的表的一个例子:

ID    Value    Signal    Read    Firmware    Date           Time
5     123      656       444       217       3/30/2009     11:00:00 AM
5     123      421       333       217       3/30/2009     04:00:00 PM
5     123      111       666       217       3/30/2009     05:00:00 PM
9     321      231       551       216       3/30/2009     09:00:00 AM
9     321      599       887       216       3/30/2009     09:30:00 AM

所以我希望Query返回:

ID    Value    Signal    Read    Firmware    Date           Time
5     123      111       666       217       3/30/2009     05:00:00 PM
9     321      599       887       216       3/30/2009     09:30:00 AM

我试过了:

SELECT DISTINCT ID, Value, Signal, Read, Firmware, Date, Time FROM ....

但这会返回所有结果。我也试过了SELECT TOP 1 ......但我无法让它工作。我知道这很简单,但我对如何只显示一个唯一的行感到困惑 谢谢你的帮助。

6 个答案:

答案 0 :(得分:8)

你试过这个吗?

SELECT id, value, MIN(Signal), MIN(Read), MIN(Firmware), MIN(Date), MIN(Time)
FROM
  ...
GROUP BY
  ID, Value

答案 1 :(得分:1)

SELECT
  ID, Value, Signal, Read, Firmware, Date, Time
FROM
  ...
GROUP BY
  ID, Value

答案 2 :(得分:1)

记录 不同,有不同的SignalReadTime值。您希望SQL服务器如何猜测您想要哪一个?

您的示例表明您对给定Id的最新记录感兴趣。如果这是真的,这个查询应该适合你:

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)

答案 3 :(得分:0)

SELECT Id, Value, Signal, Read, Firmware, Date, Time FROM table_name t1 WHERE t1.Id = (SELECT DISTINCT t2.Id FROM table_name t2)

假设有一个检查,以便具有相同Id的记录也具有相同的值

答案 4 :(得分:0)

使用TOP T WITH TIES

答案 5 :(得分:0)

类似查询(使用CROSS APPLY和相关子查询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