group-by和coalesce不能合并,报错?

Group-by and coalesce cannot be combined, gives an error?

我正在创建一个 SSRS 报告,并在对其进行数据查询时遇到了这个错误。我确实需要通过命令与一组连接的一行中的数据集(月初至今和年初至今值)。 This is the crystal report I'm referring to and converting to SSRS.

我得到的错误是这样的:Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

注意:我不能使用 2 个子查询,例如一个用于 MTD,一个用于 YTD 值,因为值与行描述不匹配。这就是我尝试将某些 coalesce 函数与单独的分组命令一起使用的全部原因。

我尝试过的:

SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
SELECT coalesce((Select ((SUM( "IS_vwSalesbyProductLineComp"."sls_amt")-SUM( "IS_vwSalesbyProductLineComp"."cost_amt"))/SUM( "IS_vwSalesbyProductLineComp"."sls_amt"))*100 ),0) AS G1MTD,
       "IS_vwSalesbyProductLineComp"."Mainslspsn_name",
       "IS_vwSalesbyProductLineComp"."PCSummarized",
       coalesce ((SELECT SUM("IS_vwSalesbyProductLineComp"."qty_to_ship"-"IS_vwSalesbyProductLineComp"."qty_return_to_stk")FROM "100"."dbo"."IS_vwSalesbyProductLineComp"
   where Year = 2021
   and Month between 1 and 12
   Group by Mainslspsn_name,PCSummarized),0) AS Qty_Sold,
       coalesce ((SELECT SUM("IS_vwSalesbyProductLineComp"."sls_amt")FROM "100"."dbo"."IS_vwSalesbyProductLineComp"
   where Year = 2021
   and Month between 1 and 12
   Group by Mainslspsn_name,PCSummarized),0) AS YTD_Sales,
       coalesce ((SELECT SUM("IS_vwSalesbyProductLineComp"."cost_amt")FROM "100"."dbo"."IS_vwSalesbyProductLineComp"
   where Year = 2021
   and Month between 1 and 12
   Group by Mainslspsn_name,PCSummarized),0) AS YTD_COGS,
       coalesce((Select ((SUM( "IS_vwSalesbyProductLineComp"."sls_amt")-SUM( "IS_vwSalesbyProductLineComp"."cost_amt"))/SUM( "IS_vwSalesbyProductLineComp"."sls_amt"))*100 
       FROM "100"."dbo"."IS_vwSalesbyProductLineComp"
   where Year = 2021
   and Month between 1 and 12
   Group by Mainslspsn_name,PCSummarized),0) AS G2YTD,
        SUM("IS_vwSalesbyProductLineComp"."qty_to_ship"-"IS_vwSalesbyProductLineComp"."qty_return_to_stk") 
        AS Qty_Sold,
        SUM("IS_vwSalesbyProductLineComp"."sls_amt") 
        AS MTD_Sales,
        SUM("IS_vwSalesbyProductLineComp"."cost_amt") 
        AS MTD_COGS   
   FROM "100"."dbo"."IS_vwSalesbyProductLineComp"
   where Year = 2021
   and Month = 12
   Group by Mainslspsn_name,PCSummarized
   order by PCSummarized

问题出在我觉得合并后的各个分组命令,因为如果没有这些命令,查询就可以工作,但只有 returns 所有行的总和值相同。 Reference

这里有一个示例数据集供参考:

CREATE TABLE IS_vwSalesbyProductLineComp
    ([Mainslspsn_no](int)
      ,[Mainslspsn_name](char(64))
      ,[PCSummarized](char(75))
      ,[Year](int)
      ,[Month](int)
      ,[MonthLit](varchar(3))
      ,[PCSummary](varchar(24))
      ,[slspsn_no](int)
      ,[fullname](char(64))
      ,[cus_no](char(20))
      ,[cus_name](char(50))
      ,[inv_dt](datetime)
      ,[sls_amt](decimal(16,2))
      ,[cost_amt](decimal(16,2))
      ,[item_no](char(30))
      ,[item_desc_1](char(30))
      ,[item_desc_2](char(30))
      ,[prod_cat](char(3))
      ,[prod_cat_desc](char(15))
      ,[qty_ordered](decimal(13,4))
      ,[qty_to_ship](decimal(13,4))
      ,[qty_return_to_stk](decimal(13,4))
      ,[unit_price](decimal(16,6))
      ,[unit_cost](decimal(16,6))
      ,[state](char(3))
      ,[city](varchar(100))
      ,[zip](varchar(20))
      ,[cus_type_cd](char(5))
      ,[loc](char(3)))
;

INSERT INTO IS_vwSalesbyProductLineComp
    ([Mainslspsn_no]
      ,[Mainslspsn_name]
      ,[PCSummarized]
      ,[Year]
      ,[Month]
      ,[MonthLit]
      ,[PCSummary]
      ,[slspsn_no]
      ,[fullname]
      ,[cus_no]
      ,[cus_name]
      ,[inv_dt]
      ,[sls_amt]
      ,[cost_amt]
      ,[item_no]
      ,[item_desc_1]
      ,[item_desc_2]
      ,[prod_cat]
      ,[prod_cat_desc]
      ,[qty_ordered]
      ,[qty_to_ship]
      ,[qty_return_to_stk]
      ,[unit_price]
      ,[unit_cost]
      ,[state]
      ,[city]
      ,[zip]
      ,[cus_type_cd]
      ,[loc])
VALUES
    (849, 'Paul Dean','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 849, 'Paul Dean', '123456','ABC PRODUCTS','2022-03-16 00:00:00.000',357.60,288.35,'01583220126','126 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',80.0000,80.0000,0.0000,4.470000,3.604391,'NC','SHELBY','28152','ASII8','FL'),
    (849, 'Paul Dean','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 849, 'Paul Dean', '123456','ABC PRODUCTS','2022-03-16 00:00:00.000',357.60,310.57,'01583220142','142 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',80.0000,80.0000,0.0000,4.470000,3.882166,'NC','SHELBY','28152','ASII8','FL'),
    (849, 'Paul Dean','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 849, 'Paul Dean', '122334','XYZ PRODUCTS','2022-03-16 00:00:00.000',56.76,33.47,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (849, 'Paul Dean','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 849, 'Paul Dean', '122334','XYZ PRODUCTS','2022-03-16 00:00:00.000',56.76,74.83,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (849, 'Paul Dean','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 849, 'Paul Dean', '122334','XYZ PRODUCTS','2022-03-16 00:00:00.000',189.20,127.29,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (201, 'John Snider','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 201, 'John Snider', '107974','CDE SUPPLIES','2022-03-16 00:00:00.000',205.92,120.87,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (201, 'John Snider','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 201, 'John Snider', '107974','CDE SUPPLIES','2022-03-16 00:00:00.000',977.60,570.48,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (201, 'John Snider','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 201, 'John Snider', '107974','CDE SUPPLIES','2022-03-16 00:00:00.000',146.64,86.41,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (201, 'John Snider','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 201, 'John Snider', '107974','CDE SUPPLIES','2022-03-16 00:00:00.000',293.28,180.30,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (201, 'John Snider','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 201, 'John Snider', '107974','CDE SUPPLIES','2022-03-16 00:00:00.000',293.28,186.11,'01583220098','98 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (207, 'House Accounts','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 207, 'House Accounts', '107974','SUPERIOR PRODUCTS','2022-03-16 00:00:00.000',365.60,215.32,'01583220065','65 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (207, 'House Accounts','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 207, 'House Accounts', '107974','SUPERIOR PRODUCTS','2022-03-16 00:00:00.000',365.60,233.08,'0158422008014','80-1/4 X 5/8 X 022 4T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (207, 'House Accounts','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 207, 'House Accounts', '107974','SUPERIOR PRODUCTS','2022-03-16 00:00:00.000',1506.00,1099.40,'01583220124','124 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (207, 'House Accounts','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 207, 'House Accounts', '107974','SUPERIOR PRODUCTS','2022-03-16 00:00:00.000',502.00,375.76,'01583220126','126 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL'),
    (207, 'House Accounts','Band Saw Blades', 2022,3, 'Mar', 'Bandsaw Blades', 207, 'House Accounts', '107974','SUPERIOR PRODUCTS','2022-03-16 00:00:00.000',9036.00,6988.46,'0158322012913','129-1/3 X 5/8 X 022 3T PREMIUM',,106,'BLD-5/8 X 022',24.0000,24.0000,0.0000,4.780000,3.117917,'FL','BOYNTON BEACH','33472','CII18','FL')
;

解决方案是将 UNION 与“归零”行一起使用并添加聚合:

WITH 
T1 AS
(
SELECT (
       SELECT((SUM(IS_vwSalesbyProductLineComp.sls_amt) - SUM(IS_vwSalesbyProductLineComp.cost_amt)) / SUM(IS_vwSalesbyProductLineComp.sls_amt)) * 100) AS G1MTD, 
       IS_vwSalesbyProductLineComp.Mainslspsn_name, 
       IS_vwSalesbyProductLineComp.PCSummarized, 
       (
       SELECT SUM(IS_vwSalesbyProductLineComp.qty_to_ship - IS_vwSalesbyProductLineComp.qty_return_to_stk)
       FROM "100".dbo.IS_vwSalesbyProductLineComp
       WHERE Year = 2021
             AND Month BETWEEN 1 AND 12
       GROUP BY Mainslspsn_name, 
                PCSummarized) AS Qty_Sold, 
       (
       SELECT SUM(IS_vwSalesbyProductLineComp.sls_amt)
       FROM "100".dbo.IS_vwSalesbyProductLineComp
       WHERE Year = 2021
             AND Month BETWEEN 1 AND 12
       GROUP BY Mainslspsn_name, 
                PCSummarized) AS YTD_Sales, 
      (
       SELECT SUM(IS_vwSalesbyProductLineComp.cost_amt)
       FROM "100".dbo.IS_vwSalesbyProductLineComp
       WHERE Year = 2021
             AND Month BETWEEN 1 AND 12
       GROUP BY Mainslspsn_name, 
                PCSummarized) AS YTD_COGS, 
       (
       SELECT((SUM(IS_vwSalesbyProductLineComp.sls_amt) - SUM(IS_vwSalesbyProductLineComp.cost_amt)) / SUM(IS_vwSalesbyProductLineComp.sls_amt)) * 100
       FROM "100".dbo.IS_vwSalesbyProductLineComp
       WHERE Year = 2021
             AND Month BETWEEN 1 AND 12
       GROUP BY Mainslspsn_name, 
                PCSummarized) AS G2YTD, 
       SUM(IS_vwSalesbyProductLineComp.qty_to_ship - IS_vwSalesbyProductLineComp.qty_return_to_stk) AS Qty_Sold2, --> renamed because ambigous
       SUM(IS_vwSalesbyProductLineComp.sls_amt) AS MTD_Sales, 
       SUM(IS_vwSalesbyProductLineComp.cost_amt) AS MTD_COGS
FROM "100".dbo.IS_vwSalesbyProductLineComp
WHERE Year = 2021
      AND Month = 12
GROUP BY Mainslspsn_name, 
         PCSummarized
),
T2 AS 
(
SELECT 0 AS C1, '' AS C2, '' AS C3, 0 AS C4, 0 AS C5, 0 AS C6, 0 AS C7, 0 AS C8, 0 AS C9, 0 AS C10
),
T3 AS
(
SELECT * FROM T1
UNION ALL
SELECT * FROM T2
)
SELECT SUM(G1MTD) AS G1MTD, 
       STRING_AGG(Mainslspsn_name, '') AS Mainslspsn_name,
       STRING_AGG(PCSummarized, '') AS PCSummarized,
       SUM(Qty_Sold) AS Qty_Sold,
       SUM(YTD_Sales) AS YTD_Sales,
       SUM(YTD_COGS) AS YTD_COGS,
       SUM(G2YTD) AS G2YTD,
       SUM(Qty_Sold2) AS Qty_Sold2,
       SUM(MTD_Sales) AS MTD_Sales,
       SUM(MTD_COGS) AS MTD_COGS
FROM T3
ORDER BY PCSummarized;