当前位置: 首页 > 知识库问答 >
问题:

插入到表中,因为从子查询返回的结果行数以及外部查询结果

诸正谊
2023-03-14

这是表定义,这将是结果的目标表

CREATE TABLE [dbo].[CONTRIBUTION_MARGIN_ACCOUNTING] (
    [ID_CONTRIBUTION_MARGIN_ACCOUNTING] [INT] IDENTITY(1000001, 1) NOT NULL
   ,[ID_CUSTOMER_ACCOUNT_POSITION]      [INT]            NOT NULL
   ,[TYP]                               [CHAR](1)        NULL
   ,[CREDIT_TYP]                        [CHAR](1)        NULL
   ,[BILL_DATE]                         [DATETIME]       NULL
   ,[BILL_NO]                           [NVARCHAR](20)   NULL
   ,[POSITION]                          [SMALLINT]       NULL
   ,[CUSTOMER_NO]                       [NVARCHAR](255)  NULL
   ,[CHARGE]                            [NVARCHAR](20)   NULL
   ,[Amount]                            [DECIMAL](18, 6) NULL
   ,CONSTRAINT [PK_CONTRIBUTION_MARGIN_ACCOUNTING] PRIMARY KEY CLUSTERED (
        [ID_CONTRIBUTION_MARGIN_ACCOUNTING] ASC
    ) WITH (PAD_INDEX = OFF
           ,STATISTICS_NORECOMPUTE = OFF
           ,IGNORE_DUP_KEY = OFF
           ,ALLOW_ROW_LOCKS = ON
           ,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
GO

ALTER TABLE [dbo].[CONTRIBUTION_MARGIN_ACCOUNTING] WITH CHECK
    ADD CONSTRAINT [CustomerAccountPosition_CONTRIBUTION_MARGIN_ACCOUNTING]
    FOREIGN KEY([ID_CUSTOMER_ACCOUNT_POSITION])
    REFERENCES [dbo].[CUSTOMER_ACCOUNT_POSITION]([ID_CUSTOMER_ACCOUNT_POSITION])
    ON DELETE CASCADE
GO

要求是:在表中插入许多新记录CONTRIBUTION_MARGIN_ACCOUNTING在子查询中找到ID_CUSTOMER ACCOUNT_POSITION金额的唯一组合

以下查询给出错误,因为< code >子查询通常会返回多行

SELECT krp.ID_CUSTOMER_ACCOUNT_POSITION
      ,kr.RECEIPT_TYP AS TYP
      ,krp.CALCULATION_TYP AS CREDIT_TYP
      ,kr.BILL_DATE AS BILL_DATE
      ,kr.NO AS BILL_NO
      ,krp.POSITION AS POSITION
      ,kun.NO AS CUSTOMER_NO
      ,(CASE WHEN krp.ID_DELIVERY_POSITION IS NOT NULL
        THEN (SELECT mge.CHARGE_NO AS chr
                    ,SUM(loadingPackage.AMOUNT) AS Amount
              FROM dbo.LOADING_PACKAGE AS loadingPackage
              INNER JOIN dbo.MATERIAL_COMPONENT AS mbe ON loadingPackage.ID_MATERIAL_COMPONENT = mbe.ID_MATERIAL_COMPONENT
              INNER JOIN dbo.MATERIAL_PACK AS mge ON mbe.ID_MATERIAL_COMPONENT = mge.ID_MATERIAL_COMPONENT
              WHERE loadingPackage.ID_DELIVERY_POSITION = DELIVERYPosition.ID_DELIVERY_POSITION
              GROUP BY mge.CHARGE_NO
                      ,loadingPackage.ID_CUSTOMER_ACCOUNT_POSITION
              )
             WHEN krp.ID_DELIVERY_POSITION IS NULL
             THEN (SELECT mge.CHARGE_NO AS ch
                         ,SUM(loadingPackage.AMOUNT) AS Amount
                   FROM dbo.LOADING_PACKAGE AS loadingPackage
                   INNER JOIN dbo.MATERIAL_COMPONENT AS mbe ON loadingPackage.ID_MATERIAL_COMPONENT = mbe.ID_MATERIAL_COMPONENT
                   INNER JOIN dbo.MATERIAL_PACK AS mge ON mbe.ID_MATERIAL_COMPONENT = mge.ID_MATERIAL_COMPONENT
                   WHERE loadingPackage.ID_CUSTOMER_ACCOUNT_POSITION = krp.ID_CUSTOMER_ACCOUNT_POSITION
                   GROUP BY mge.CHARGE_NO
                           ,loadingPackage.ID_CUSTOMER_ACCOUNT_POSITION
             )
            ELSE
                NULL
            END) AS CHARGE
FROM dbo.CUSTOMER ACCOUNT_POSITION AS krp
INNER JOIN dbo.CUSTOMER ACCOUNT AS kr ON kr.ID_CUSTOMER_ACCOUNT = krp.ID_CUSTOMER ACCOUNT
LEFT OUTER JOIN dbo.AMOUNT AS deck ON krp.ID_CUSTOMER_ACCOUNT_POSITION = deck.ID_CUSTOMER_ACCOUNT_POSITION
LEFT OUTER JOIN dbo.DELIVERY_POSITION AS DELIVERYPosition ON krp.ID_DELIVERY_POSITION = DELIVERYPosition.ID_DELIVERY_POSITION
LEFT OUTER JOIN dbo.CUSTOMER AS kun ON kun.ID_CUSTOMER = kr.ID_CUSTOMER
WHERE deck.ID_CUSTOMER_ACCOUNT_POSITION IS NULL
AND kr.STATUS = 'A' ;

不确定如何实现,例如:如果某个< code > ID _ CUSTOMER < code > ACCOUNT _ POSITION 有2个< code >金额,我们应该在表< code > CONTRIBUTION _ MARGIN _ ACCOUNTING 中创建2个具有相同< code > ID _ CUSTOMER < code > ACCOUNT _ POSITION 但金额不同的记录。

抽样资料

共有1个答案

李新霁
2023-03-14

因此,将这些子查询放入frompart,加入'em:

SELECT ...
      ,ISNULL(charge_dlvr.CHARGE_NO, charge_pkg.CHARGE_NO) AS CHARGE_NO
      ,ISNULL(charge_dlvr.Amount, charge_pkg.Amount) AS Amount
FROM dbo.CUSTOMER ACCOUNT_POSITION AS krp
...
OUTER APPLY
(SELECT mge.CHARGE_NO
                    ,SUM(loadingPackage.AMOUNT) AS Amount
              FROM dbo.LOADING_PACKAGE AS loadingPackage
              INNER JOIN dbo.MATERIAL_COMPONENT AS mbe ON loadingPackage.ID_MATERIAL_COMPONENT = mbe.ID_MATERIAL_COMPONENT
              INNER JOIN dbo.MATERIAL_PACK AS mge ON mbe.ID_MATERIAL_COMPONENT = mge.ID_MATERIAL_COMPONENT
              WHERE loadingPackage.ID_DELIVERY_POSITION = DELIVERYPosition.ID_DELIVERY_POSITION
              GROUP BY mge.CHARGE_NO
                      ,loadingPackage.ID_CUSTOMER_ACCOUNT_POSITION
) charge_dlvr
OUTER APPLY
(SELECT mge.CHARGE_NO
                         ,SUM(loadingPackage.AMOUNT) AS Amount
                   FROM dbo.LOADING_PACKAGE AS loadingPackage
                   INNER JOIN dbo.MATERIAL_COMPONENT AS mbe ON loadingPackage.ID_MATERIAL_COMPONENT = mbe.ID_MATERIAL_COMPONENT
                   INNER JOIN dbo.MATERIAL_PACK AS mge ON mbe.ID_MATERIAL_COMPONENT = mge.ID_MATERIAL_COMPONENT
                   WHERE loadingPackage.ID_CUSTOMER_ACCOUNT_POSITION = krp.ID_CUSTOMER_ACCOUNT_POSITION
                     AND krp.ID_DELIVERY_POSITION IS NULL
                   GROUP BY mge.CHARGE_NO
                           ,loadingPackage.ID_CUSTOMER_ACCOUNT_POSITION
) charge_pkg
WHERE deck.ID_CUSTOMER_ACCOUNT_POSITION IS NULL
 类似资料:
  • 问题内容: 我有以下从数据库获取十六进制代码的函数 我的问题是我在回调函数中返回了结果,但getColour函数未返回任何内容。我希望getColour函数返回的值。 在我调用getColour的那一刻,它不返回任何内容 我尝试做类似的事情 但当然SELECT查询在返回值时已经完成 问题答案: 您只需要对回调中的db查询结果进行处理。就像。

  • 问题内容: 有关表格的概述如下: 我有一个表,可以称之为联接,它有两列,都是其他表的外键。让我们将两列称为userid和buildingid,以便join看起来像 我基本上需要在此表中插入一堆行。通过在此表中具有多个条目,将为每个用户分配多个建筑物。因此,可以通过以下方式将用户13分配到建筑物1、2和3 我试图弄清楚如果建筑物号是恒定的,该如何在查询中执行此操作,也就是说,我正在将一群人分配给同一

  • 我正在玩mongob,并将一些测试数据{name:"david"}输入到“用户”集合中。我通过键入mongoshell验证了数据在MongoDB中 结果: 在 node.js 脚本中,使用以下代码: 不返回任何结果 我没有发现任何错误,也没有错误。请告知

  • Firebase查询返回此查询的值 该项由model类捕获 但是,返回null

  • 问题内容: 我有一个报告,显示客户订购的产品及其价格: 我想插入一行来汇总每个公司的订单,如下所示: 这是一些代码,显示了我所拥有的查询的基本结构: 有谁知道如何做到这一点?我在Transact-SQL(Microsoft SQL Server)中编写此代码。 问题答案: 感谢大家的反馈/帮助,至少让我想到了不同的方法。我想出了一些与我使用的SQL Server版本无关的东西(我们的供应商经常更改

  • 问题内容: 我有一个动态查询,执行时会给出带有很多列的结果。我想将来自此动态查询的结果插入到临时表中。我这样做是因为我想对临时表执行一些过滤并获得所需的结果。 在该线程中先创建一个临时表,然后使用插入数据。 由于列的列表太长,我想避免此步骤,而且我不知道字段的数据类型。 错误信息 关键字“ exec”附近的语法不正确。 这该怎么做 ?是否可以通过这种方式完成?如果不是,请指定其他替代方法以将执行动