需要修复 SQL 用于创建视图的查询

Need to fix a SQL Query for creating a view

请使用以下临时 table 和数据集作为参考。使用以下数据集,我正在尝试创建一个新数据集。

DECLARE @Temp TABLE 
(
year_month int,
Vuid int,
Puid int,
ac_cd varchar(20),
sub_ac_cd varchar(20),
jdg_sts varchar(20),
voy_pl_usd int,
updt_stamp datetime
)
insert into @temp values(
202005,1,1,'M011','0','S',30,GETDATE()-2
)
insert into @temp values(
202006,1,1,'M011','0','P',20,GETDATE()-1
)
insert into @temp values(
202007,1,1,'M011','0','M',40,GETDATE()  
)
insert into @temp values(
202005,1,2,'M011','0','S',15,GETDATE()-2
)
insert into @temp values(
202006,1,2,'M011','0','P',10,GETDATE()-1
)
insert into @temp values(
202007,1,2,'M011','0','P',15,GETDATE()
)

输出数据集应如下所示:

我写了下面的查询,它部分满足了我的要求:

(SELECT Vuid, Puid, ac_cd, sub_ac_cd, SUM (VOY_PL_USD) AS Cost
     FROM @Temp
     GROUP BY
            Vuid,
            Puid,
            ac_cd,
            sub_ac_cd
            )

但 Cost 还应将最新的 jdg_sts 列值与 SUM 一起附加。我知道最新的记录是根据最新 Year_month.

选择的

请注意:我的查询将放在 VIEW 中。请告诉我如何实现它..

数据集:

另一个版本的输出:

试试这个:

CREATE OR ALTER VIEW vw_view
AS
(
SELECT TOP (1) WITH TIES *
      ,SUM(VOY_PL_USD) OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd) cost
FROM Temp
ORDER BY ROW_NUMBER() OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd ORDER BY year_month DESC) 
)
GO

要将值放在一列中,只需使用字符串连接:

CREATE OR ALTER VIEW vw_view_1
AS
(
    SELECT TOP (1) WITH TIES year_month
                            ,Vuid
                            ,Puid
                            ,ac_cd
                            ,sub_ac_cd                          
                            ,voy_pl_usd
                            ,updt_stamp
                            ,CAST(SUM(VOY_PL_USD) OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd) AS VARCHAR(12)) + jdg_sts AS [cost]
    FROM Temp
    ORDER BY ROW_NUMBER() OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd ORDER BY year_month DESC) 
)
GO
 DECLARE @Temp TABLE (
    year_month INT
    ,Vuid INT
    ,Puid INT
    ,ac_cd VARCHAR(20)
    ,sub_ac_cd VARCHAR(20)
    ,jdg_sts VARCHAR(20)
    ,voy_pl_usd INT
    ,updt_stamp DATETIME
    )

INSERT INTO @temp
VALUES (
    202005
    ,1
    ,1
    ,'M011'
    ,'0'
    ,'S'
    ,30
    ,GETDATE() - 2
    )

INSERT INTO @temp
VALUES (
    202006
    ,1
    ,1
    ,'M011'
    ,'0'
    ,'P'
    ,20
    ,GETDATE() - 1
    )

INSERT INTO @temp
VALUES (
    202007
    ,1
    ,1
    ,'M011'
    ,'0'
    ,'M'
    ,40
    ,GETDATE()
    )

INSERT INTO @temp
VALUES (
    202005
    ,1
    ,2
    ,'M011'
    ,'0'
    ,'S'
    ,15
    ,GETDATE() - 2
    )

INSERT INTO @temp
VALUES (
    202006
    ,1
    ,2
    ,'M011'
    ,'0'
    ,'P'
    ,10
    ,GETDATE() - 1
    )

INSERT INTO @temp
VALUES (
    202007
    ,1
    ,2
    ,'M011'
    ,'0'
    ,'P'
    ,15
    ,GETDATE()
    )

SELECT *
FROM @Temp(SELECT Vuid, Puid, ac_cd, sub_ac_cd, SUM(VOY_PL_USD) AS Cost FROM @Temp GROUP BY Vuid, Puid, ac_cd, sub_ac_cd);

WITH cte
AS (
    SELECT Puid
        ,jdg_sts
        ,ROW_NUMBER() OVER (
            PARTITION BY puid ORDER BY updt_stamp DESC
            ) AS run
    FROM @Temp
    )
SELECT A.*
    ,cast(Cost AS VARCHAR) + jdg_sts
FROM (
    SELECT T1.Vuid
        ,T1.Puid
        ,T1.ac_cd
        ,T1.sub_ac_cd
        ,SUM(T1.VOY_PL_USD) AS Cost
    FROM @Temp T1
    GROUP BY T1.Vuid
        ,T1.Puid
        ,T1.ac_cd
        ,T1.sub_ac_cd
    ) A
LEFT JOIN cte cte ON cte.Puid = A.Puid
WHERE run = 1