0

すべての顧客の注文を表示し、SQLを使用して列を追加して、顧客が支払うべき合計金額を表示できるクエリを作成しようとしています。

次のコードは正しいデータを取得します。

SELECT
    cu.FName + '  ' + cu.SName as 'Name', 
    cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
    cu.PostCode, 
    co.DateOrdered, co.DateDispatched, 
    ma.MaterialName, 
    it.Quantity, 
    ma.Price as 'Total' 
FROM
    Customers cu 
LEFT JOIN 
    CustomerOrder co ON co.CustomerID = cu.CustomerID 
LEFT JOIN
    ItemOrder it ON it.OrderID = co.OrderID 
LEFT JOIN 
    Materials ma ON ma.MaterialID = it.MaterialID 

上記にコードを追加して、顧客が支払うべき合計金額を表示する列を追加する必要がありますが、行き詰まっています。誰か助けてもらえますか?

Date Order   |    Dispatch Date | Item  | QTY  |   Price | TOTAL AMOUNT FOR BOTH ROWS
2012-10-30   |    2012-11-25    | Bath  | 1    |   49.99 |  
2012-10-30   |    2012-11-25    | Sink  | 1    |   55.99 |
4

2 に答える 2

0

これを試して:

Select 
  cu.FName + '  ' + cu.SName as 'Name', 
  cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
  cu.PostCode, 
  co.DateOrdered, co.DateDispatched, 
  ma.MaterialName, 
  it.Quantity, 
  ma.Price as 'Total' ,
  (it.Quantity * ma.Price) 'Owe amount'
from Customers cu 
left join CustomerOrder co on co.CustomerID = cu.CustomerID 
left join ItemOrder it on it.OrderID = co.OrderID 
left join Materials ma on ma.MaterialID = it.MaterialID 
于 2012-11-07T12:19:01.250 に答える
0
SELECT
    cu.FName + '  ' + cu.SName as 'Name', 
    cu.Address1 + ', ' + cu.Address2 + ', ' + cu.Address3 as 'Dispatch Address', 
    cu.PostCode, 
    co.DateOrdered, co.DateDispatched, 
    ma.MaterialName, 
    it.Quantity, 
    ma.Price as 'Total',
    sum(it.Quantity * ma.Price) over (partition by cu.CustomerID) as TotalOwed
from Customers cu 
left join CustomerOrder co on co.CustomerID = cu.CustomerID 
left join ItemOrder it on it.OrderID = co.OrderID 
left join Materials ma on ma.MaterialID = it.MaterialID 
于 2012-11-07T12:22:08.390 に答える