2013-10-31 133 views
1

我已经做了一个查询,我想添加一个“SENOKO”列,通过使用数据库中的字段来计算总和。如果我没有通过SENOKO进行分组,则重复的结果将显示在报告中。 为了避免重复记录,我当时就想,集团通过圣诺哥,但错误如下所示起来:无效的列名称“SENOKO”(SQL查询)

Invalid column name "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 [email protected] 
GROUP BY customer.customer, customer.imp_license_no, customer.psq_level, whbal.stock_type, SENOKO 

任何人都可以请帮助?

回答

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 [email protected] 
) as subquery 
GROUP BY subquery.customer, subquery.imp_license_no, subquery.psq_level, subquery.stock_type, Subquery.SENOKO 
+0

出了另一个错误: **不能使用聚合或子查询中使用了由群列表BY子句中的表达式** – user2901955

+0

请帮助。 。赞赏 – user2901955

+0

@ user2901955:看到我编辑的答案。 –