1

下面的查询用于从数据库中检索记录以显示在报告中。但是 SENOKO 和 EUNOS 列在每一行中显示重复的总数。这似乎子查询未连接到主查询导致未进行过滤。

SELECT whbal.customer, customer.imp_license_no, customer.psq_level, 
   (SELECT SUM(CONVERT(DECIMAL(8,3),(CONVERT(DECIMAL(8, 3), whbal.qty_good) + CONVERT(DECIMAL(8, 3), whbal.qty_slack)) * CONVERT(DECIMAL(8, 3), whbal.std_weight) / 1000)) 
    FROM whbal WHERE warehouse='SKW') AS SENOKO
   (SELECT SUM(CONVERT(DECIMAL(8,3),(CONVERT(DECIMAL(8, 3), whbal.qty_good) + CONVERT(DECIMAL(8, 3), whbal.qty_slack)) * CONVERT(DECIMAL(8, 3), whbal.std_weight) / 1000)) 
    FROM whbal WHERE warehouse='EKW') AS EUNOS
FROM customer 
INNER JOIN whbal ON whbal.customer=customer.customer AND whbal.date_create<=@date1                   
INNER JOIN stktype ON whbal.stock_type=stktype.stock_type
WHERE whbal.customer BETWEEN @cust1 AND @cust2 AND whbal.stock_type=@type
GROUP BY whbal.customer, customer.customer, customer.imp_license_no, customer.psq_level

结果:

在此处输入图像描述

有谁知道是什么问题和解决方法,请指教和指导。提前致谢

4

1 回答 1

1

这可能是你想要的:

SELECT whbal.customer, customer.imp_license_no, customer.psq_level, 
    SUM(CASE WHEN warehouse='SKW' 
        THEN CONVERT(DECIMAL(8, 3), whbal.qty_good) 
           + CONVERT(DECIMAL(8, 3), whbal.qty_slack) 
         * CONVERT(DECIMAL(8, 3), whbal.std_weight)
        ELSE 0.0 END
       / 1000.0) AS SENOKO,
    SUM(CASE WHEN warehouse='EKW' 
        THEN CONVERT(DECIMAL(8, 3), whbal.qty_good) 
           + CONVERT(DECIMAL(8, 3), whbal.qty_slack) 
         * CONVERT(DECIMAL(8, 3), whbal.std_weight)
        ELSE 0.0 END
       / 1000.0) AS EUNOS
FROM customer 
INNER JOIN whbal ON whbal.customer=customer.customer AND whbal.date_create<=@date1                   
INNER JOIN stktype ON whbal.stock_type=stktype.stock_type
WHERE whbal.customer BETWEEN @cust1 AND @cust2 AND whbal.stock_type=@type
GROUP BY whbal.customer, customer.customer, customer.imp_license_no, customer.psq_level
于 2013-11-04T02:16:44.980 回答