将所有与分区合并

时间:2019-05-11 02:44:31

标签: sql sql-server union window-functions

我有两个独立的查询,我想将它们合并为一个。就他们自己而言,它们工作正常,我尝试使用union,但似乎无法正确完成。基本上,一个查询从某个日期开始就达到平衡,而另一个查询则在某个日期范围内计算活动。我希望结果显示在彼此相邻的列中。

尝试编写不带分区且作为简单分组依据的查询。使用联合,但后来我无法获得第二个查询的列出现在主选择语句上。

Declare @Date datetime = '04/01/2019'

期初余额代码:

    Select Left(Account,4)Entity, right(Account,9)Account, sum(debit+credit)BBal
From GLT_CURRENT__TRANSACTION 
Where Left(Account,4) = '9452'  and 
Right(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062') 
and Accounting_Date <= EOMONTH('04-01-2019',-1)
Group By Left(Account,4), right(Account,9)

活动总和代码

    Select Left(Account,4)Entity,Right(Account,9),Sum(debit+credit)Activity
From GLT_CURRENT__TRANSACTION AS A Where 
Left(Account,4) = '9452' and Accounting_Date >= '04-01-2019' and Accounting_Date <= Eomonth('04-01-2019')
AND Right(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062')
Group By Left(Account,4), Right(Account,9)

如何将两者合而为一?尝试并集,但对第二个查询不起作用。

Entity   Account  BBalance Activity
  9452 1110.0130     50.00    2,500

3 个答案:

答案 0 :(得分:1)

另一种简便的方法是使用CTE。该代码基于上面的初始余额和移动查询。

DECLARE @Date DATETIME = '04/01/2019' --Set the variable for the first day of the period.

;with BeginningBalance
as
( 
    --Gather the beginning or opening balance
    Select 
        Left(Account,4) as Entity, 
        right(Account,9) as Account, 
        sum(debit+credit) as BBal
    From 
        GLT_CURRENT__TRANSACTION 
    Where 
        Left(Account,4) = '9452'  and 
        Right(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062')  and
        Accounting_Date <= EOMONTH(@Date,-1) --All transaction movement up until 31Mar
    Group By 
        Left(Account,4), 
        right(Account,9)
),
Activity --Activity / movement
as
(
    Select 
        Left(Account,4) as Entity,
        Right(Account,9) as Account,
        Sum(debit+credit)Activity
    From 
        GLT_CURRENT__TRANSACTION  
    Where 
        Left(Account,4) = '9452' and 
        Accounting_Date >= @Date and 
        Accounting_Date <= Eomonth(@Date) AND 
        Right(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062')
    Group By 
        Left(Account,4), 
        Right(Account,9)
)
SELECT 
    ISNULL(a.Entity, b.Entity) as [Entity], --Use entity from Beginning or activity just in case the account was made on or after 1APR or not used after 31Mar
    ISNULL(a.Account, b.Account) as [Account], --Use Account from Beginning or activity just in case the account was made on or after 1APR or not used after 31Mar
    ISNULL(BBal,0) as [BBal], --Zero opening if there is no opening balance
    ISNULL(Activity,0) as [Activity] --Zero activity if there is no account movement within the period
FROM 
    BeginningBalance a
FULL OUTER JOIN --Should include all rows from beginning balance & activity. You may want to exclude inactive accounts with zero balances as you're summing up values since the start of your GL
    Activity b
ON 
    a.Entity = b.Entity AND
    a.Account = b.Account

答案 1 :(得分:1)

我认为您过于复杂了

UploadTaskSnapshot

此查询在提供@date(如2018年3月14日)时,将选择2018年3月31日之前的所有交易。对于每一行,SUM中的CASE WHEN会测试会计日期是过去的日期(对于bbal)还是当月的日期(对于活动)。如果特定交易的会计日期不符合CASE WHEN测试返回false的规则,则CASE WHEN的返回值为空,因此不进行汇总。

要了解有关此查询如何工作的更多信息,请在不进行分组/求和的情况下运行它:

SELECT
    LEFT(Account,4) as Entity, 
    RIGHT(Account,9) as Account, 
    SUM(CASE WHEN accounting_date <= EOMONTH(@date, -1) THEN debit+credit END) as BBal,
    SUM(CASE WHEN accounting_date > EOMONTH(@date, -1) THEN debit+credit END) as Activity
FROM
    GLT_CURRENT__TRANSACTION 
WHERE
    Left(Account,4) = '9452'  and 
    Right(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062')  and
    Accounting_Date <= EOMONTH(@Date) 
GROUP BY 
    Left(Account,4), 
    right(Account,9)

3月@date的交易类似:

SELECT
    LEFT(Account,4) as Entity, 
    RIGHT(Account,9) as Account, 
    Accounting_date,
    (CASE WHEN accounting_date <= EOMONTH(@date, -1) THEN debit+credit END) as BBal,
    (CASE WHEN accounting_date > EOMONTH(@date, -1) THEN debit+credit END) as Activity
FROM
    GLT_CURRENT__TRANSACTION 
WHERE
    Left(Account,4) = '9452'  and 
    Right(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062')  and
    Accounting_Date <= EOMONTH(@Date)   

看看如何根据会计日期将交易金额划分为“ bbal”或“ activity”列?现在,当我们将它们求和并进行分组(从查询结果中删除日期列)时:

1, 1, 02-Feb-18, $100, null --previous month
1, 1, 28-Feb-18, $300, null --previous month
1, 1, 02-Mar-18, null, $400 --current month
1, 1, 28-Mar-18, null, $500 --current month

答案 2 :(得分:0)

试试这个-

DECLARE @Date DATETIME = '04/01/2019'

SELECT  
A.Entity, 
A.Account,
SUM(A.BBalance) BBalance,
SUM(A.Activity) Activity
FROM
(
    SELECT LEFT(Account,4)Entity, 
    RIGHT(Account,9)Account,
    CAST(SUM(debit+Credit) OVER ( PARTITION BY Right(Account,9)) AS NUMERIC(8,2)) As BBalance,
    0 Activity
    FROM GLT_CURRENT__TRANSACTION 
    WHERE LEFT(Account,4) = '9452'  
    AND RIGHT(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062') 
    AND Accounting_Date <= EOMONTH(@Date,-1) As D
    GROUP BY Entity, D.Account, BBalance
    --ORDER BY Account

    UNION ALL

    SELECT LEFT(Account,4)Entity,
    RIGHT(Account,9),
    0 BBalance,
    SUM(debit+credit)Activity
    FROM GLT_CURRENT__TRANSACTION 
    WHERE LEFT(Account,4) = '9452' 
    AND Accounting_Date >= @Date 
    AND Accounting_Date <= Eomonth(@Date)
    AND RIGHT(Account,9) IN ('1110.0130','1110.0131','2110.0061','2110.0062')
    Group By LEFT(Account,4), RIGHT(Account,9)
)A
GROUP BY A.Entity,A.Account