3个表由不同的字段链接

时间:2016-03-17 21:46:07

标签: sql

我试图写一个SQl语句,我不能完全理解

我写了一个发票系统,我想得到发票的总金额

我有3张桌子

InvoiceHeader,InvoiceDetail和Transactions

两张发票表由一个名为InvoiceRef的字段链接。 事务表通过名为TicketItemID

的字段链接到invoicedetail

我允许用户针对单个发票项目进行付款,因此单个发票上的单个发票项目有多个交易

我想在invoiceHeader表中显示每张发票的总付款金额

以下内容获取发票的总付款

select  sum(Transactions.Amount)
from InvoiceDetail
 join Transactions
on InvoiceDetail.TicketItemID=Transactions.Reference
where InvoiceDetail.InvoiceRef= '000004'

但是,如果我试图将其包含在更大的声明中,那么我会收到错误。这是声明

select InvoiceHeader.InvoiceRef, 
InvoiceHeader.CustomerRef,
 InvoiceHeader.CompanyName,  
 InvoiceHeader.CreatedOn, 
 (SELECT    isnull(SUM(InvoiceDetail.Price * InvoiceDetail.Quantity * (InvoiceDetail.VATRate + 1 )),0) from InvoiceDetail where InvoiceDetail.InvoiceRef=InvoiceHeader.InvoiceRef) AS InvoiceTotal , 
 (SELECT isnull(sum(Amount),0)  from Transactions where Reference = InvoiceHeader.InvoiceRef) as AmountPaid,
  (Select sum(Transactions.Amount) from InvoiceDetail Join(Transactions) on InvoiceDetail.TicketItemID=Transactions.Reference where InvoiceDetail.InvoiceRef=InvoiceHeader.InvoiceRef) as ActualAmount,      
  invoiceheader.paid,       
  InvoiceHeader.Changed,      
   InvoiceHeader.New       
   From InvoiceHeader  

错误是

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'.

2 个答案:

答案 0 :(得分:2)

你应该删除交易表周围的括号,我相信:

select 
    InvoiceHeader.InvoiceRef, 
    InvoiceHeader.CustomerRef,
    InvoiceHeader.CompanyName,  
    InvoiceHeader.CreatedOn, 
    (SELECT    isnull(SUM(InvoiceDetail.Price * InvoiceDetail.Quantity * (InvoiceDetail.VATRate + 1 )),0) 
    from InvoiceDetail where InvoiceDetail.InvoiceRef=InvoiceHeader.InvoiceRef) AS InvoiceTotal , 
    (SELECT isnull(sum(Amount),0)  from Transactions 
    where Reference = InvoiceHeader.InvoiceRef) as AmountPaid,
    (Select sum(Transactions.Amount) from InvoiceDetail Join Transactions 
    on InvoiceDetail.TicketItemID=Transactions.Reference 
    where InvoiceDetail.InvoiceRef=InvoiceHeader.InvoiceRef) as ActualAmount,      
    invoiceheader.paid,       
    InvoiceHeader.Changed,      
    InvoiceHeader.New       
From InvoiceHeader  

答案 1 :(得分:2)

在select子句中使用相关子查询通常是导致性能问题的原因,因此我建议采用不同的方法来实现查询。请注意,您在下面看到的左连接用于模拟现有子查询的效果,很可能您不需要所有这些作为左连接,并且您应尽可能使用内连接,

SELECT
        IH.InvoiceRef
      , IH.CustomerRef
      , IH.CompanyName
      , IH.CreatedOn
      , ISNULL(ID1.InvoiceTotal, 0) AS InvoiceTotal
      , ISNULL(T.AmountPaid, 0)     AS AmountPaid
      , ISNULL(ID2.ActualAmount, 0) AS ActualAmount
      , IH.paid
      , IH.Changed
      , IH.New
FROM InvoiceHeader IH
        LEFT OUTER JOIN
        (
                SELECT
                        InvoiceDetail.InvoiceRef
                      , SUM(InvoiceDetail.Price * InvoiceDetail.Quantity * (InvoiceDetail.VATRate + 1)) AS InvoiceTotal
                FROM InvoiceDetail
                GROUP BY
                        InvoiceDetail.InvoiceRef
        ) ID1 ON ID1.InvoiceRef = IH.InvoiceRef
        LEFT OUTER JOIN
        (
                SELECT
                        InvoiceDetail.InvoiceRef
                      , SUM(Transactions.Amount) AS ActualAmount
                FROM InvoiceDetail
                        INNER JOIN Transactions ON InvoiceDetail.TicketItemID = Transactions.Reference
                GROUP BY
                        InvoiceDetail.InvoiceRef
        ) ID2 ON ID2.InvoiceRef = IH.InvoiceRef
        LEFT OUTER JOIN
        (
                SELECT
                        Reference
                      , SUM(Amount) AS AmountPaid
                FROM Transactions
                GROUP BY
                        Reference
        ) T ON T.Reference = IH.InvoiceRef