2009-11-06 15 views
5

Sagen wir, ich eine Datenbanktabelle, die wie folgt aussieht:T-SQL: Nach Datum sortieren, dann gruppieren?

ID   name  salary  start_date    city  region 
----------- ---------- ----------- ----------------------- ---------- ------ 
      1 Jason   40420 1994-02-01 00:00:00.000 New York W 
      2 Robert   14420 1995-01-02 00:00:00.000 Vancouver N 
      3 Celia   24020 1996-12-03 00:00:00.000 Toronto W 
      4 Linda   40620 1997-11-04 00:00:00.000 New York N 
      5 David   80026 1998-10-05 00:00:00.000 Vancouver W 
      6 James   70060 1999-09-06 00:00:00.000 Toronto N 
      7 Alison   90620 2000-08-07 00:00:00.000 New York W 
      8 Chris   26020 2001-07-08 00:00:00.000 Vancouver N 
      9 Mary    60020 2002-06-09 00:00:00.000 Toronto W 

Gibt es eine einfache dies durch start_date absteigend zu sortieren, dann für die jede Stadt Gruppe sie durch die neueste start_date? Zum Beispiel:

ID   name  salary  start_date    city  region 
----------- ---------- ----------- ----------------------- ---------- ------ 
     9 Mary    60020 2002-06-09 00:00:00.000 Toronto W 
     6 James   70060 1999-09-06 00:00:00.000 Toronto N 
     3 Celia   24020 1996-12-03 00:00:00.000 Toronto W 
     8 Chris   26020 2001-07-08 00:00:00.000 Vancouver N 
     5 David   80026 1998-10-05 00:00:00.000 Vancouver W 
     2 Robert   14420 1995-01-02 00:00:00.000 Vancouver N 
     7 Alison   90620 2000-08-07 00:00:00.000 New York W 
     4 Linda   40620 1997-11-04 00:00:00.000 New York N 
     1 Jason   40420 1994-02-01 00:00:00.000 New York W 

Vielen Dank für Ihre Antworten.

Antwort

1

Try this:

SELECT * 
FROM 
    (
    SELECT 
     *, 
     (SELECT MAX(start_date) FROM cities c2 WHERE c1.city = c2.city) AS latest_start_date 
    FROM cities c1 
    ) 
ORDER BY latest_start_date DESC, start_date DESC 

Die innere Abfrage wird Ihnen etwas wie folgt aus:

ID   name  salary  start_date    city  region lastest_start_date 
----------- ---------- ----------- ----------------------- ---------- ------ ------------------- 
      1 Jason   40420 1994-02-01 00:00:00.000 New York W  2000-08-07 00:00:00.000 
      2 Robert   14420 1995-01-02 00:00:00.000 Vancouver N  2001-07-08 00:00:00.000 
      3 Celia   24020 1996-12-03 00:00:00.000 Toronto W  2002-06-09 00:00:00.000 
      4 Linda   40620 1997-11-04 00:00:00.000 New York N  2000-08-07 00:00:00.000 
      5 David   80026 1998-10-05 00:00:00.000 Vancouver W  2001-07-08 00:00:00.000 
      6 James   70060 1999-09-06 00:00:00.000 Toronto N  2002-06-09 00:00:00.000 
      7 Alison   90620 2000-08-07 00:00:00.000 New York W  2000-08-07 00:00:00.000 
      8 Chris   26020 2001-07-08 00:00:00.000 Vancouver N  2001-07-08 00:00:00.000 
      9 Mary    60020 2002-06-09 00:00:00.000 Toronto W  2002-06-09 00:00:00.000 
7

In SQL Server 2005 oder höher aussehen könnte:

select 
    * 
from 
    (select *,max(start_date) over(partition by city) max_date from tablename) alias 
order by max_date desc, start_date desc 
+0

+1 nur antworten, dass die Tabelle wählt nur einmal – Andomar

+1

nicht von start_date ab, um zu bestellen Vergessen, nach max_date ab. –

+0

+1. Ich wusste nichts von der OVER-Klausel. – Heinzi

3
SELECT yourTable.* 
    FROM yourTable INNER JOIN 
     (SELECT city, MAX(start_date) AS max_city_date 
     FROM yourTable 
     GROUP BY city) max_dates 
     ON yourTable.city = max_dates.city 
ORDER BY max_dates.max_city_date DESC, yourTable.city, yourTable.start_date DESC 

Die yourTable.city in Die Klausel ORDER BY stellt eine konsistente Gruppierung nach Stadt sicher, wenn zwei Städte das gleiche max_city_date haben.

2

Verknüpfen Sie die Abfrage für sich selbst und gruppieren Sie nach Stadtnamen. Dann können Sie das maximale Startdatum für eine Stadt in der ORDER BY-Klausel verwenden.

select c1.* 
from cities c1 
left join cities c2 on c1.city = c2.city 
group by c1.id, c1.name, c1.salary, c1.start_date, c1.city, c1.region 
order by max(c2.start_date) desc, c1.city, c1.start_date desc 
+0

Warum die linke beitreten? – Thorsten

+0

@IronGoofy: links oder innen würde beides funktionieren – Andomar

2

Die Frage ist nicht klar ... aber das würde produzieren die Tabelle, die Sie aufgelistet:

select * 
from MyTable p 
order by 
    (SELECT MAX(start_date) AS max_city_date FROM MyTable WHERE city=p.city) desc, 
    city, 
    start_date desc 

Ist das, was Sie wollen ?? Gib mir noch ein paar Hinweise, wenn du kannst.

ID   name  salary  start_date    city  region 
----------- ---------- ----------- ----------------------- ---------- ---------- 
9   Mary  60020  2002-06-09 00:00:00.000 Toronto W 
6   James  70060  1999-09-06 00:00:00.000 Toronto N 
3   Celia  24020  1996-12-03 00:00:00.000 Toronto W 
8   Chris  26020  2001-07-08 00:00:00.000 Vancouver N 
5   David  80026  1998-10-05 00:00:00.000 Vancouver W 
2   Robert  14420  1995-01-02 00:00:00.000 Vancouver N 
7   Alison  90620  2000-08-07 00:00:00.000 NewYork W 
4   Linda  40620  1997-11-04 00:00:00.000 NewYork N 
1   Jason  40420  1994-02-01 00:00:00.000 NewYork W