首页 > 数据库 >SQL——练习:上展BOM

SQL——练习:上展BOM

时间:2023-04-21 13:46:45浏览次数:42  
标签:bd -- 上展 ITEM BOM SQL DATE ID

--练习:元件上展BOM
IF EXISTS
(
    SELECT *
    FROM tempdb.dbo.sysobjects
    WHERE id = OBJECT_ID(N'tempdb.dbo.#temp_bom')
) --是否存在该临时表
    DROP TABLE #temp_bom --存在则删除

CREATE TABLE #temp_bom --创建临时表
(
    ROOT_ITEM_ID UNIQUEIDENTIFIER,   --品号(元件)
    TOTAL_SEQ NVARCHAR(200),         --层级关系
    SEQ INT,                         --序号
    LEV INT,                         --层次
    BOM_ID UNIQUEIDENTIFIER,
    BOM_D_ID UNIQUEIDENTIFIER,
    PARENT_ITEM_ID UNIQUEIDENTIFIER, --父级品号
    ITEM_ID UNIQUEIDENTIFIER         --自身品号
)

DECLARE @ITEM_CODE VARCHAR(20),
        @CurrentLevel AS INT,
        @Level INT,
        @IsBottom INT -- 0-多阶,1-尾阶

SELECT @ITEM_CODE = '130010009',
       @Level = 20,
       @IsBottom = 0

INSERT INTO #temp_bom
(
    ROOT_ITEM_ID,
    TOTAL_SEQ,
    SEQ,
    LEV,
    BOM_ID,
    BOM_D_ID,
    PARENT_ITEM_ID,
    ITEM_ID
)
SELECT bd.SOURCE_ID_ROid AS ROOT_ITEM_ID,
       '0000' AS TOTAL_SEQ,
       (ROW_NUMBER() OVER (PARTITION BY bd.SOURCE_ID_ROid ORDER BY bd.PARENT_ITEM_ID)) AS SEQ,
       1 AS LEV,
       bd.BOM_ID AS BOM_ID,
       bd.BOM_D_ID AS BOM_D_ID,
       bd.PARENT_ITEM_ID AS PARENT_ITEM_ID,
       bd.SOURCE_ID_ROid AS ITEM_ID
FROM dbo.BOM_D AS bd
    INNER JOIN dbo.ITEM AS i
        ON i.ITEM_BUSINESS_ID = bd.SOURCE_ID_ROid
WHERE i.ITEM_CODE = @ITEM_CODE
      AND bd.SOURCE_ID_RTK = 'ITEM'
      AND
      (
          bd.EXPRITY_DATE = '9998-12-31 00:00:00.0000000'
          OR bd.EXPRITY_DATE >= GETDATE()
      )
      AND
      (
          bd.EFFECTIVE_DATE = '1900-01-01 00:00:00.0000000'
          OR bd.EFFECTIVE_DATE <= GETDATE()
      )
ORDER BY bd.SOURCE_ID_ROid,
         bd.PARENT_ITEM_ID


UPDATE tb
SET tb.TOTAL_SEQ = tb.TOTAL_SEQ + '.' + RIGHT('1000' + CAST(tb.SEQ AS VARCHAR(4)), 4)
FROM #temp_bom AS tb
WHERE tb.LEV = 1


SELECT @CurrentLevel = 2

WHILE @CurrentLevel <= @Level
BEGIN

    INSERT INTO #temp_bom
    (
        ROOT_ITEM_ID,
        TOTAL_SEQ,
        SEQ,
        LEV,
        BOM_ID,
        BOM_D_ID,
        PARENT_ITEM_ID,
        ITEM_ID
    )
    SELECT tb.ROOT_ITEM_ID,
           tb.TOTAL_SEQ,
           (ROW_NUMBER() OVER (PARTITION BY tb.ROOT_ITEM_ID,
                                            tb.TOTAL_SEQ
                               ORDER BY tb.PARENT_ITEM_ID,
                                        bd.PARENT_ITEM_ID
                              )
           ),
           @CurrentLevel,
           b.BOM_ID,
           bd.BOM_D_ID,
           bd.PARENT_ITEM_ID,
           bd.SOURCE_ID_ROid
    FROM #temp_bom AS tb
        INNER JOIN dbo.BOM_D AS bd
            ON bd.SOURCE_ID_ROid = tb.PARENT_ITEM_ID
               AND
               (
                   bd.EXPRITY_DATE = '9998-12-31 00:00:00.0000000'
                   OR bd.EXPRITY_DATE >= GETDATE()
               )
               AND
               (
                   bd.EFFECTIVE_DATE = '1900-01-01 00:00:00.0000000'
                   OR bd.EFFECTIVE_DATE <= GETDATE()
               )
        INNER JOIN dbo.BOM AS b
            ON b.BOM_ID = bd.BOM_ID
               AND b.Owner_Org_ROid = 'C8BAE02C-0A60-4FD0-46A9-12140D245E5D'
    WHERE tb.LEV = @CurrentLevel - 1

    IF @@ROWCOUNT = 0
    BEGIN
        BREAK
    END

    UPDATE tb
    SET tb.TOTAL_SEQ = tb.TOTAL_SEQ + '.' + RIGHT('1000' + CAST(tb.SEQ AS VARCHAR(4)), 4)
    FROM #temp_bom AS tb
    WHERE tb.LEV = @CurrentLevel

    SELECT @CurrentLevel += 1
END

IF @IsBottom = 1
BEGIN
    DELETE tb
    FROM #temp_bom AS tb
    WHERE EXISTS
    (
        SELECT 1 FROM #temp_bom AS tb2 WHERE tb2.ITEM_ID = tb.PARENT_ITEM_ID
    )
END



SELECT --tb.BOM_ID,tb.BOM_D_ID,
    --tb.ROOT_ITEM_ID,
    i.ITEM_CODE 元件品号,
    --i.ITEM_NAME,
    tb.TOTAL_SEQ,
    tb.SEQ,
    tb.LEV,
    tb.PARENT_ITEM_ID,
    i2.ITEM_CODE 父品号,
    --i2.ITEM_NAME,
    tb.ITEM_ID,
    i3.ITEM_CODE 品号
--i3.ITEM_NAME
FROM #temp_bom AS tb
    LEFT JOIN dbo.ITEM AS i
        ON i.ITEM_BUSINESS_ID = tb.ROOT_ITEM_ID
    LEFT JOIN dbo.ITEM AS i2
        ON i2.ITEM_BUSINESS_ID = tb.PARENT_ITEM_ID
    LEFT JOIN dbo.ITEM AS i3
        ON i3.ITEM_BUSINESS_ID = tb.ITEM_ID
ORDER BY tb.TOTAL_SEQ

 

 

 

 

标签:bd,--,上展,ITEM,BOM,SQL,DATE,ID
From: https://www.cnblogs.com/xiaoli9627/p/17340054.html

相关文章

  • hiveSQL mapreduce任务调优
    sethive.merge.mapredfiles=true;--在Map-Reduce的任务结束时合并小文件setmapred.max.split.size=30000000;--决定每个map处理的最大的文件大小,单位为B--setmapred.min.split.size=10000000;--公司集群默认值--setmapred.min.split.size.per.node=;......
  • Mysql - Order By 踩坑记录(【string类型】)
    一、记录:在用orderby排序时,发现结果与“逻辑”不符合!!!SELECTchFROM`test`ORDERBYchDESC//降序二、猜想:以为是ASCII的原因?三、验证:数字0到9的ASCII码值分别为48到57,所以排除此原因。四、启发:通过查资料orderbyint,突然发现是数据类型的问题!总结:1、当order......
  • SQL Server 语句笔记
    创建表Createtable表名(列1 char(6))notnull primarykey,列2varchar(50),列2,smallint)修改表Altertable 表名 add 列1char(10)notnull创建索引Createuniqueclustered index编号_INDon产品编号删除索引DropIndex 产品.产......
  • Mysql 5.7 update to 8.0
    获取mysql8.0安装源[root@ganluren-wk~]#wgethttp://repo.mysql.com/mysql80-community-release-el7.rpm安装rpm源[root@ganluren-wk~]#rpm-ivhmysql80-community-release-el7.rpm提示冲突卸载mysql5.7rpm[root@ganluren-wk~]#rpm-emysql57-community-release再次......
  • Centos7 mysql 5.7 安装
    一.卸载原有数据库1.通过命令rpm-qa|grepmysql,rpm-qa|grep mariadb 查看原有数据库[root@ganluren-wketc]#rpm-qa|grepmysql[root@ganluren-wketc]#rpm-qa|grep mariadb2.[root@ganluren-wketc]#rpm-e--nodepsmariadb-libs-5.5.68-1.el7.x86_64卸载......
  • mysql索引--普通索引,唯一索引,主键索引,参照完整性约束,数据完整性约束
    --方法1:createindex--对employee表的员工部门号列创建普通索引depart_ind--createindexdepart_indonemployees(员工部门号);--对employee表的姓名和地址列创建复合索引ad_ind;--createindexad_indonemployees(姓名,地址);--对departments表的部门名称列......
  • C#写一套最全的SQL server帮助类(包括增删改查)
    我定义了一系列静态方法,用于执行SQLServer数据库的增删改查等操作。其中:ExecuteNonQuery方法用于执行指定的SQL语句并返回受影响的行数;ExecuteScalar方法用于执行指定的SQL语句并返回查询结果的第一行第一列;ExecuteDataTable方法用于执行指定的SQL语句并返回一个数据表;ExecuteRea......
  • C#写一套最全的MySQL帮助类(包括增删改查)
    介绍说明:这个帮助类包含了六个主要的方法:ExecuteNonQuery、ExecuteScalar、ExecuteQuery、ExecuteQuery(泛型)、Insert、Update和Delete。其中,ExecuteNonQuery用于执行不返回结果集的SQL语句;ExecuteScalar用于执行一个查询,并返回结果集中第一行的第一列;ExecuteQuery用于执行一个查询......
  • mysql详解必读
    原文地址zhuanlan.zhihu.commysql详解必读残枫cps​目录收起优化mysql1、MySQL中有哪几种锁?2、MySQL中有哪些不同的表格?3、简述在MySQL数据库中MyISAM和InnoDB的区别4、MySQL中InnoDB支持的四种事务隔离级别名称,以及逐级之间的区别?5、CHAR和VARCHAR的区别?6、主键......
  • sql部分语法
    原文地址zhuanlan.zhihu.comsql部分语法残枫cps​目录收起mysqldumpIF表达式IFNULL(expr1,expr2)IFELSE做为流程控制语句使用INNERJOINLEFTJOINRIGHTJOINFULLOUTERJOINcasewhenthenmysqldumpmysqldump用来备份数据库或在不同数据库之间迁移数据,mydqldump的备份内......