简体   繁体   English

SQL 得到上个月的零钱

[英]SQL get change from previous month

I have a couple of tables that looks similar to this one with this dummy data:我有几个表看起来与这个相似的表带有这个虚拟数据:

CREATE TABLE ClientTable (
    ClientId int,
    SupervisorId int,
    ManagerId int,
    Date datetime,
    OthersNonRelevantColumns varchar
)

CREATE TABLE ClientValues (
    ClientId int,
    SupervisorId int,
    ManagerId int,
    ValueDate date,
    Value float,
    OthersNonRelevantColumns varchar
)

INSERT INTO dbo.ClientTable
VALUES
( 1,1,1, '2021-01-31', ''),
( 2,1,1, '2021-02-28', ''),
( 3,1,1, '2021-02-28', ''),
( 4,2,1, '2021-03-31', ''),
( 5,2,1, '2021-03-31', ''),
( 6,3,2, '2021-01-31', ''),
( 7,3,2, '2021-03-31', '')

INSERT INTO dbo.ClientValues
VALUES
( 1,1,1, '2021-01-31', 100.0, ''),
( 1,1,1, '2021-02-28', 90.0, ''),
( 1,1,1, '2021-03-31', 110.0, ''),
( 2,1,1, '2021-02-28', 50.0, '' ),
( 2,1,1, '2021-03-31', 75.0, '' ),
( 3,1,1, '2021-02-28', 100.0, ''),
( 3,1,1, '2021-03-31', 101.0, ''),
( 4,2,1, '2021-03-31', 90.0, ''),
( 5,2,1, '2021-01-31', 70.0, ''),

( 6,3,2, '2021-01-31', 10.0, ''),
( 6,3,2, '2021-02-28', 15.0, ''),
( 6,3,2, '2021-03-31', 30.0, ''),
( 7,3,2, '2021-03-31', 100.0, '')

and I will need to get the number of supervisors, clients, value and change from previous month results grouped by end of month date and managers, so for example the expected result for the previous data could be something like this.我将需要按月末日期和经理分组的上个月结果的主管、客户、价值和变化的数量,例如,以前数据的预期结果可能是这样的。 Bear in mind the data is cumulative for supervisors and clients in ClientsTable请记住,客户表中主管和客户的数据是累积的

EOMDate     ManagerId   Supervisors     Clients         Value       ChangeValue     ChgPerc
2021-01-31  1           1               1               170         
2021-01-31  2           1               1               10          
2021-02-28  1           1               3               240         70              41%
2021-02-28  2           1               1               15          5               50%
2021-03-31  1           2               5               376         136             57%
2021-03-31  2           1               2               130         115             866%

What I have so far is this query but I don´t know how to get the change from previous month and also I miss the february record for manager 2到目前为止,我所拥有的是这个查询,但我不知道如何获得上个月的变化,而且我错过了经理 2 的 2 月记录

SELECT
    EOMONTH(ct.[Date]) AS EoMDate,
    ct.ManagerId,
    (SELECT count(DISTINCT ct2.SupervisorId) AS AccHolders
        FROM ClientTable ct2
        WHERE ct2.ManagerId= ct.ManagerId
        AND EOMONTH(ct2.[Date]) <= EOMONTH(ct.[Date])) AS AccountHolders,
    (SELECT count(ct3.ClientId) AS Clients
        FROM ClientTable ct3 
        WHERE ct3.ManagerId = ct.ManagerId
        AND EOMONTH(ct3.[Date]) <= EOMONTH(ct.[Date])) AS NumberOfClients,
    (SELECT SUM(cv.[Value]) AS Net
        FROM ClientValues cv
        WHERE cv.ManagerId= ct.ManagerId
        AND EOMONTH(cv.ValueDate) <= EOMONTH(ct.[Date])) NetPosition
FROM ClientTable ct
GROUP BY ct.ManagerId, EOMONTH(ct.[Date])
ORDER BY EoMDate, ct.ManagerId

You may try the following which uses window functions to extract your net and net position values from a subquery similar to your initial attempt but using data from the ClientValue table.您可以尝试以下操作,它使用 window 函数从子查询中提取您的净值和净 position 值,类似于您的初始尝试,但使用来自ClientValue表的数据。

SELECT
    EoMDate, 
    ManagerId,
    Supervisors,
    Clients,
    Value,
    Value - LAG(Value) OVER (PARTITION BY ManagerId ORDER BY EoMDate) as ChangeValue,
    CAST(100*(Value - LAG(Value) OVER (PARTITION BY ManagerId ORDER BY EoMDate))/LAG(Value) OVER (PARTITION BY ManagerId ORDER BY EoMDate) AS DECIMAL(5,2)) as ChangePerc
FROM
    (
    SELECT
        EOMONTH(cv.[ValueDate]) AS EoMDate,
        cv.ManagerId,
        (
            SELECT COUNT(DISTINCT ct.SupervisorId)
            FROM ClientTable ct
            WHERE EOMONTH(ct.[Date])<=EOMONTH(cv.[ValueDate]) AND ct.ManagerId = cv.ManagerId
        )  as Supervisors,
        (
            SELECT COUNT(DISTINCT ct.ClientId)
            FROM ClientTable ct
            WHERE EOMONTH(ct.[Date])<=EOMONTH(cv.[ValueDate]) AND ct.ManagerId = cv.ManagerId
        ) as Clients,
        SUM(cv.Value) as Value
    FROM
        ClientValues cv
    GROUP BY
        EOMONTH(cv.[ValueDate]), cv.ManagerId
) t
ORDER BY
    EoMDate,ManagerId

View Demo on Db Fiddle 在 Db Fiddle 上查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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