【问题标题】:Why a union all queries are repeating the data?为什么联合所有查询都在重复数据?
【发布时间】:2021-02-20 09:43:03
【问题描述】:

我有这个过程,它为不同的分类帐类型返回这些列,但对于每种类型,它都会重复数据,我试图弄清楚但不能。

我遍历了每一行和每个联合,但它仍然重复每种类型的数据。我已经删除了一些其他问题,但这个问题让我很头疼。

ALTER procedure [dbo].[SP_Rpt_TrailBalance] --2,8
    @Company int = null,
    @YTD    date = null
As
Begin

    declare @FromDate date
    declare @ToDate date
    declare @CurrentYear int
    declare @PreviousMonth int
    declare @CurrentMonth int
    declare @PreviousYear int
    declare @Period int

    set @CurrentYear = year(@YTD) 

set @FromDate =    CAST('01/01/' + CAST( @CurrentYear as varchar(4)) as Date)
set @ToDate = @YTD  

--If @Period = 1
--Begin
--  set @StartingMonth = 1
--End

--Else

--Begin
--set @StartingMonth = month(getdate())
--End

    --set @ToDate = month(getdate())
    set @PreviousMonth = month(getdate()) - 1
    --set @CurrentYear = year(getdate())
    set @PreviousYear = year(getdate()) - 1

    Select
    comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText as 'ShortText', g.BusinessArea,
    (
    --(select isnull(sum(g1.Amount),0) from GLedger g1 where g1.DC = 'Debit' and month(vi.PostingDate) = 12 and year(vi.PostingDate) = @PreviousYear and g1.GLAccountNo = g.GLAccountNo and g1.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g2.Amount),0) from GLedger g2 where g2.DC = 'Credit' and month(vi.PostingDate) = 12 and year(vi.PostingDate) = @PreviousYear and g2.GLAccountNo = g.GLAccountNo and g2.BusinessArea = g.BusinessArea)
    0
    ) as 'C/fwd balance',
    (
    case when @Period = 1 then
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(vi.PostingDate) between @FromDate and @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(vi.PostingDate) between @FromDate and @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    else
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(vi.PostingDate) = @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(vi.PostingDate) = @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    end
    ) as 'Previous Month',
    isnull((select sum(dr.Amount) from GLedger dr where dr.DC = 'Debit' and dr.GLAccountNo = g.GLAccountNo and dr.BusinessArea = g.BusinessArea and vi.PostingDate between @FromDate and @ToDate),0) as 'Reporting Debit',
    isnull((select abs(sum(cr.Amount)) from GLedger cr where cr.DC = 'Credit' and cr.GLAccountNo = g.GLAccountNo and cr.BusinessArea = g.BusinessArea and vi.PostingDate between @FromDate and @ToDate),0) as 'Reporting Credit'
    from GLedger g --, COA c, Company p
    Inner Join Company comp
    ON comp.CompanyID= g.CompanyID
    Inner join COA c
    ON c.GLAccount= g.GLAccountNo
    Inner Join VendorInvoice vi 
    ON vi.VendorInvoice_ID= g.MainID and g.LedgerType_ID= 1
    where 
    g.GLAccountNo = c.GLAccount and
    comp.CompanyID = @Company and                   --VendorInvoices
    --month(vi.PostingDate) between @FromDate and @ToDate
    --and year(vi.PostingDate) = @CurrentYear
    (vi.PostingDate) between @FromDate and @ToDate
    and isnull(g.Active,0) = 1
    group by comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText, g.BusinessArea, vi.PostingDate
    --order by g.GLAccountNo

    UNION ALL 


    Select
    comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText as 'ShortText', g.BusinessArea,
    (
    --(select isnull(sum(g1.Amount),0) from GLedger g1 where g1.DC = 'Debit' and month(vi.PostingDate) = 12 and year(vi.PostingDate) = @PreviousYear and g1.GLAccountNo = g.GLAccountNo and g1.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g2.Amount),0) from GLedger g2 where g2.DC = 'Credit' and month(vi.PostingDate) = 12 and year(vi.PostingDate) = @PreviousYear and g2.GLAccountNo = g.GLAccountNo and g2.BusinessArea = g.BusinessArea)
    0
    ) as 'C/fwd balance',
    (
    case when @Period = 1 then
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(vi.PostingDate) between @FromDate and @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(vi.PostingDate) between @FromDate and @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    else
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(vi.PostingDate) = @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(vi.PostingDate) = @PreviousMonth and year(vi.PostingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    end
    ) as 'Previous Month',
    isnull((select sum(dr.Amount) from GLedger dr where dr.DC = 'Debit' and dr.GLAccountNo = g.GLAccountNo and dr.BusinessArea = g.BusinessArea and  vi.PostingDate between @FromDate and @ToDate),0) as 'Reporting Debit',
    isnull((select abs(sum(cr.Amount)) from GLedger cr where cr.DC = 'Credit' and cr.GLAccountNo = g.GLAccountNo and cr.BusinessArea = g.BusinessArea and vi.PostingDate between @FromDate and @ToDate),0) as 'Reporting Credit'
    from GLedger g --, COA c, Company p
    Inner Join Company comp
    ON comp.CompanyID= g.CompanyID
    Inner join COA c
    ON c.GLAccount= g.GLAccountNo
    Inner Join VendorInvoice VI
    ON VI.VendorInvoice_ID= g.MainID
    Inner Join VendorInvoiceTransactions VIT
    ON G.SubID = VIT.VendorTransactionsID and G.LedgerType_ID = 2   --VendorInvoiceTransactions
    where 
    g.GLAccountNo = c.GLAccount and
    comp.CompanyID = @Company and                   
    --month(vi.PostingDate) between @FromDate and @ToDate
    --and year(vi.PostingDate) = @CurrentYear
    (vi.PostingDate) between @FromDate and @ToDate
    and isnull(g.Active,0) = 1
    group by comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText, g.BusinessArea, vi.PostingDate

    UNION ALL 

    Select
    comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText as 'ShortText', g.BusinessArea,
    (
    --(select isnull(sum(g1.Amount),0) from GLedger g1 where g1.DC = 'Debit' and month(s.BillingDate) = 12 and year(s.BillingDate) = @PreviousYear and g1.GLAccountNo = g.GLAccountNo and g1.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g2.Amount),0) from GLedger g2 where g2.DC = 'Credit' and month(s.BillingDate) = 12 and year(s.BillingDate) = @PreviousYear and g2.GLAccountNo = g.GLAccountNo and g2.BusinessArea = g.BusinessArea)
    0
    ) as 'C/fwd balance',
    (
    case when @Period = 1 then
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(s.BillingDate) between @FromDate and @PreviousMonth and year(s.BillingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(s.BillingDate) between @FromDate and @PreviousMonth and year(s.BillingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea) 
    0
    else
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(s.BillingDate) = @PreviousMonth and year(s.BillingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(s.BillingDate) = @PreviousMonth and year(s.BillingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    end
    ) as 'Previous Month',
    isnull((select sum(dr.Amount) from GLedger dr where dr.DC = 'Debit' and dr.GLAccountNo = g.GLAccountNo and dr.BusinessArea = g.BusinessArea and s.BillingDate between @FromDate and @ToDate),0) as 'Reporting Debit',
    isnull((select abs(sum(cr.Amount)) from GLedger cr where cr.DC = 'Credit' and cr.GLAccountNo = g.GLAccountNo and cr.BusinessArea = g.BusinessArea and s.BillingDate between @FromDate and @ToDate),0) as 'Reporting Credit'
    from GLedger g --, COA c, Company p
    Inner Join Company comp
    ON comp.CompanyID= g.CompanyID
    Inner join COA c
    ON c.GLAccount= g.GLAccountNo
    Inner Join Sales S 
    ON G.MainID = S.SalesID and G.LedgerType_ID = 3   --Sales
    where 
    g.GLAccountNo = c.GLAccount and
    comp.CompanyID = @Company and                   
    --month(s.BillingDate) between @FromDate and @ToDate
    --and year(s.BillingDate) = @CurrentYear
    (s.BillingDate) between @FromDate and @ToDate
    and isnull(g.Active,0) = 1
    group by comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText, g.BusinessArea, s.BillingDate

    UNION ALL 

    Select
    comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText as 'ShortText', g.BusinessArea,
    (
    --(select isnull(sum(g1.Amount),0) from GLedger g1 where g1.DC = 'Debit' and month(ST.PricingDate) = 12 and year(g1.Datetime) = @PreviousYear and g1.GLAccountNo = g.GLAccountNo and g1.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g2.Amount),0) from GLedger g2 where g2.DC = 'Credit' and month(ST.PricingDate) = 12 and year(g2.Datetime) = @PreviousYear and g2.GLAccountNo = g.GLAccountNo and g2.BusinessArea = g.BusinessArea)
    0
    ) as 'C/fwd balance',
    (
    case when @Period = 1 then
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(ST.PricingDate) between @FromDate and @PreviousMonth and year(ST.PricingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(ST.PricingDate) between @FromDate and @PreviousMonth and year(ST.PricingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    else
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(ST.PricingDate) = @PreviousMonth and year(ST.PricingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(ST.PricingDate) = @PreviousMonth and year(ST.PricingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    end
    ) as 'Previous Month',
    isnull((select sum(dr.Amount) from GLedger dr where dr.DC = 'Debit' and dr.GLAccountNo = g.GLAccountNo and dr.BusinessArea = g.BusinessArea and ST.PricingDate between @FromDate and @ToDate),0) as 'Reporting Debit',
    isnull((select abs(sum(cr.Amount)) from GLedger cr where cr.DC = 'Credit' and cr.GLAccountNo = g.GLAccountNo and cr.BusinessArea = g.BusinessArea and ST.PricingDate between @FromDate and @ToDate),0) as 'Reporting Credit'
    from GLedger g --, COA c, Company p
    Inner Join Company comp
    ON comp.CompanyID= g.CompanyID
    Inner join COA c
    ON c.GLAccount= g.GLAccountNo
    Inner Join SalesTransactions ST 
    ON G.SubID = ST.SalesTransactionID and G.LedgerType_ID = 4  --SalesTransactions
    where 
    g.GLAccountNo = c.GLAccount and
    comp.CompanyID = @Company and                   
    --month(ST.PricingDate) between @FromDate and @ToDate
    --and year(ST.PricingDate) = @CurrentYear
    (ST.PricingDate) between @FromDate and @ToDate
    and isnull(g.Active,0) = 1
    group by comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText, g.BusinessArea, ST.PricingDate

    UNION ALL 

    Select
    comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText as 'ShortText', g.BusinessArea,
    (
    --(select isnull(sum(g1.Amount),0) from GLedger g1 where g1.DC = 'Debit' and month(AP.PostingDate) = 12 and year(AP.PostingDate) = @PreviousYear and g1.GLAccountNo = g.GLAccountNo and g1.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g2.Amount),0) from GLedger g2 where g2.DC = 'Credit' and month(AP.PostingDate) = 12 and year(AP.PostingDate) = @PreviousYear and g2.GLAccountNo = g.GLAccountNo and g2.BusinessArea = g.BusinessArea)
    0
    ) as 'C/fwd balance',
    (
    case when @Period = 1 then
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(AP.PostingDate) between @FromDate and @PreviousMonth and year(AP.PostingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(AP.PostingDate) between @FromDate and @PreviousMonth and year(AP.PostingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    else
    --(select isnull(sum(g3.Amount),0) from GLedger g3 where g3.DC = 'Debit' and month(AP.PostingDate) = @PreviousMonth and year(AP.PostingDate) = @CurrentYear and g3.GLAccountNo = g.GLAccountNo and g3.BusinessArea = g.BusinessArea) +
    --(select isnull(sum(g4.Amount),0) from GLedger g4 where g4.DC = 'Credit' and month(AP.PostingDate) = @PreviousMonth and year(AP.PostingDate) = @CurrentYear and g4.GLAccountNo = g.GLAccountNo and g4.BusinessArea = g.BusinessArea)
    0
    end
    ) as 'Previous Month',
    isnull((select sum(dr.Amount) from GLedger dr where dr.DC = 'Debit' and dr.GLAccountNo = g.GLAccountNo and dr.BusinessArea = g.BusinessArea and AP.PostingDate between @FromDate and @ToDate ),0) as 'Reporting Debit',
    isnull((select abs(sum(cr.Amount)) from GLedger cr where cr.DC = 'Credit' and cr.GLAccountNo = g.GLAccountNo and cr.BusinessArea = g.BusinessArea and AP.PostingDate between @FromDate and @ToDate),0) as 'Reporting Credit'
    from GLedger g --, COA c, Company p
    Inner Join Company comp
    ON comp.CompanyID= g.CompanyID
    Inner join COA c
    ON c.GLAccount= g.GLAccountNo
    Inner Join  tblAccountsInvoicePayable AP 
    ON G.MainID = AP.PayableInvoice_ID and G.LedgerType_ID = 5  --AccountsInvoicePayable
    where 
    g.GLAccountNo = c.GLAccount and
    comp.CompanyID = @Company and                   
    --month(AP.PostingDate) between @FromDate and @ToDate
    --and year(AP.PostingDate) = @CurrentYear
    (AP.PostingDate) between @FromDate and @ToDate
    and isnull(g.Active,0) = 1
    group by comp.CompanyCode, g.Currency, g.GLAccountNo, c.GLAccountLongText, g.BusinessArea, AP.PostingDate
end

【问题讨论】:

    标签: sql sql-server tsql stored-procedures


    【解决方案1】:

    所以请记住,UNION 命令会结合两个或多个 SELECT 语句的结果,但只保留不同的值。

    E.g.
    table1 --> 1,2,3,4,5
    table2 --> 1,2,4,5,6,7
    
    ```
    SELECT c FROM table1
    UNION
    SELECT c FROM table2; 
    ```
    
    Result --> 1,2,3,4,5,6,7
    

    另一方面,UNION ALL 命令合并两个或多个 SELECT 语句的结果并允许重复。

    E.g.
    table1 --> 1,2,3,4,5
    table2 --> 1,2,4,5,6,7
    
    ```
    SELECT c FROM table1
    UNION ALL
    SELECT c FROM table2; 
    ```
    
    Result --> 1,1,2,2,3,4,4,5,5,6,7
    

    READ HERE

    EXAMPLES HERE

    【讨论】:

      【解决方案2】:

      尝试使用UNION 而不是UNION ALL。在网上简单搜索一下就会发现以下区别:

      UNION ALL 保留每个原始数据集中的所有记录,UNION 删除所有重复记录。

      【讨论】:

        【解决方案3】:

        如果您不想重复结果,请使用 UNION 而不是 UNION ALL。

        UNION ALL 保留每个原始数据集中的所有记录,UNION 消除重复行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-22
          相关资源
          最近更新 更多