下面的查询用于从数据库中检索报告中显示的记录。但是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
结果:
有谁知道问题和解决方案是什么,请提供建议和指导。 提前致谢
答案 0 :(得分: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