2013-10-10 71 views
1

我想寫一個SQL查詢,生成訂單ID的,看起來像需要SQL查詢根據日期來生成訂單ID

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) 
+3

請出示你已經嘗試了什麼 - 但是,這是否來自9? – sgeddes

+0

+1用於提供可執行示例。 –

回答

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 | 
相關問題