从具有互斥约束的单个字段查询 return 两列
Query to return two columns from single field with mutually exclusive constraint
我想从单个字段中获取基本上显示的两列;一个具有当前计划值,另一个具有最近的前一个计划,结构如下 table。我想 select 只记录 StatusId != 2(记录数=3)。并显示 StatusId = 2 的 PrevPlan 列。
并且想要如下所示的输出。
尝试过的查询
我使用这些 table 的常规内部连接和外部应用(使用 CreatedDate Desc 排序,因为有更多旧值)来获得 PrevPlan。但它正在用相同的值更新整个 PrevPlan 列。返回的记录数是 3,这是正确的。
OUTER APPLY
(
SELECT TOP 1
PlanPrev.Id
,PlanPrev.[Name]
FROM
Plans PlanPrev
inner join Trials tPrev on tPrev.PlanId = PlanPrev.Id
WHERE
tPrev.StatusId = 2
ORDER BY
tPrev.CreatedDate Desc
) AS PlanPrev
所以我在下面使用了一个 CTE,它正在更新,但它返回所有 6 条记录而不是 3 条。
WITH ContractsCTE (ContractId, CurrentPlan, PrevPlan)
AS
(
Select ContractId, p.[Name] as CurrentPlan, p.[Name] as PrevPlan
from Trials t
inner join Contracts c on c.Id = t.ContractId
inner join Plans p on t.PlanId = p.Id
where t.StatusId != 2
)
select ContractId, cte.CurrentPlan, p.[Name] as PrevPlan
from ContractsCTE cte
left join Trials t1 on cte.ContractId = t1.ContractId
left join Contracts c1 on c1.Id = t1.ContractId
left join Plans p1 on t1.PlanId = p1.Id
and t1.StatusId = 2
Order by CreateDate desc;
有没有一种方法可以在不使用任何 Temp tables/update 查询的情况下实现它?任何帮助将不胜感激。
我们可以先把之前的plan明细分离出来再用。
我稍微修改了 CTE 的第二部分如下:
select cte.ContractId, cte.CurrentPlan, p1.[Name] as PrevPlan
from ContractsCTE cte
left join (SELECT t.CONTRACTID, t.STATUSID, t.PLANID, row_number() OVER (PARTITION BY t.CONTRACTID, t.STATUSID ORDER BY t.CREATEDATE DESC) rn FROM @TRAILS t WHERE t.STATUSID = 2)t1
on cte.ContractId = t1.ContractId AND t1.rn = 1
left join @CONTRACTS c1 on c1.Id = t1.ContractId AND t1.rn = 1
left join @PLANS p1 on t1.PlanId = p1.Id AND t1.rn = 1
Order by cte.CONTRACTID
db<>fiddle here.
我想从单个字段中获取基本上显示的两列;一个具有当前计划值,另一个具有最近的前一个计划,结构如下 table。我想 select 只记录 StatusId != 2(记录数=3)。并显示 StatusId = 2 的 PrevPlan 列。
并且想要如下所示的输出。
尝试过的查询
我使用这些 table 的常规内部连接和外部应用(使用 CreatedDate Desc 排序,因为有更多旧值)来获得 PrevPlan。但它正在用相同的值更新整个 PrevPlan 列。返回的记录数是 3,这是正确的。
OUTER APPLY
(
SELECT TOP 1
PlanPrev.Id
,PlanPrev.[Name]
FROM
Plans PlanPrev
inner join Trials tPrev on tPrev.PlanId = PlanPrev.Id
WHERE
tPrev.StatusId = 2
ORDER BY
tPrev.CreatedDate Desc
) AS PlanPrev
所以我在下面使用了一个 CTE,它正在更新,但它返回所有 6 条记录而不是 3 条。
WITH ContractsCTE (ContractId, CurrentPlan, PrevPlan)
AS
(
Select ContractId, p.[Name] as CurrentPlan, p.[Name] as PrevPlan
from Trials t
inner join Contracts c on c.Id = t.ContractId
inner join Plans p on t.PlanId = p.Id
where t.StatusId != 2
)
select ContractId, cte.CurrentPlan, p.[Name] as PrevPlan
from ContractsCTE cte
left join Trials t1 on cte.ContractId = t1.ContractId
left join Contracts c1 on c1.Id = t1.ContractId
left join Plans p1 on t1.PlanId = p1.Id
and t1.StatusId = 2
Order by CreateDate desc;
有没有一种方法可以在不使用任何 Temp tables/update 查询的情况下实现它?任何帮助将不胜感激。
我们可以先把之前的plan明细分离出来再用。
我稍微修改了 CTE 的第二部分如下:
select cte.ContractId, cte.CurrentPlan, p1.[Name] as PrevPlan
from ContractsCTE cte
left join (SELECT t.CONTRACTID, t.STATUSID, t.PLANID, row_number() OVER (PARTITION BY t.CONTRACTID, t.STATUSID ORDER BY t.CREATEDATE DESC) rn FROM @TRAILS t WHERE t.STATUSID = 2)t1
on cte.ContractId = t1.ContractId AND t1.rn = 1
left join @CONTRACTS c1 on c1.Id = t1.ContractId AND t1.rn = 1
left join @PLANS p1 on t1.PlanId = p1.Id AND t1.rn = 1
Order by cte.CONTRACTID
db<>fiddle here.