0

我有一个包含 ID 和 TIMESTAMP 的日志表。我想按 ID 排序,然后按时间戳排序。

例如,这是结果集的样子:

12345   05:40   
12345   05:50   
12345   06:22   
12345   07:55   
12345   08:33   

完成后,我想在第三列中插入一个订单值,表示它在组中的位置从最早到最新。

所以,你会有这样的事情:

12345   05:40   1 <---First entry
12345   05:50   2
12345   06:22   3
12345   07:55   4
12345   08:33   5 <---Last entry

如何在 SQL 语句中做到这一点?我可以选择数据和 ORDER BY ID、TIMESTAMP。但是,我似乎无法根据分组插入订单值。:(

4

3 回答 3

3

试试这个update 不是 insert

小提琴演示在这里

;with cte as(
  select id, yourdate, row_number() over(order by id,yourdate) rn
  from yourTable
)
Update ut Set thirdCol = rn
From yourTable ut join cte on ut.Id = cte.id and ut.yourdate = cte.yourdate

注意:如果您需要获取thirdColumn每个 id 的更新,请使用分区您的行号row_number() over (partition by id, order by order by id,yourdate)

结果:

|    ID | YOURDATE | THIRDCOL |
|-------|----------|----------|
| 12345 |    05:40 |        1 |
| 12345 |    05:50 |        2 |
| 12345 |    06:22 |        3 |
| 12345 |    07:55 |        4 |
| 12345 |    08:33 |        5 |
于 2013-10-15T14:45:01.900 回答
1

使用派生表和更新。

IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
        drop table #TableOne
end


CREATE TABLE #TableOne
( 
SomeColumnA int , 
LetterOfAlphabet varchar(12) , 
PositionOrdinal int not null default 0 
)




INSERT INTO #TableOne ( SomeColumnA , LetterOfAlphabet )
select 123 , 'x'
union all select 123  , 'b'
union all select 123  , 'z'
union all select 123  , 't'
union all select 123  , 'c'
union all select 123  , 'd'
union all select 123  , 'e'
union all select 123  , 'a'

Select 'pre' as SpaceTimeContinium , * from #TableOne order by LetterOfAlphabet

Update 
#TableOne
Set PositionOrdinal = derived1.rowid
From 
( select SomeColumnA , LetterOfAlphabet , rowid = row_number() over (order by LetterOfAlphabet asc)  from #TableOne innerT1  ) 
as derived1
join #TableOne t1
    on t1.LetterOfAlphabet = derived1.LetterOfAlphabet and t1.SomeColumnA = derived1.SomeColumnA


Select 'post' as SpaceTimeContinium, * from #TableOne order by LetterOfAlphabet


IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
        drop table #TableOne
end
于 2013-10-15T14:46:03.577 回答
0

要在不执行 insert 和 update 的情况下获得所需的顺序,您可以设置聚集索引来为您处理它。下面的示例创建一个集群主键。

为此,您必须删除表上已有的所有聚集索引,因为每个表只能有一个聚集索引。

CREATE TABLE dbo.Table_1
(
ID int NOT NULL,
DTStamp datetime NOT NULL
) 


ALTER TABLE dbo.Table_1 ADD CONSTRAINT
PK_Table_1 PRIMARY KEY CLUSTERED 
(
ID,
DTStamp
)

插入一些随机数据进行测试...

    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,getdate());
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,1,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,2,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,3,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12346,dateadd(mi,4,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,5,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,6,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,7,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12340,dateadd(mi,8,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,1,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,2,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,3,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,4,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,5,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,6,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,7,getdate()));
    INSERT INTO [dbo].[Table_1]([ID],[DTStamp])VALUES(12344,dateadd(mi,8,getdate()));

现在查询您的表并查看订单...

SELECT  [ID] ,[DTStamp]  FROM [Table_1]

如果您需要在查询中显示顺序,可以使用 over 子句添加行号。

SELECT  [ID] ,[DTStamp],row_number() over (partition by [ID] order by [ID] ,[DTStamp]) as SortOdr FROM [Table_1]
于 2013-10-15T15:18:10.613 回答