我想要使用日期时间升序选择不同的数字作为列
I want distinct numbers with choice as columns using datetime ascending order
有一个table和
1001 vsp,science,BBA 25-05-2020
1001 vsp,Maths,Btech 26-05-2020
1001 vsp,Maths,Btech 27-05-2020
1002 hyd,science,BBA 24-05-2020
1002 blr,Maths,Btech 25-05-2020
我要
1001 vsp,science,bba vsp,Maths,Btech vsp,Maths,Btech
你需要一个我最喜欢的组合来实现你的目标:
- CTE(创建适当的子请求):https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql
- ROW_NUMBER(对行进行排名):https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql
- PIVOT(调整结果):https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot
现在解决方案:
WITH orderedCourse AS
(
SELECT GroupId,
CourseLabel,
ROW_NUMBER() OVER (PARTITION BY GroupId ORDER BY CourseDate) AS CourseNumber
FROM @myCourses
)
SELECT TOP (1) GroupId, [1], [2], [3], [4]
FROM
(
SELECT GroupId,
CourseLabel,
CourseNumber
FROM orderedCourse
) AS src
PIVOT
(
MIN(CourseLabel) -- default agregate
FOR CourseNumber IN ([1], [2], [3], [4] /*... if you have more courses by group*/)
) AS pvt
ORDER BY GroupId
给出结果:
1001 vsp,science,BBA vsp,Maths,Btech vsp,Maths,Btech NULL
我用这段代码声明了 table:
INSERT INTO @myCourses
SELECT 1001, 'vsp,science,BBA', CAST('25-05-2020' AS date) UNION ALL
SELECT 1001, 'vsp,Maths,Btech', CAST('26-05-2020' AS date) UNION ALL
SELECT 1001, 'vsp,Maths,Btech', CAST('27-05-2020' AS date) UNION ALL
SELECT 1002, 'yd,science,BBA', CAST('24-05-2020' AS date) UNION ALL
SELECT 1002, 'blr,Maths,Btech', CAST('25-05-2020' AS date);
SELECT GroupId,
CourseLabel,
CourseDate,
ROW_NUMBER() OVER (PARTITION BY GroupId ORDER BY CourseDate) AS CourseNumber
FROM @myCourses;
只需将条件聚合与 row_number()
:
一起使用
select col1,
max(case when seqnum = 1 then col2 end),
max(case when seqnum = 2 then col2 end),
max(case when seqnum = 3 then col2 end)
from (select t.*,
row_number() over (partition by col1 order by col3) as seqnum
from t
) t
group by col1;
这与字符串聚合无关。它也不需要过多的子查询和 CTE。
有一个table和
1001 vsp,science,BBA 25-05-2020
1001 vsp,Maths,Btech 26-05-2020
1001 vsp,Maths,Btech 27-05-2020
1002 hyd,science,BBA 24-05-2020
1002 blr,Maths,Btech 25-05-2020
我要
1001 vsp,science,bba vsp,Maths,Btech vsp,Maths,Btech
你需要一个我最喜欢的组合来实现你的目标:
- CTE(创建适当的子请求):https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql
- ROW_NUMBER(对行进行排名):https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql
- PIVOT(调整结果):https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot
现在解决方案:
WITH orderedCourse AS
(
SELECT GroupId,
CourseLabel,
ROW_NUMBER() OVER (PARTITION BY GroupId ORDER BY CourseDate) AS CourseNumber
FROM @myCourses
)
SELECT TOP (1) GroupId, [1], [2], [3], [4]
FROM
(
SELECT GroupId,
CourseLabel,
CourseNumber
FROM orderedCourse
) AS src
PIVOT
(
MIN(CourseLabel) -- default agregate
FOR CourseNumber IN ([1], [2], [3], [4] /*... if you have more courses by group*/)
) AS pvt
ORDER BY GroupId
给出结果:
1001 vsp,science,BBA vsp,Maths,Btech vsp,Maths,Btech NULL
我用这段代码声明了 table:
INSERT INTO @myCourses
SELECT 1001, 'vsp,science,BBA', CAST('25-05-2020' AS date) UNION ALL
SELECT 1001, 'vsp,Maths,Btech', CAST('26-05-2020' AS date) UNION ALL
SELECT 1001, 'vsp,Maths,Btech', CAST('27-05-2020' AS date) UNION ALL
SELECT 1002, 'yd,science,BBA', CAST('24-05-2020' AS date) UNION ALL
SELECT 1002, 'blr,Maths,Btech', CAST('25-05-2020' AS date);
SELECT GroupId,
CourseLabel,
CourseDate,
ROW_NUMBER() OVER (PARTITION BY GroupId ORDER BY CourseDate) AS CourseNumber
FROM @myCourses;
只需将条件聚合与 row_number()
:
select col1,
max(case when seqnum = 1 then col2 end),
max(case when seqnum = 2 then col2 end),
max(case when seqnum = 3 then col2 end)
from (select t.*,
row_number() over (partition by col1 order by col3) as seqnum
from t
) t
group by col1;
这与字符串聚合无关。它也不需要过多的子查询和 CTE。