SELECT DISTINCT with LEFT JOIN, ORDERed BY in t-SQL

前端 未结 5 2052
悲哀的现实
悲哀的现实 2021-02-14 05:08

I have the following table in SQL Server 2008:

CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT)

INSERT tbl VALUES
(1, \'05:00\', \'6:00\', 1         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 06:00

    You must include the column tbl.dtOut in your select list so it knows what to order on.

    SELECT DISTINCT tbl.id, tbl.dtOut FROM tbl   
      LEFT JOIN tbl AS t1
      ON tbl.type = t1.type AND
         tbl.dtIn = t1.dtIn
      ORDER BY tbl.dtOut ASC
    

提交回复
热议问题