根据SQL Server表中存在的行编号,行分为列

发布于 2025-01-30 09:56:16 字数 5335 浏览 4 评论 0原文

我在SQL Server表中的一组车辆故障零件如下:

车辆_ID失败零件ROWNUMBER
100011
100021
10003 1 1 00012
100012
1 0001 1 21 00062
100072
100013
100090002 1 2 0002 1 2 0002 1 2 0002 1
3 200012 0002 1 2 0002 1
200021 1 2 0002 1
200032
200072
300061
300071
400111
400151
400161

ROWNUMBER是我们必须定义行的列。结果表如下。 Maximum number of the failed part column count is 5.

Vehicle_IDFailed Part1Failed Part2Failed Part3Failed Part4Failed Part5
1000100020003
1000100060007
100010009
200010002
200030007
300060007
4001100150016

我尝试了以下语句,但无法确切的结果。

WITH cte AS 
(
    SELECT 
        *, 
        ROW_NUMBER() OVER (PARTITION BY Vehicle_ID,RowNumber)
                           ORDER BY [Failed Part]) rn
    FROM 
        VehicleTable
)
SELECT
    Vehicle_ID,
    MAX(CASE WHEN rn = 1 THEN [Failed Part] END) AS FailedPart1,
    MAX(CASE WHEN rn = 2 THEN [Failed Part] END) AS FailedPart2,
    MAX(CASE WHEN rn = 3 THEN [Failed Part] END) AS FailedPart3,
    MAX(CASE WHEN rn = 4 THEN [Failed Part] END) AS FailedPart4,
    MAX(CASE WHEN rn = 5 THEN [Failed Part] END) AS FailedPart5
FROM 
    cte 
GROUP BY
    Vehicle_ID

I have set of vehicle failed parts in a SQL Server table as per below:

Vehicle_IDFailed PartRowNumber
100011
100021
100031
100012
100062
100072
100013
100093
200011
200021
200032
200072
300061
300071
400111
400151
400161

RowNumber is the column we have to define the row. The results table like below. Maximum number of the failed part column count is 5.

Vehicle_IDFailed Part1Failed Part2Failed Part3Failed Part4Failed Part5
1000100020003
1000100060007
100010009
200010002
200030007
300060007
4001100150016

I tried below statement but could not arrive exact results.

WITH cte AS 
(
    SELECT 
        *, 
        ROW_NUMBER() OVER (PARTITION BY Vehicle_ID,RowNumber)
                           ORDER BY [Failed Part]) rn
    FROM 
        VehicleTable
)
SELECT
    Vehicle_ID,
    MAX(CASE WHEN rn = 1 THEN [Failed Part] END) AS FailedPart1,
    MAX(CASE WHEN rn = 2 THEN [Failed Part] END) AS FailedPart2,
    MAX(CASE WHEN rn = 3 THEN [Failed Part] END) AS FailedPart3,
    MAX(CASE WHEN rn = 4 THEN [Failed Part] END) AS FailedPart4,
    MAX(CASE WHEN rn = 5 THEN [Failed Part] END) AS FailedPart5
FROM 
    cte 
GROUP BY
    Vehicle_ID

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2025-02-06 09:56:16

您的代码中的问题真的很微妙。您正在做的是在ware_id上分组,问题在于,输出表中的每个perhate_id都有多个行。相反,您应该做的是通过在CTE中计算的“ Code> rn ”上分组:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY Vehicle_ID, RowNumber
                                 ORDER BY [Failed Part]) rn
    FROM VehicleTable
)
SELECT
    Vehicle_ID,
    MAX(CASE WHEN rn = 1 THEN [Failed Part] END) AS FailedPart1,
    MAX(CASE WHEN rn = 2 THEN [Failed Part] END) AS FailedPart2,
    MAX(CASE WHEN rn = 3 THEN [Failed Part] END) AS FailedPart3,
    MAX(CASE WHEN rn = 4 THEN [Failed Part] END) AS FailedPart4,
    MAX(CASE WHEN rn = 5 THEN [Failed Part] END) AS FailedPart5
FROM cte 
GROUP BY
    Vehicle_ID, rn

如果您希望行排序如输出中,则只需要添加order通过ware_id,rn最后。

尝试

The issue in your code is really subtle. What you're doing is grouping on Vehicle_ID, problem is that there is more than one row for each Vehicle_ID in the output table. What you should do instead is fixing it by grouping also on the column number rn that you computed in the cte:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY Vehicle_ID, RowNumber
                                 ORDER BY [Failed Part]) rn
    FROM VehicleTable
)
SELECT
    Vehicle_ID,
    MAX(CASE WHEN rn = 1 THEN [Failed Part] END) AS FailedPart1,
    MAX(CASE WHEN rn = 2 THEN [Failed Part] END) AS FailedPart2,
    MAX(CASE WHEN rn = 3 THEN [Failed Part] END) AS FailedPart3,
    MAX(CASE WHEN rn = 4 THEN [Failed Part] END) AS FailedPart4,
    MAX(CASE WHEN rn = 5 THEN [Failed Part] END) AS FailedPart5
FROM cte 
GROUP BY
    Vehicle_ID, rn

If you want your rows ordered as in the output, you just need to add ORDER BY Vehicle_ID, rn at the end.

Try it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文