除以返回 'Subquery returned more than 1 value.' 错误的子查询
Divide by Subquery returning 'Subquery returned more than 1 value.' error
我有以下table,其中我有两个销售订单,每个销售订单都有不同数量的交易。
SaleOrder
Transaction
Amount
S1
T1
20
S1
T2
20
S2
T1
15
S2
T2
15
S2
T3
15
问题是我在 SQL 服务器 table 中获取每笔交易的总销售订单金额,而实际上它们需要在交易数量之间平均分配,如下所示:
SaleOrder
Transaction
Amount
S1
T1
10
S1
T2
10
S2
T1
5
S2
T2
5
S2
T3
5
我编写了以下查询:
SELECT SalesOrder,
Transaction,
Max(Amount) / (SELECT COUNT(Transaction) AS Transaction FROM Test_Table GROUP BY SalesOrder)
FROM Test_Table
GROUP BY SalesOrder,
Transaction
但它给我 'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >=
or when the subquery is used as an expression.' 错误。有人可以解释这是什么以及如何纠正这个错误吗?
因为在子查询中你 GROUP BY SalesOrder
它 returns 每个 SalesOrder
.
1 行
但是你想要特定SalesOrder
.
的交易数量
使用 MAX() 和 COUNT() window 函数:
SELECT DISTINCT SalesOrder, Transaction,
MAX(Amount) OVER (PARTITION BY SalesOrder, Transaction) /
COUNT(*) OVER (PARTITION BY SalesOrder)
FROM Test_Table
如果列 Amount
的数据类型是 INTEGER 那么 SQL 服务器将执行整数除法,所以可能您需要先乘以 1.0
以便得到正确的结果带小数位的结果:
SELECT DISTINCT SalesOrder, Transaction,
1.0 * MAX(Amount) OVER (PARTITION BY SalesOrder, Transaction) /
COUNT(*) OVER (PARTITION BY SalesOrder)
FROM Test_Table
试试这个
SELECT SaleOrder, [Transaction]
, Max(Amount) / COUNT([Transaction]) over (partition by SaleOrder)
FROM Test_Table GROUP BY SaleOrder,[Transaction]
我认为你不需要 group by
,只是 window 功能:
SELECT SalesOrder, Transaction,
Amount) / COUNT(*) OVER (PARTITION BY salesorder)
FROM Test_Table;
我有以下table,其中我有两个销售订单,每个销售订单都有不同数量的交易。
SaleOrder | Transaction | Amount |
---|---|---|
S1 | T1 | 20 |
S1 | T2 | 20 |
S2 | T1 | 15 |
S2 | T2 | 15 |
S2 | T3 | 15 |
问题是我在 SQL 服务器 table 中获取每笔交易的总销售订单金额,而实际上它们需要在交易数量之间平均分配,如下所示:
SaleOrder | Transaction | Amount |
---|---|---|
S1 | T1 | 10 |
S1 | T2 | 10 |
S2 | T1 | 5 |
S2 | T2 | 5 |
S2 | T3 | 5 |
我编写了以下查询:
SELECT SalesOrder,
Transaction,
Max(Amount) / (SELECT COUNT(Transaction) AS Transaction FROM Test_Table GROUP BY SalesOrder)
FROM Test_Table
GROUP BY SalesOrder,
Transaction
但它给我 'Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >=
or when the subquery is used as an expression.' 错误。有人可以解释这是什么以及如何纠正这个错误吗?
因为在子查询中你 GROUP BY SalesOrder
它 returns 每个 SalesOrder
.
1 行
但是你想要特定SalesOrder
.
的交易数量
使用 MAX() 和 COUNT() window 函数:
SELECT DISTINCT SalesOrder, Transaction,
MAX(Amount) OVER (PARTITION BY SalesOrder, Transaction) /
COUNT(*) OVER (PARTITION BY SalesOrder)
FROM Test_Table
如果列 Amount
的数据类型是 INTEGER 那么 SQL 服务器将执行整数除法,所以可能您需要先乘以 1.0
以便得到正确的结果带小数位的结果:
SELECT DISTINCT SalesOrder, Transaction,
1.0 * MAX(Amount) OVER (PARTITION BY SalesOrder, Transaction) /
COUNT(*) OVER (PARTITION BY SalesOrder)
FROM Test_Table
试试这个
SELECT SaleOrder, [Transaction]
, Max(Amount) / COUNT([Transaction]) over (partition by SaleOrder)
FROM Test_Table GROUP BY SaleOrder,[Transaction]
我认为你不需要 group by
,只是 window 功能:
SELECT SalesOrder, Transaction,
Amount) / COUNT(*) OVER (PARTITION BY salesorder)
FROM Test_Table;