SQL Server Recursive query - Can't get order correct based on display order column

eyh26e7m  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(103)

I have a bill of material table (MBOM) and I am trying to write a recursive query. The parts are to be displayed under their respective parent in order defined by displayorder. I can't figure out how to get this in the correct order. The query I am using is

WITH tree ( id, parentid, level, displayorder, uom, quantity ) AS (
    SELECT
        id,
        parentid,
        0 as level,
        displayorder,
        uom,
        quantity
    FROM
        MBOM
    WHERE
        parentid = 47

    UNION ALL

    SELECT
        c2.id,
        c2.parentid,
        tree.level + 1,
        c2.displayorder,
        c2.uom,
        c2.quantity
    FROM
        MBOM c2
        INNER JOIN tree ON tree.id = c2.parentid
)
SELECT
    *
FROM
    tree;

This is my raw data.

parentidlevelidquantityuomdisplayorder
470177.601188410
47068.333488420
47019166530
47055166540
470102166550
470103166560
470104166570
47058166580
551416066510
5514666520
55145466540
5512466550
5511466560
551115166570
551118166580
551142166590
5511191665100
5511251665110
5513591665130
5511642665140
5511551665150
5511601665160
551671665170
551571665180
5511231665190
5511061665120
106260.908288410
106292166530
1062131366540
1062372666520
1313130166510
131312266520
67266166510
6725666520
1602114166510
16025366520
1642163166510
164212166520
359260.142688410
359241266520
359271166530
3592141166540
1413140166510
141312266520
71370166510
7135566520
125260.000188410
125293166530
1252161266540
1252374466520
1613162166510
161312266520
119260.000188410
119265166530
1192129166540
1192121166550
1192374466520
121312266520
1213120166510
1293128166510
129312266520
142242166510
1422144166520
1422146166530
1422148166540
1422150166550
1503405166520
1503149166510
1483405166520
1483147166510
1463405266520
1463145166510
1443405266520
1443143166510
118260.026588410
118241266520
1182117166530
118281166540
115260.063988410
115241366520
1152116166530
1152111166540

This is my desired result

parentidlevelidquantityuomdisplayorder
04716650
471177.601188410
47168.333488420
47119166530
47155166540
552416066510
5524666520
55245466540
5522466550
5521466560
552115166570
115360.063988410
115341366520
1153116166530
1153111166540
552118166580
118360.026588410
118341266520
1183117166530
118381166540
552142166590
142342166510
1423144166520
1444143166510
1444405266520
1423146166530
1464145166510
1464405266520
1423148166540
1484147166510
1484405166520
1423150166550
1504149166510
1504405166520
5521191665100
119360.000188410
1193374466520
119365166530
1193129166540
1294128166510
129412266520
1193121166550
1214120166510
121412266520
5521251665110
125360.000188410
1253374466520
125393166530
1253161266540
1614162166510
161412266520
5521061665120
106360.908288410
1063372666520
106392166530
1063131366540
1314130166510
131412266520
5523591665130
359360.142688410
359341266520
359371166530
71470166510
7145566520
3593141166540
1414140166510
141412266520
5521642665140
1643163166510
164312166520
5521551665150
5521601665160
1603114166510
16035366520
552671665170
67366166510
6735666520
552571665180
5521231665190
471102166550
471103166560
471104166570
47158166580
cx6n0qe3

cx6n0qe31#

SQL will not guarantee any order without an ORDER BY , so you might want to add one:

WITH tree ( id, parentid, level, displayorder, uom, quantity ) AS (
    SELECT
        id,
        parentid,
        0 as level,
        displayorder,
        uom,
        quantity
    FROM
        MBOM
    WHERE
        parentid = 47

    UNION ALL

    SELECT
        c2.id,
        c2.parentid,
        tree.level + 1,
        c2.displayorder,
        c2.uom,
        c2.quantity
    FROM
        MBOM c2
        INNER JOIN tree ON tree.id = c2.parentid
)
SELECT
    *
FROM
    tree
ORDER BY level, displayorder

相关问题