0

以下のORDER BY句が原因でエラーが発生しています。ORDER BY節で TotalPrice 列を参照するにはどうすればよいですか:

SELECT * FROM
(

SELECT O.OrderID,
       (SELECT SUM(SubTotal) FROM DB_OrderDetails OD WHERE OD.OrderID = O.OrderID) AS TotalPrice,
       ROW_NUMBER() OVER (ORDER BY TotalPrice) AS RowNum

    FROM DB_Orders O

) Orders
4

2 に答える 2

4

同じサブクエリで名前で参照することはできません。外側のクエリでそれを行う必要があります。

SELECT orders.*, 
       ROW_NUMBER() OVER (ORDER BY TotalPrice) AS RowNum FROM
(
    SELECT O.OrderID,
       (SELECT SUM(SubTotal) FROM DB_OrderDetails OD 
        WHERE OD.OrderID = O.OrderID) AS TotalPrice
    FROM DB_Orders O
) Orders
于 2012-08-24T14:35:03.083 に答える
1

これはどう:

select 
    o.orderID,
    sum(od.SubTotal) as TotalPrice,
    row_number() over (order by sum(od.SubTotal)) as RowNum
from DB_Orders o
join DB_OrderDetails od
    on o.OrderID = od.OrderID
group by o.OrderID

以下に例を示します: SQL Fiddle

于 2012-08-24T14:47:39.167 に答える