sql-server 如何在SQL中将相同ID的行转换为具有相同标题的列?[closed]

hpcdzsge  于 2022-10-31  发布在  其他
关注(0)|答案(1)|浏览(158)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

4天前关闭。
这篇文章3天前被编辑并提交审查。
Improve this question
我有一个数据集

生成此数据集所运行的查询:

CREATE TABLE #TempAppointmentTable (
    UniqueID int,
    DateOfAppointment nvarchar(500),
    TypeOfAppointment nvarchar(500),
    AppointmentType nvarchar(500),
    VisitStage int
);

INSERT INTO #TempAppointmentTable
    ([UniqueID], [DateOfAppointment], [TypeOfAppointment], [AppointmentType],[VisitStage])
VALUES
    (82, '2022-03-10 00:00:00.000', 'a','d',1),
    (82, '2022-03-15 00:00:00.000', 'b','e',2),
    (82, '2022-03-20 00:00:00.000', 'c','f',3),
    (83, '2022-03-20 00:00:00.000', 'c','g',4),
    (83, '2022-03-20 00:00:00.000', 'c','h',5)
;
SELECT * FROM #TempAppointmentTable

DROP TABLE #TempAppointmentTable

我需要得到一个输出,在一行中显示每个UniqueID的每一列,如下所示

我使用的是SQL Server

mf98qq94

mf98qq941#

您可以使用数据表的COUNT(*)来建立查询。
它将从每行到第一行执行JOIN操作,直到没有剩余的行

CREATE TABLE #TempAppointmentTable (
    UniqueID int,
    DateOfAppointment nvarchar(500),
    TypeOfAppointment nvarchar(500),
    AppointmentType nvarchar(500),
    VisitStage int
);

INSERT INTO #TempAppointmentTable
    ([UniqueID], [DateOfAppointment], [TypeOfAppointment], [AppointmentType],[VisitStage])
VALUES
    (82, '2022-03-10 00:00:00.000', 'a','d',1),
    (84, '2022-03-20 00:00:00.000', 'e','h',5),
    (84, '2022-03-20 00:00:00.000', 'f','i',5),
    (82, '2022-03-15 00:00:00.000', 'b','e',2),
    (82, '2022-03-20 00:00:00.000', 'c','f',3),
    (83, '2022-03-20 00:00:00.000', 'd','g',4),
    (84, '2022-03-20 00:00:00.000', 'g','j',5),
    (84, '2022-03-20 00:00:00.000', 'h','k',5),
    (84, '2022-03-20 00:00:00.000', 'i','l',5)
;

DECLARE @list_id TABLE (idx INT IDENTITY, id INT)
DECLARE @nb_rows INT
DECLARE @nb_cols INT
DECLARE @i INT = 0
DECLARE @j INT = 0
DECLARE @cur_id INT
DECLARE @cur_nb_rows INT
DECLARE @sql VARCHAR(MAX)

INSERT INTO @list_id(id) SELECT [UniqueID] FROM #TempAppointmentTable GROUP BY [UniqueID] ORDER BY COUNT(*) DESC;
SELECT @nb_rows = COUNT(*) FROM @list_id;
SELECT @nb_cols = MAX(val) FROM (SELECT COUNT(*) FROM #TempAppointmentTable GROUP BY [UniqueID])t(val);

SET @sql = '
WITH TMP ([Row], [UniqueID], [DateOfAppointment], [TypeOfAppointment], [AppointmentType],[VisitStage]) AS
(
    select ROW_NUMBER() OVER(PARTITION BY [UniqueID] ORDER BY UniqueID), * from #TempAppointmentTable
)'

WHILE @i < @nb_rows
BEGIN
    SELECT @cur_id = id FROM @list_id WHERE idx = @i+1
    SELECT @cur_nb_rows = COUNT(*) FROM #TempAppointmentTable WHERE [UniqueID] = @cur_id
    IF @i != 0 
        SET @sql = @sql + '
UNION'
    SET @sql = @sql + '
SELECT TMP' + CONVERT(VARCHAR,@i) + '0.[UniqueID]'
    WHILE @j+1 < @nb_cols
    BEGIN
        IF @j < @cur_nb_rows
            SET @sql = @sql + ', TMP'+ CONVERT(VARCHAR,@i) + CONVERT(VARCHAR,@j) + '.[DateOfAppointment] AS [DateOfAppointment' +  CONVERT(VARCHAR,@j) + ']'
                            + ', TMP'+ CONVERT(VARCHAR,@i) + CONVERT(VARCHAR,@j) + '.[TypeOfAppointment] AS [TypeOfAppointment' +  CONVERT(VARCHAR,@j) + ']'
                            + ', TMP'+ CONVERT(VARCHAR,@i) + CONVERT(VARCHAR,@j) + '.[AppointmentType] AS [AppointmentType' +  CONVERT(VARCHAR,@j) + ']'
                            + ', TMP'+ CONVERT(VARCHAR,@i) + CONVERT(VARCHAR,@j) + '.[VisitStage] AS [VisitStage' +  CONVERT(VARCHAR,@j) + ']'
        ELSE
            SET @sql = @sql + ', NULL, NULL, NULL, NULL'
        SET @j = @j + 1
    END
    SET @j = 0
    SET @sql = @sql + '
FROM TMP AS TMP' + CONVERT(VARCHAR,@i) + '0'
    WHILE @j+1 < @nb_cols
    BEGIN
        SET @j = @j + 1
        IF @j < @cur_nb_rows
            SET @sql = @sql + '
INNER JOIN TMP AS TMP' + CONVERT(VARCHAR,@i) + CONVERT(VARCHAR,@j) + ' ON TMP' + CONVERT(VARCHAR,@i) + CONVERT(VARCHAR,@j) + '.Row = ' + CONVERT(VARCHAR,@j+1) +' AND TMP' + CONVERT(VARCHAR,@i) + CONVERT(VARCHAR,@j) + '.UniqueID = ' + CONVERT(VARCHAR,@cur_id)
    END
    SET @sql = @sql + '
WHERE TMP' + CONVERT(VARCHAR,@i) + '0.Row = 1 AND TMP' + CONVERT(VARCHAR,@i) + '0.UniqueID = ' + CONVERT(VARCHAR,@cur_id)
    SET @j = 0
    SET @i = @i + 1
END

PRINT(@sql)

EXEC(@sql)

Drop table #TempAppointmentTable

输出如下所示
| 唯一ID|任命日期0|预约类型0|约会类型0|访问阶段0|任命日期1|任命类型1|预约类型1|访视阶段1|任命日期2|任命类型2|预约类型2|访视阶段2|任命日期3|任命类型3|预约类型3|访视阶段3|
| - -|- -|- -|- -|- -|- -|- -|- -|- -|- -|- -|- -|- -|- -|- -|- -|- -|
| 八十二人|2022年3月10日上午10时00分00秒|一种|日|一个|2022年3月15日上午10时00分00秒|B|电子|2个|2022年03月20日00:00:00.000| C语言|关闭|三个|空值|空值|空值|空值|
| 八十三个|2022年03月20日00:00:00.000|日|克|四个|空值|空值|空值|空值|空值|空值|空值|空值|空值|空值|空值|空值|
| 八十四人|2022年03月20日00:00:00.000|电子|小时|五个|2022年03月20日00:00:00.000|关闭|我|五个|2022年03月20日00:00:00.000|克|j型|五个|2022年03月20日00:00:00.000|小时|k型|五个|

相关问题