联合所有与分区

Union ALL with partition

我有两个单独的查询,我想将它们合并为一个。他们自己工作正常,我尝试使用 union,但我似乎无法正确使用。基本上,一个查询在某个日期获得余额,另一个查询计算一系列日期的 activity。我希望结果出现在彼此相邻的列中。

尝试编写不带分区的查询并将其作为简单的分组依据。使用 union 但我无法让第二个查询的列出现在主 select 语句中。

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)

activity

总和的代码
    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

试试这个-

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

另一种简单的方法是使用 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

我认为你是 over-complicating 东西

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)

此查询在给定 @date(如 2018 年 3 月 14 日)时,会选择 2018 年 3 月 31 日之前的所有交易。对于每一行,SUM 中的 CASE WHEN 测试会计日期是过去的(对于 bbal)还是当前的月份(对于 activity)。如果特定交易的会计日期不符合规则 CASE WHEN test returns false) 则 CASE WHEN 中的 return 值为空,因此不会求和

要了解有关此查询如何工作的更多信息,运行 它没有 grouping/summing:

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)   

3 月@date 的交易情况如下:

1, 1, 02-Feb-18, 0, null --previous month
1, 1, 28-Feb-18, 0, null --previous month
1, 1, 02-Mar-18, null, 0 --current month
1, 1, 28-Mar-18, null, 0 --current month

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

1, 1, 0, 0  --the 0 is £100+0, the 0 is 0+0