从不同的表Sql server

时间:2017-09-25 17:41:48

标签: sql sql-server tsql sql-server-2012

我有几个存储金额的表,我想分组并得到总和 - 多表的原因是nhibernate descriminators。 我正在使用Union all并且工作但是查询非常大。

我正在使用以下查询

     SELECT CustomerAccountNumber,
       vc.CustomerName,
       SUM(PermAmount) AS PermAmount,
       SUM(FreetextAmount) AS FreetextAmount,
       (SUM(PermAmount) + SUM(FreetextAmount)) AS TotalAmountByCustomer
FROM
(
    SELECT pp.CustomerAccountNumber,
           pl.Amount AS PermAmount,
           0 AS FreetextAmount
    FROM dbo.PermanentPlacementTransactionLine pl
         INNER JOIN dbo.TransactionLine tl ON pl.TransactionLineId = tl.Id
         INNER JOIN dbo.PermanentPlacement pp ON pl.PermanentPlacementId = pp.Id
    WHERE tl.CurrentStatus = 1
    GROUP BY pp.CustomerAccountNumber,
             pl.Amount,
             tl.Id
    UNION ALL
    SELECT ft.CustomerAccountNumber,
           0 AS PermAmount,
           ft.Amount AS FreetextAmount
    FROM dbo.FreeTextTransactionLine fttl
         INNER JOIN dbo.TransactionLine tl ON fttl.TransactionLineId = tl.Id
         INNER JOIN dbo.[FreeText] ft ON fttl.FreeTextId = ft.Id
    WHERE tl.CurrentStatus = 1
    GROUP BY ft.CustomerAccountNumber,
             ft.Amount,
             tl.Id
) WIPSummary
INNER JOIN dbo.vw_Customer vc ON WIPSummary.CustomerAccountNumber = vc.CustomerAccount
GROUP BY CustomerAccountNumber,
         vc.CustomerName;

有没有在单独的列中显示金额的优雅方式? 我可以使用分区,如果它是同一个表,并希望逐行显示。

1 个答案:

答案 0 :(得分:1)

尝试这些查询,易于理解,可能比您的查询更快。

我认为您的视图中的值是唯一的

WITH cte_a 
     AS (SELECT pp.customeraccountnumber 
                ,Sum(pl.amount) AS PermAmount 
                ,0              AS FreetextAmount 
         FROM   dbo.permanentplacementtransactionline pl 
                INNER JOIN dbo.transactionline tl 
                        ON pl.transactionlineid = tl.id 
                INNER JOIN dbo.permanentplacement pp 
                        ON pl.permanentplacementid = pp.id 
         WHERE  tl.currentstatus = 1 
         GROUP  BY pp.customeraccountnumber), 
     cte_b 
     AS (SELECT ft.customeraccountnumber 
                ,0              AS PermAmount 
                ,Sum(ft.amount) AS FreetextAmount 
         FROM   dbo.freetexttransactionline fttl 
                INNER JOIN dbo.transactionline tl 
                        ON fttl.transactionlineid = tl.id 
                INNER JOIN dbo.[freetext] ft 
                        ON fttl.freetextid = ft.id 
         WHERE  tl.currentstatus = 1 
         GROUP  BY ft.customeraccountnumber) 
SELECT vc.customeraccountnumber 
       ,vc.customername 
       ,Isnull(A.permamount, 0)       AS PermAmount 
       ,Isnull(B.freetextamount, 0)   AS FreetextAmount 
       ,Isnull(A.permamount, 0) 
        + Isnull(B.freetextamount, 0) AS TotalAmountByCustomer 
FROM   dbo.vw_customer vc 
       LEFT JOIN cte_a a 
              ON vc.customeraccount = A.customeraccountnumber 
       LEFT JOIN cte_b b 
              ON vc.customeraccount = A.customeraccountnumber 

如果没有表格结构和样本数据,那就是我能帮到你做的最好的事情。