在另一列中使用子查询列

时间:2015-05-06 08:03:50

标签: sql sql-server

我有这样的查询。我想使用PaidAmount列来计算另一列。有可能吗?

SELECT 
    B.SubProjectCode, 
    A.StatementBreakDown, 
    A.Description, 
    A.ApplicableInvoices, 
    A.AgreedAmount, 
    A.SettlePercentage, 
    (A.AgreedAmount * (A.SettlePercentage/100)) DueToSettle,
    (SELECT SUM(C.PaymentAmount) FROM VendorSOAPayments C WHERE C.VendorSOAID = A.VendorSOAID ) PaidAmount, 
    (DueToSettle - PaidAmount ) BalanceAmt
FROM VendorSOA A
INNER JOIN SubProject B 
    ON A.SubProjectID = B.SubProjectID

4 个答案:

答案 0 :(得分:2)

使用公用表表达式(CTE),它不会损害性能。

;WITH CTE as
(
  SELECT 
    B.SubProjectCode, 
    A.StatementBreakDown, 
    A.Description, 
    A.ApplicableInvoices, 
    A.AgreedAmount, 
    A.SettlePercentage, 
    (A.AgreedAmount * (A.SettlePercentage/100)) DueToSettle,
    (SELECT SUM(C.PaymentAmount) FROM VendorSOAPayments C WHERE C.VendorSOAID = A.VendorSOAID ) PaidAmount, 
    (DueToSettle - PaidAmount ) BalanceAmt
  FROM VendorSOA A
  INNER JOIN SubProject B 
  ON A.SubProjectID = B.SubProjectID
)
SELECT
   SubProjectCode, 
   StatementBreakDown, 
   Description, 
   ApplicableInvoices, 
   AgreedAmount, 
   SettlePercentage, 
   DueToSettle,
   PaidAmount, 
   (DueToSettle - PaidAmount ) BalanceAmt
 FROM CTE

答案 1 :(得分:1)

试试这个

  Select *,(DueToSettle - PaidAmount ) BalanceAmt 
   from    
    (SELECT 
        B.SubProjectCode, 
        A.StatementBreakDown, 
        A.Description, 
        A.ApplicableInvoices, 
        A.AgreedAmount, 
        A.SettlePercentage, 
        (A.AgreedAmount * (A.SettlePercentage/100)) DueToSettle,
        (SELECT SUM(C.PaymentAmount) FROM VendorSOAPayments C WHERE C.VendorSOAID = A.VendorSOAID ) PaidAmount
      FROM VendorSOA A
      INNER JOIN SubProject B 
      ON A.SubProjectID = B.SubProjectID) as Main

答案 2 :(得分:0)

您可以尝试使用"使用"制作临时表/视图子句并使用该表中的列来计算您的另一列

with temp as 
(SELECT 
    B.SubProjectCode, 
    A.StatementBreakDown, 
    A.Description, 
    A.ApplicableInvoices, 
    A.AgreedAmount, 
    A.SettlePercentage, 
    (A.AgreedAmount * (A.SettlePercentage/100)) DueToSettle,
    (SELECT SUM(C.PaymentAmount) FROM VendorSOAPayments C WHERE C.VendorSOAID = A.VendorSOAID ) PaidAmount,     
FROM VendorSOA A
INNER JOIN SubProject B 
    ON A.SubProjectID = B.SubProjectID)
select 
    SubProjectCode, 
    StatementBreakDown, 
    Description, 
    ApplicableInvoices, 
    AgreedAmount, 
    SettlePercentage,
    DueToSettle,
    PaidAmount,
    (DueToSettle - PaidAmount ) as BalanceAmt
from temp;

答案 3 :(得分:0)

您也可以使用APPLY

CROSS APPLY的作用类似于INNER JOIN,即必须从子查询返回结果,否则将丢弃整行。 DueToSettle值总是排成一排,所以在这里使用它是安全的。

OUTER APPLY就像LEFT OUTER JOIN一样,这是我在第二个查询中从单独的表中选择使用的内容:

SELECT 
    B.SubProjectCode, 
    A.StatementBreakDown, 
    A.Description, 
    A.ApplicableInvoices, 
    A.AgreedAmount, 
    A.SettlePercentage, 
    DueToSettle.SettleAmount,
    Payments.PaidAmount, 
    (DueToSettle.SettleAmount - Payments.PaidAmount ) BalanceAmt
FROM VendorSOA A
INNER JOIN SubProject B 
    ON A.SubProjectID = B.SubProjectID
CROSS APPLY(VALUES(A.AgreedAmount * (A.SettlePercentage/100))) DueToSettle(SettleAmount)
OUTER APPLY 
(
  SELECT 
    SUM(C.PaymentAmount) 
  FROM VendorSOAPayments C 
  WHERE C.VendorSOAID = A.VendorSOAID
) Payments(PaidAmount);