Foxtable(狐表)用户栏目专家坐堂 → CTE中如何避免重复值


  共有3768人关注过本帖树形打印复制链接

主题:CTE中如何避免重复值

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/4/4 17:54:00 [显示全部帖子]

WITH CTE
    AS (SELECT a.产品编码,
            CAST(NULL AS NVARCHAR(20)) AS 父件编码,
            CAST(1 AS FLOAT) AS 用量,
            CAST(0 AS FLOAT) AS 损耗,
            0 AS Levle,
            CAST(产品编码 AS VARCHAR(MAX)) AS path
        FROM (SELECT DISTINCT 产品编码 FROM MPS01) a
        UNION ALL
        SELECT BOM.产品编码,
            BOM.父件编码,
            BOM.用量,
            BOM.损耗,
            c.Levle + 1 AS Levle,
            CAST(c.path + '\' + BOM.产品编码 AS VARCHAR(MAX)) AS path
        FROM CTE AS c
            INNER JOIN BOM
                ON c.产品编码 = BOM.父件编码
       )

select * from (SELECT *,ROW_NUMBER() over(partition by 产品编码,父件编码 order by levle desc) as rowNum from cte) as a where rowNum = 1


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/4/4 20:51:00 [显示全部帖子]

无法做到你说的去重复。


 回到顶部