views:

327

answers:

6

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    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

So I want the Query to return:

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

I have tried:

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

But this returns all of the results. I have also tried SELECT TOP 1... but I couldn't get that to work. I know this is simple, but I'm confused on how to get this to display only 1 single unique row.
Thanks for the help.

+1  A: 
SELECT
  ID, Value, Signal, Read, Firmware, Date, Time
FROM
  ...
GROUP BY
  ID, Value
Seb
Error: Cannot group on fields selected with '*' And it doesn't work when I include the columns.
Nick S.
Well, that was just an example. You'll have to put the exact fields. Let me clarify that for you...
Seb
A: 
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

stack programmer
Error: At most one record can be returned by this subquery.
Nick S.
A: 

use TOP 1 WITH TIES

zvolkov
do you have an example of this? I get error: select statement includes a reserved word.
Nick S.
I'll post a similar query for you to study as an example.
zvolkov
+4  A: 

Have you tried this?

SELECT id, value, MIN(Signal), MIN(Read), MIN(Firmware), MIN(Date), MIN(Time)
FROM
  ...
GROUP BY
  ID, Value
Jhonny D. Cano -Leftware-
i'd do this too!
Martlark
+1  A: 

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)
David Schmitt
A: 

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
zvolkov