1

我做了一个查询,我想添加一个“SENOKO”列,通过使用数据库中的字段来计算总和。如果我没有按 SENOKO 分组,那么重复的结果将显示在报告中。为了避免重复记录,我想通过 SENOKO 分组,但错误显示如下:

列名“SENOKO”无效

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

有人可以帮忙吗?

4

1 回答 1

1

SENOKO是派生列,所以不能直接在GROUP BY子句中使用。

用这个:

select * from (
SELECT customer.customer, customer.imp_license_no, customer.psq_level, whbal.stock_type, (SELECT SUM((CONVERT(DECIMAL(9, 3), qty_good) + CONVERT(DECIMAL(9, 3), qty_slack)) * CONVERT(DECIMAL(9, 3), std_weight) / 1000) FROM whbal WHERE warehouse='SKW')AS SENOKO 
FROM customer 
INNER JOIN whbal ON customer.customer=whbal.customer 
INNER JOIN stktype ON whbal.stock_type=stktype.stock_type 
WHERE customer.customer BETWEEN @cust1 AND @cust2 AND whbal.stock_type=@type 
) as subquery
GROUP BY subquery.customer, subquery.imp_license_no, subquery.psq_level, subquery.stock_type, Subquery.SENOKO
于 2013-10-31T02:24:26.267 回答