0

重要なフィールドがハイフンでマークされた3つのテーブルがあります。すべてのCustomer.MostRecentGiftCardPurchaseAmountをCustomergiftcardpurchase.Amountで更新するにはどうすればよいですか?

Customer
-pk customerID
-Money MostRecentGiftCardPurchaseAmount
Customergiftcardpurchase
-fk customerID
-fk invoiceID
-Money amount
Invoice
-pk invoiceID
-Date invoiceDate
4

2 に答える 2

1

試す:

update Customer set
    MostRecentGiftCardPurchaseAmount = mrp.amount
from
    Customer c
    inner join (
        select lp.customerID, lp.amount
        from
            Customergiftcardpurchase lp
            inner join Invoice li
                on lp.customerID = li.customerID
            inner join (
                select p.customerID, max(i.invoiceDate) as MaxInvoiceDate
                from
                    Customergiftcardpurchase p
                    inner join Invoice i
                        on p.InvoiceId = i.InvoiceId
                group by p.customerID
            ) as md
                on lp.customerID = md.customerID
                and li.invoiceDate = md.MaxInvoiceDate
    ) as mrp
        on c.customerID = mrp.customerID
于 2012-10-16T17:48:35.940 に答える
0

サブクエリを使用します。

 Update c Set 
   MostRecentGiftCardPurchaseAmount =
     (Select Amount 
      From Customergiftcardpurchase p
          Join Invoice I 
             On I.invoiceId = p.invoiceId
                And i.invoiceDate = 
                   (Select Max(invoiceDate)
                    From Customergiftcardpurchase c2 
                       Join Invoice I2 On I2.invoiceId = c2.invoiceId
                    Where customerID = c.customerID)
      Where customerID = c.customerID)
 From Customer c
于 2012-10-16T17:48:25.447 に答える