好的,我有一份月份订单清单,这样就很容易了。
SELECT COUNT(*) AS ITEMS,
下一部分易于:
COUNT(DISTINCT(PICKSET_NO))AS PICKSETS,
下一部分我无法解决:
SUM(ITEMS/PICKSETS) AS AVGPICKSETSIZE
FROM dbo.orders
感谢您的帮助。这是一个块中的代码。
SELECT
COUNT(*) AS ITEMS,
COUNT(DISTINCT(PICKSET_NO))AS PICKSETS,
SUM(ITEMS/PICKSETS)
FROM dbo.CollationOrders
GO
答案 0 :(得分:2)
重复表达式:
SELECT COUNT(*) AS ITEMS,
COUNT(DISTINCT PICKSET_NO) AS PICKSETS,
COUNT(*) / (1.0 * COUNT(DISTINCT PICKSET_NO))
FROM dbo.CollationOrders;
您无法在同一select
中重复使用列别名。
1.0是为了防止整数除法。
答案 1 :(得分:0)
您可以这样做:
SELECT ITEMS,
PICKSETS,
ITEMS / (1.0 * PICKSETS)
FROM (
SELECT COUNT(*) AS ITEMS,
COUNT(DISTINCT PICKSET_NO) AS PICKSETS
FROM TableName)t
答案 2 :(得分:0)
我最终使用了:
SELECT
COUNT(*) AS ITEMS,
COUNT(DISTINCT om.PicksetNo) AS PICKSETS ,
FORMAT(COUNT(*) / (1.0 * COUNT(DISTINCT om.PicksetNo)),'N2'),
ca.YWK AS YWK
FROM CHDS_Common.dbo.OMOrder om
INNER JOIN CHDS_Management.dbo.Calendar ca ON om.EarliestPickDate = ca.DT
GROUP BY ca.YWK