简体   繁体   中英

Sum values by month, year, and by account, for all months leading up to todays month and show 0's values where data does not exist for specific row

I am having trouble showing my data as requested. I have tried Using recursive CTE table (to the the best of my knowledge at least), ive have tried a cross apply and outer apply but that only then repeats the values for all the dates and results in duplicated values. I need to sum up totals by Manager, Account and by the Month for our Fiscal Year (07/01/xxxx - 6/30/xxxx).

Data would need to look like this where it creates a row for all accounts and zeros out the totals if it doesnt exist for that month:

+-------------+---------------+-----------+--------------+----------+------------------+
| 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             |
+-------------+---------------+-----------+--------------+----------+------------------+

Sometimes the rows will have values in all months and sometimes they wont. But i have not found a good way to get this to show correctly.

Here is an example of me trying a recursive cte with a cross apply but it duplicated values for months. For example for Account Alpha in July it gave me one row with the actual values and one row with 0's. I cannot just grab the max of that column because sometimes people will and can have negative values. #PreFinal is just the table that holds the actual values for account by month and 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

Here is the example of the data that is in the #PreFinal table and what its data types are.

    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)

Please try below queries -- Both will work

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM