我想解决这个问题:
我有这样的表或数据:
id date
1 2014-07-01 00:00:00.000
2 2014-07-01 00:00:00.000
3 2014-07-01 00:00:00.000
4 2014-07-03 00:00:00.000
5 2014-07-03 00:00:00.000
6 2014-07-03 00:00:00.000
7 2014-07-03 00:00:00.000
8 2014-07-05 00:00:00.000
9 2014-07-05 00:00:00.000
10 2014-07-05 00:00:00.000
11 2014-07-05 00:00:00.000
12 2014-07-05 00:00:00.000
13 2014-07-05 00:00:00.000
我想按日期分组,然后按百分比划分这些组。可能最好的想法是添加额外的列,使其看起来像这样:
id date group
1 2014-07-01 00:00:00.000 1
2 2014-07-01 00:00:00.000 1
3 2014-07-01 00:00:00.000 2
4 2014-07-03 00:00:00.000 1
5 2014-07-03 00:00:00.000 1
6 2014-07-03 00:00:00.000 2
7 2014-07-03 00:00:00.000 2
8 2014-07-05 00:00:00.000 1
9 2014-07-05 00:00:00.000 1
10 2014-07-05 00:00:00.000 1
11 2014-07-05 00:00:00.000 2
12 2014-07-05 00:00:00.000 2
13 2014-07-05 00:00:00.000 2
有人可以帮我这个,谢谢。
答案 0 :(得分:1)
假设您想要在一个单独的组中为每个日期创建一半的行:
DECLARE @SampleData TABLE(id int, date date)
INSERT INTO @SampleData VALUES
(1 , '2014-07-01 00:00:00.000')
,(2 , '2014-07-01 00:00:00.000')
,(3 , '2014-07-01 00:00:00.000')
,(4 , '2014-07-03 00:00:00.000')
,(5 , '2014-07-03 00:00:00.000')
,(6 , '2014-07-03 00:00:00.000')
,(7 , '2014-07-03 00:00:00.000')
,(8 , '2014-07-05 00:00:00.000')
,(9 , '2014-07-05 00:00:00.000')
,(10 , '2014-07-05 00:00:00.000')
,(11 , '2014-07-05 00:00:00.000')
,(12 , '2014-07-05 00:00:00.000')
,(13 , '2014-07-05 00:00:00.000');
SELECT
id
,date
,(ROW_NUMBER() OVER (PARTITION BY date ORDER BY date) % 2) + 1 AS [DateGroup]
FROM @SampleData;