Sql 按月份、年份和科目对截至今日月份的所有月份的值求和,并显示0';s值,其中特定行的数据不存在

Sql 按月份、年份和科目对截至今日月份的所有月份的值求和,并显示0';s值,其中特定行的数据不存在,sql,sql-server,tsql,Sql,Sql Server,Tsql,我无法按要求显示我的数据。我尝试过使用递归CTE表(至少就我所知),我尝试过交叉应用和外部应用,但只有这样才能重复所有日期的值,并产生重复的值。我需要按经理、账户和会计年度(07/01/xxxx-6/30/xxxx)的月份汇总总计 数据需要如下所示,即为所有账户创建一行,如果该月不存在,则将总计归零: +-------------+---------------+-----------+--------------+----------+------------------+ | Manager

我无法按要求显示我的数据。我尝试过使用递归CTE表(至少就我所知),我尝试过交叉应用和外部应用,但只有这样才能重复所有日期的值,并产生重复的值。我需要按经理、账户和会计年度(07/01/xxxx-6/30/xxxx)的月份汇总总计

数据需要如下所示,即为所有账户创建一行,如果该月不存在,则将总计归零:

+-------------+---------------+-----------+--------------+----------+------------------+
| ManagerName |  DebtorName   | YearMonth | YearMonthDay |   TTV    | CommissionAmount |
+-------------+---------------+-----------+--------------+----------+------------------+
| Person 1    | Account Alpha | 2019-07   | 7/1/2019     | 11930.31 | 996.34           |
| Person 1    | Account Alpha | 2019-08   | 8/1/2019     | 83835.74 | 6833.35          |
| Person 1    | Account Alpha | 2019-09   | 9/1/2019     | 0.00     | 0.00             |
| Person 1    | Account Alpha | 2019-10   | 10/1/2019    | 0.00     | 0.00             |
| Person 1    | Account Alpha | 2019-11   | 11/1/2019    | 0.00     | 0.00             |
| Person 1    | Account Beta  | 2019-07   | 7/1/2019     | 188      | 15.04            |
| Person 1    | Account Beta  | 2019-08   | 8/1/2019     | 8662.2   | 769.18           |
| Person 1    | Account Beta  | 2019-09   | 9/1/2019     | 8497.73  | 781.5            |
| Person 1    | Account Beta  | 2019-10   | 10/1/2019    | 8497.73  | 781.5            |
| Person 1    | Account Beta  | 2019-11   | 11/1/2019    | 0.00     | 0.00             |
| Person 2    | Account Gamma | 2019-07   | 7/1/2019     | 1478.38  | 143.73           |
| Person 2    | Account Gamma | 2019-08   | 8/1/2019     | 0.00     | 0.00             |
| Person 2    | Account Gamma | 2019-09   | 9/1/2019     | 0.00     | 0.00             |
| Person 2    | Account Gamma | 2019-10   | 10/1/2019    | 0.00     | 0.00             |
| Person 2    | Account Gamma | 2019-11   | 11/1/2019    | 0.00     | 0.00             |
+-------------+---------------+-----------+--------------+----------+------------------+
有时行在所有月份都有值,有时则没有。但是我还没有找到一个好的方法来正确地显示这一点

下面是一个示例,我尝试使用交叉应用的递归cte,但它复制了数月的值。例如,对于7月份的AccountAlpha,它给了我一行实际值和一行0。我不能仅仅抓住那一列的最大值,因为有时候人们会而且可能会有负值#PreFinal只是保存account by month和Manager的实际值的表

SELECT ManagerName, DebtorName, YearMonth, YearMonthDay
    ,SUM(TTV) AS TTV
    ,SUM(CommissionAmount) AS CommissionAmount 
FROM #PreFinal
GROUP BY ManagerName, DebtorName, YearMonth, YearMonthDay
UNION ALL 
SELECT DISTINCT ManagerName, DebtorName, LEFT(Y.YearMonthDay, 7) AS YearMonth, Y.YearMonthDay
    ,0
    ,0
    ,0
    ,0
FROM #YearMonthDay y 
Cross apply (SELECT ManagerName, DebtorName, YearMonth, YearMonthDay
                ,SUM(TTV) AS TTV
                ,SUM(CommissionAmount) AS CommissionAmount 
            FROM #PreFinal
            GROUP BY ManagerName, DebtorName, YearMonth, YearMonthDay) P
ORDER BY ManagerName, DebtorName, YearMonthDay
以下是#PreFinal表中的数据示例及其数据类型

    CREATE TABLE #PreFinal (ManagerName VARCHAR(150), DebtorName VARCHAR(150), YearMonth VARCHAR(7), YearMonthDay DATE, TTV NUMERIC(16, 2), CommissionAmount NUMERIC(16, 2))

INSERT INTO #PreFinal VALUES 
    ('Person 1', 'Account Alpha', '2019-07', '07-01-2019', 11930.31, 996.34)
    ,('Person 1', 'Account Alpha', '2019-08', '08-01-2019', 83835.74, 6833.35)
    ,('Person 1', 'Account Beta', '2019-07', '07-01-2019', 188, 15.04)
    ,('Person 1', 'Account Beta', '2019-08', '08-01-2019', 8662.20, 769.18)
    ,('Person 1', 'Account Beta', '2019-09', '09-01-2019', 8497.73, 781.5)
    ,('Person 2', 'Account Gamma', '2019-07', '07-01-2019', 1478.38, 143.73)
请尝试下面的查询——这两种查询都可以

    select t2.ManagerName,t2.DebtorName,t1.YearMonth,t1.YearMonthDay,sum(isnull(t3.TTV,0)) as TTV,sum(isnull(t3.CommissionAmount,0)) as CommissionAmount   from #YearMonthDay t1
    cross apply (select distinct ManagerName,DebtorName  from #PreFinal) t2 
    left join #PreFinal t3 on t3.ManagerName=t2.ManagerName and t3.DebtorName=t2.DebtorName and t1.YearMonth=t3.YearMonth
    group by t2.ManagerName,t2.DebtorName,t1.YearMonth,t1.YearMonthDay
    order by t2.ManagerName,t2.DebtorName,t1.YearMonth,t1.YearMonthDay

----OR -----
    SELECT T1.ManagerName,T1.DebtorName,T1.YearMonth,T1.YearMonthDay,SUM(T1.TTV) AS TTV, SUM(T1.CommissionAmount) AS CommissionAmount  from
    (SELECT ManagerName, DebtorName, YearMonth, YearMonthDay    ,SUM(TTV) AS TTV    ,SUM(CommissionAmount) AS CommissionAmount FROM #PreFinal
    GROUP BY ManagerName, DebtorName, YearMonth, YearMonthDay
UNION ALL 
    SELECT DISTINCT ManagerName, DebtorName, LEFT(Y.YearMonthDay, 7) AS YearMonth, Y.YearMonthDay    ,0    ,0   FROM #YearMonthDay y 
Cross apply (SELECT ManagerName, DebtorName, YearMonth, YearMonthDay
                ,SUM(TTV) AS TTV
                ,SUM(CommissionAmount) AS CommissionAmount 
            FROM #PreFinal
            GROUP BY ManagerName, DebtorName, YearMonth, YearMonthDay) P
) T1
GROUP BY T1.ManagerName,T1.DebtorName,T1.YearMonth,T1.YearMonthDay
ORDER BY T1.ManagerName,T1.DebtorName,T1.YearMonth,T1.YearMonthDay
请尝试下面的查询——这两种查询都可以

    select t2.ManagerName,t2.DebtorName,t1.YearMonth,t1.YearMonthDay,sum(isnull(t3.TTV,0)) as TTV,sum(isnull(t3.CommissionAmount,0)) as CommissionAmount   from #YearMonthDay t1
    cross apply (select distinct ManagerName,DebtorName  from #PreFinal) t2 
    left join #PreFinal t3 on t3.ManagerName=t2.ManagerName and t3.DebtorName=t2.DebtorName and t1.YearMonth=t3.YearMonth
    group by t2.ManagerName,t2.DebtorName,t1.YearMonth,t1.YearMonthDay
    order by t2.ManagerName,t2.DebtorName,t1.YearMonth,t1.YearMonthDay

----OR -----
    SELECT T1.ManagerName,T1.DebtorName,T1.YearMonth,T1.YearMonthDay,SUM(T1.TTV) AS TTV, SUM(T1.CommissionAmount) AS CommissionAmount  from
    (SELECT ManagerName, DebtorName, YearMonth, YearMonthDay    ,SUM(TTV) AS TTV    ,SUM(CommissionAmount) AS CommissionAmount FROM #PreFinal
    GROUP BY ManagerName, DebtorName, YearMonth, YearMonthDay
UNION ALL 
    SELECT DISTINCT ManagerName, DebtorName, LEFT(Y.YearMonthDay, 7) AS YearMonth, Y.YearMonthDay    ,0    ,0   FROM #YearMonthDay y 
Cross apply (SELECT ManagerName, DebtorName, YearMonth, YearMonthDay
                ,SUM(TTV) AS TTV
                ,SUM(CommissionAmount) AS CommissionAmount 
            FROM #PreFinal
            GROUP BY ManagerName, DebtorName, YearMonth, YearMonthDay) P
) T1
GROUP BY T1.ManagerName,T1.DebtorName,T1.YearMonth,T1.YearMonthDay
ORDER BY T1.ManagerName,T1.DebtorName,T1.YearMonth,T1.YearMonthDay

向您的问题中添加DDL和示例数据,最好是将
insert
语句添加到表变量或临时表中,这将有助于我们为您提供帮助。猜测
YearMonthDay
可能是
Date
对任何人都没有帮助。感谢@HABO为我刚刚编辑的帖子提供了指导信息。在您的问题中添加DDL和示例数据,最好是
insert
语句到表变量或临时表中,将有助于我们帮助您。猜测
YearMonthDay
可能是
Date
对任何人都没有帮助。感谢@HABO提供的指导,我刚刚用这些信息编辑了这篇文章。您好,第一个查询最适合我。非常感谢。您好,第一个查询是最适合我的查询。非常感谢。