需要SQL查询才能根据日期生成订单ID

时间:2013-10-10 22:27:22

标签: sql sql-server sql-server-2008 common-table-expression

我正在尝试编写一个SQL查询来生成看起来像

的数据的orderid
 ID SYMBOL   Date          OrderID
 112    GR   24-Jan-13     0
 112    FIX  31-Aug-11     0
 112    FIX  29-Sep-11     0
 112    FIX  17-Nov-11     0
 112    MIN  19-Dec-08     0
 112    MIN  05-Feb-09     0
 445    GR   24-Jan-13     0
 445    GR   25-Jul-13     0
 445    FIX  31-Aug-11     0
 445    FIX  29-Sep-11     0
 445    FIX  17-Nov-11     0

需要输出为:

ID  SYMBOL  Date        OrderID
112 GR      24-Jan-13   1
112 GR      25-Jul-13   2
112 FIX     31-Aug-11   1
112 FIX     29-Sep-11   2
112 FIX     17-Nov-11   3
112 MIN     19-Dec-08   1
112 MIN     05-Feb-09   2
445 GR      24-Jan-13   1
445 GR      25-Jul-13   2
445 FIX     31-Aug-11   1
445 FIX     29-Sep-11   2
445 FIX     17-Nov-11   3

查询: 我的尝试:

;with ctes  (ID, SYMBOL, date, orderid)as
 (
    select ID, SYMBOL, date , orderid from #temp

Union all

select  m.ID, m.SYMBOL, m.date, t.orderid + 1
from ctes t 
inner join #temp m on t.ID = m.id 
and t.symbol = m.symbol

  )select  * from ctes



create table #temp (id int, SYMBOL varchar(5), [Date] datetime, orderid int)
insert into #temp (id, symbol, [date], orderid)
values(112, 'GR',   '24-Jan-13',    0) 
insert into #temp (id, symbol, [date], orderid)
values(112,'GR', '25-Jul-13',   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'FIX',  '31-Aug-11' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'FIX',  '29-Sep-11' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'FIX',  '17-Nov-11' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'FIX',  '14-Jan-13' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'MIN',  '19-Dec-08' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'MIN',  '05-Feb-09' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'MIN',  '19-Aug-09' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(112, 'MIN',  '19-Sep-13' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(445, 'GR',   '24-Jan-13' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(445, 'GR',   '25-Jul-13' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(445, 'FIX',  '31-Aug-11' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(445, 'FIX',  '29-Sep-11' ,   0)
insert into #temp (id, symbol, [date], orderid)
values(445, 'FIX',  '17-Nov-11',    0)

1 个答案:

答案 0 :(得分:2)

SQL Server实际上有一个buildin函数:ROW_NUMBER()

在你的情况下,你会像这样使用它:

SQL Fiddle

MS SQL Server 2008架构设置

create table dbo.temp (id int, SYMBOL varchar(5), [Date] datetime, orderid int)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'GR',   '24-Jan-13',    0) 
insert into dbo.temp (id, symbol, [date], orderid)
values(112,'GR', '25-Jul-13',   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'FIX',  '31-Aug-11' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'FIX',  '29-Sep-11' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'FIX',  '17-Nov-11' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'FIX',  '14-Jan-13' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'MIN',  '19-Dec-08' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'MIN',  '05-Feb-09' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'MIN',  '19-Aug-09' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(112, 'MIN',  '19-Sep-13' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(445, 'GR',   '24-Jan-13' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(445, 'GR',   '25-Jul-13' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(445, 'FIX',  '31-Aug-11' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(445, 'FIX',  '29-Sep-11' ,   0)
insert into dbo.temp (id, symbol, [date], orderid)
values(445, 'FIX',  '17-Nov-11',    0)

查询1

SELECT id,SYMBOL,[Date],
       ROW_NUMBER()OVER(PARTITION BY id,SYMBOL ORDER BY [Date]) AS OrderId
  FROM dbo.Temp
 ORDER BY id,SYMBOL,[Date]

<强> Results

|  ID | SYMBOL |                             DATE | ORDERID |
|-----|--------|----------------------------------|---------|
| 112 |    FIX |    August, 31 2011 00:00:00+0000 |       1 |
| 112 |    FIX | September, 29 2011 00:00:00+0000 |       2 |
| 112 |    FIX |  November, 17 2011 00:00:00+0000 |       3 |
| 112 |    FIX |   January, 14 2013 00:00:00+0000 |       4 |
| 112 |     GR |   January, 24 2013 00:00:00+0000 |       1 |
| 112 |     GR |      July, 25 2013 00:00:00+0000 |       2 |
| 112 |    MIN |  December, 19 2008 00:00:00+0000 |       1 |
| 112 |    MIN |  February, 05 2009 00:00:00+0000 |       2 |
| 112 |    MIN |    August, 19 2009 00:00:00+0000 |       3 |
| 112 |    MIN | September, 19 2013 00:00:00+0000 |       4 |
| 445 |    FIX |    August, 31 2011 00:00:00+0000 |       1 |
| 445 |    FIX | September, 29 2011 00:00:00+0000 |       2 |
| 445 |    FIX |  November, 17 2011 00:00:00+0000 |       3 |
| 445 |     GR |   January, 24 2013 00:00:00+0000 |       1 |
| 445 |     GR |      July, 25 2013 00:00:00+0000 |       2 |