English 中文(简体)
SQL "Group By" VarChar Field With Max Date or All results of the same date
原标题:

Lets say I have this table

--------------------------------------------------------------
|  ID  |  DATE  |  GROUPNAME  |  RESULT  |  INFO1  |  INFO2  |
--------------------------------------------------------------
| 1    | 01/06  | Group1      | 12345    | Abc     | xxxx    |
| 2    | 01/04  | Group2      | 54321    | AAA     | zzzz    |
| 3    | 01/03  | Group3      | 11111    | BBB     | zzzz    |
| 4    | 01/06  | Group1      | 22222    | Def     | xxxx    |
| 5    | 01/02  | Group3      | 33333    | CCC     | yyyy    |
--------------------------------------------------------------

I want to make a query that selects the max date of each groupname and return all results of that date from that groupname. And order by the groupname

E.g., My result would be

1 | 01/06 | Group1 | 12345 | Abc | xxxx
4 | 01/06 | Group1 | 22222 | Def | xxxx
2 | 01/04 | Group2 | 54321 | AAA | zzzz
3 | 01/03 | Group3 | 11111 | BBB | zzzz

What would be an efficient query to produce that result set?

Thank you!

最佳回答

Unless I m missing something:

SELECT t.id,
       t.date,
       t.groupname,
       t.result,
       t.info1,
       t.info2
  FROM TABLE t
  JOIN (SELECT t.groupname,
               MAX(t.date)  maxd 
          FROM TABLE t
      GROUP BY t.groupname) x ON x.groupname = t.groupname
                             AND x.maxd = t.date
ORDER BY t.groupname
问题回答
SELECT TABLE.ID, TABLE.DATE, TABLE.GROUPNAME, TABLE.RESULT, TABLE.INFO1, TABLE.INFO2
FROM TABLE INNER JOIN (SELECT GROUPNAME, MAX(DATE) FROM TABLE GROUP BY GROUPNAME) T
ON TABLE.GROUPNAME = T.GROUPNAME AND TABLE.DATE = T.DATE
ORDER BY TABLE.GROUPNAME 

try this:

Width (
Select max(Date), groupname
from Table
group gy groupname
) AS T1
Select T2.id,
       T2.Date,
       T2.groupname,
       T2.result,
       T2.info1,
       T2.info2
from T1 
left join Table T2 
on (T1.date = T2.date 
AND T1.groupname = T2.groupname) 
order by groupname




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签