自定义字段的递归结果

时间:2018-04-22 20:15:56

标签: tsql sql-server-2008-r2

enter image description here enter image description here我需要将每个级别设置为计算字段中设置的总和。这些可以是积极的,也可以是消极的。

我已经包含了我的表结构的简化版本和一些示例数据以及我想要生成的内容。

CREATE TABLE ACCOUNT_TEST([ID] [int] IDENTITY(1,1) NOT NULL, CODE VARCHAR(12), AMMOUNT MONEY)

    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(200, 3)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(205, 2)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(300, 20)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(400, 4)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(410, 4)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(420, 4)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(450, 3)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(700, 25)
    INSERT INTO ACCOUNT_TEST(CODE,AMMOUNT) VALUES(710, 30)

    CREATE TABLE [dbo].[BALANCE_TEST]([ID] [int] IDENTITY(1,1) NOT NULL,
        [POSITION] [int] NOT NULL,
        [TITLE] [nvarchar](200) NULL,
        [AMMOUNT] [money] NULL,
        [IDENTIFIER] [nvarchar](10) NULL,
        [CALCULATION] [nvarchar](255) NULL,
        [ACCOUNTS] nvarchar(300));

    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(1,'  A)',0,'A','AI',null)
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(2,'     I.',0,'AI','AI1+AI2',null)
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(3,'                1.',5,'AI1',NULL,'200,205')
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(4,'                2.',20,'AI2',NULL,'300')
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(5,'  B)',0,'B','BI+BII',null)
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(6,'     I.',15,'BI',NULL,'400,410,420,450')
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(7,'     II.',0,'BII','BII1+BII2',null)
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(8,'                1.',25,'BII1',NULL,'700')
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(9,'                2.',30,'BII2',NULL,'710')
    INSERT INTO BALANCE_TEST(POSITION,TITLE,AMMOUNT,IDENTIFIER,CALCULATION,ACCOUNTS) VALUES(10,'  TOTAL (A - B)',0,'TOTAL_AB','A-B',null)

结果 我手动更新,以便您可以看到预期的结果。

    UPDATE BALANCE_TEST SET AMMOUNT = (SELECT SUM(AMMOUNT) FROM BALANCE_TEST WHERE ID IN(3,4)) WHERE ID = 2
    UPDATE BALANCE_TEST SET AMMOUNT = (SELECT SUM(AMMOUNT) FROM BALANCE_TEST WHERE ID IN(2)) WHERE ID = 1
    UPDATE BALANCE_TEST SET AMMOUNT = (SELECT SUM(AMMOUNT) FROM BALANCE_TEST WHERE ID IN(8,9)) WHERE ID = 7
    UPDATE BALANCE_TEST SET AMMOUNT = (SELECT SUM(AMMOUNT) FROM BALANCE_TEST WHERE ID IN(6,7)) WHERE ID = 5
    UPDATE BALANCE_TEST SET AMMOUNT = (SELECT SUM(AMMOUNT) FROM BALANCE_TEST WHERE ID IN(2)) WHERE ID = 1
    UPDATE BALANCE_TEST SET AMMOUNT = (SELECT AMMOUNT FROM BALANCE_TEST WHERE ID = 1) - (SELECT AMMOUNT FROM BALANCE_TEST WHERE ID = 5) WHERE ID = 10

谢谢。

1 个答案:

答案 0 :(得分:0)

Juanma,Thx用于将DDL与样本数据一起添加。现在问题好多了......

但是:你想要实现的目标更接近Excel之类的东西,可能就像expression tree一样。

SQL-Server是执行此操作的错误工具:可以使用电锯切割面包,但结果不会很有说服力。

您可以使用SQL-Server为底层元数据和要计算的数据建模,但是您应该使用适当的编程语言来解释和计算它。

一些重大缺陷:

  • 从不存储逗号分隔值(例如200,205正在破坏1.NF)。这必须存储在带有外键的相关表中。
  • AI1+AI2相同:必须存储在term1,term2,operator结构中。
  • Don使用money,而不是decimal(10,4)
  • 不要使用类似章节的数字和缩进来构建层次结构。
    • 而是使用自引用表(阅读HIERARCHYID)来构建infint层次结构或
    • 一组可连接的表,子表和子子表(如果存在最大深度)
  • 尝试理解表达式树的内部结构,以便在此之后构建表。
祝你好运!这是 - 至少我理解 - 不是微不足道的......