SQL 视图的查询修复
Query fix for a SQL 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
)
insert into @temp values(202005,1,1,'M01180','0','S',30)
insert into @temp values(202006,1,1,'M01180','0','P',20)
insert into @temp values(202007,1,1,'M01180','0','M',40)
insert into @temp values(202005,1,2,'M01180','0','S',15)
insert into @temp values(202006,1,2,'M01180','0','P',10)
insert into @temp values(202007,1,2,'M01180','0','P',15)
insert into @temp values(202005,2,1,'M01180','0','S',60)
insert into @temp values(202006,2,1,'M01180','0','P',15)
insert into @temp values(202007,2,1,'M01180','0','M',10)
insert into @temp values(202005,2,2,'M01180','0','S',25)
insert into @temp values(202006,2,2,'M01180','0','P',25)
insert into @temp values(202007,2,2,'M01180','0','A',30)
insert into @temp values(202005,1,1,'M01170','0','A',20)
insert into @temp values(202006,1,1,'M01170','0','A',20)
insert into @temp values(202007,1,1,'M01170','0','A',35)
insert into @temp values(202005,1,2,'M01170','0','P',15)
insert into @temp values(202006,1,2,'M01170','0','S',5)
insert into @temp values(202007,1,2,'M01170','0','M',5)
insert into @temp values(202005,2,1,'M01170','0','S',5)
insert into @temp values(202006,2,1,'M01170','0','P',15)
insert into @temp values(202007,2,1,'M01170','0','P',20)
insert into @temp values(202005,2,2,'M01170','0','S',5)
insert into @temp values(202006,2,2,'M01170','0','P',15)
insert into @temp values(202007,2,2,'M01170','0','M',10)
输出数据集应如下所示:
在上图中,Port cost
是 VOY_PL_USD
列与基于 voy_uid
、pc_uid
、ac_cd('M01180')
& sub_ac_cd
列。同样,Agent fee
是 VOY_PL_USD
列与基于 voy_uid
、pc_uid
、ac_cd('M01170')
和 [=19 的最新 jdg_sts
列值连接的 SUM =] 列。根据最新的Year_month
字段选择最新的记录。
请注意:我的查询将放在一个视图中。请告诉我如何实现它..
数据集:
这是我写的:
SELECT TOP (1) WITH TIES
Vuid
,Puid
,ac_cd
,sub_ac_cd
,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)
试试这个:
SELECT Vuid
,Puid
,CAST(MAX([Port Cost]) AS VARCHAR(12)) + MAX(CASE WHEN [Pro Cost Suffix] = 1 THEN [jdg_sts] END) AS [Port Cost]
,CAST(MAX([Agent Fee]) AS VARCHAR(12)) + MAX(CASE WHEN [Agent Fee Suffix] = 1 THEN [jdg_sts] END) AS [Agent Fee]
FROM
(
SELECT Vuid, Puid
,SUM(CASE WHEN ac_cd = 'M01180' THEN VOY_PL_USD END) OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd)
,SUM(CASE WHEN ac_cd = 'M01170' THEN VOY_PL_USD END) OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd)
,CASE WHEN ac_cd = 'M01180' THEN DENSE_RANK() OVER (PARTITION BY Vuid, Puid, sub_ac_cd ORDER BY Year_month DESC) END
,CASE WHEN ac_cd = 'M01170' THEN DENSE_RANK() OVER (PARTITION BY Vuid, Puid, sub_ac_cd ORDER BY Year_month DESC) END
,jdg_sts
FROM Temp
) DS (Vuid, Puid, [Port Cost], [Agent Fee], [Pro Cost Suffix], [Agent Fee Suffix], [jdg_sts])
GROUP BY Vuid
,Puid
ORDER BY Vuid
,Puid;
请使用以下临时 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
)
insert into @temp values(202005,1,1,'M01180','0','S',30)
insert into @temp values(202006,1,1,'M01180','0','P',20)
insert into @temp values(202007,1,1,'M01180','0','M',40)
insert into @temp values(202005,1,2,'M01180','0','S',15)
insert into @temp values(202006,1,2,'M01180','0','P',10)
insert into @temp values(202007,1,2,'M01180','0','P',15)
insert into @temp values(202005,2,1,'M01180','0','S',60)
insert into @temp values(202006,2,1,'M01180','0','P',15)
insert into @temp values(202007,2,1,'M01180','0','M',10)
insert into @temp values(202005,2,2,'M01180','0','S',25)
insert into @temp values(202006,2,2,'M01180','0','P',25)
insert into @temp values(202007,2,2,'M01180','0','A',30)
insert into @temp values(202005,1,1,'M01170','0','A',20)
insert into @temp values(202006,1,1,'M01170','0','A',20)
insert into @temp values(202007,1,1,'M01170','0','A',35)
insert into @temp values(202005,1,2,'M01170','0','P',15)
insert into @temp values(202006,1,2,'M01170','0','S',5)
insert into @temp values(202007,1,2,'M01170','0','M',5)
insert into @temp values(202005,2,1,'M01170','0','S',5)
insert into @temp values(202006,2,1,'M01170','0','P',15)
insert into @temp values(202007,2,1,'M01170','0','P',20)
insert into @temp values(202005,2,2,'M01170','0','S',5)
insert into @temp values(202006,2,2,'M01170','0','P',15)
insert into @temp values(202007,2,2,'M01170','0','M',10)
输出数据集应如下所示:
在上图中,Port cost
是 VOY_PL_USD
列与基于 voy_uid
、pc_uid
、ac_cd('M01180')
& sub_ac_cd
列。同样,Agent fee
是 VOY_PL_USD
列与基于 voy_uid
、pc_uid
、ac_cd('M01170')
和 [=19 的最新 jdg_sts
列值连接的 SUM =] 列。根据最新的Year_month
字段选择最新的记录。
请注意:我的查询将放在一个视图中。请告诉我如何实现它..
数据集:
这是我写的:
SELECT TOP (1) WITH TIES
Vuid
,Puid
,ac_cd
,sub_ac_cd
,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)
试试这个:
SELECT Vuid
,Puid
,CAST(MAX([Port Cost]) AS VARCHAR(12)) + MAX(CASE WHEN [Pro Cost Suffix] = 1 THEN [jdg_sts] END) AS [Port Cost]
,CAST(MAX([Agent Fee]) AS VARCHAR(12)) + MAX(CASE WHEN [Agent Fee Suffix] = 1 THEN [jdg_sts] END) AS [Agent Fee]
FROM
(
SELECT Vuid, Puid
,SUM(CASE WHEN ac_cd = 'M01180' THEN VOY_PL_USD END) OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd)
,SUM(CASE WHEN ac_cd = 'M01170' THEN VOY_PL_USD END) OVER (PARTITION BY Vuid, Puid, ac_cd, sub_ac_cd)
,CASE WHEN ac_cd = 'M01180' THEN DENSE_RANK() OVER (PARTITION BY Vuid, Puid, sub_ac_cd ORDER BY Year_month DESC) END
,CASE WHEN ac_cd = 'M01170' THEN DENSE_RANK() OVER (PARTITION BY Vuid, Puid, sub_ac_cd ORDER BY Year_month DESC) END
,jdg_sts
FROM Temp
) DS (Vuid, Puid, [Port Cost], [Agent Fee], [Pro Cost Suffix], [Agent Fee Suffix], [jdg_sts])
GROUP BY Vuid
,Puid
ORDER BY Vuid
,Puid;