我想 select 相同 Table 相同的列来分隔 Desc_Main 和 Desc_sup 通过 cdValuse
I want to select same Table same column to separate Desc_Main and Desc_sup by cdValuse
我想从 cdValue
中分离到 Desc_Main 和 Desc_Sup
- 100000 是主要的并且有来自 cdValue
的 sup 值是 '101000','102000'
- 200000 是主要的并且有来自 cdValue
的 sup 值是 '201000','202000'
我想在这个table
中展示
我的脚本
SELECT
cdValue
, Desc_sup = CASE Left(cdValue,3)
WHEN '100' THEN cdDesc_th
WHEN '200' THEN cdDesc_th
ELSE cdDesc_th END
,
Desc_Main = CASE Left(cdValue,3)
WHEN '101' THEN cdDesc_th
WHEN '102' THEN cdDesc_th
WHEN '103' THEN cdDesc_th
WHEN '104' THEN cdDesc_th
WHEN '105' THEN cdDesc_th
WHEN '106' THEN cdDesc_th
WHEN '201' THEN cdDesc_th
WHEN '202' THEN cdDesc_th
ELSE cdDesc_th END
FROM SellingAgent..tb_LookupCode (NOLOCK)
WHERE cdType='Cat_FAQ'
请帮我使用Case-when 非常感谢
如果你的DBMS版本是2012+
那么,可以使用first_value()
函数
with tb_LookupCode(cdValue, Desc_sup, Desc_Main) as
(
select 100000, 'FAQ','FAQ' union all
select 101000, 'Philip Fund','Philip Fund' union all
select 102000, 'LTF&RMF','LTF&RMF' union all
select 200000, 'Article','Article' union all
select 201000, 'Financial Article','Financial Article'
)
select first_value(cdValue) over
(partition by substring(cast(cdValue as varchar(25)),1,1)
order by cdValue)
as cdValue,
first_value(Desc_sup) over
(partition by substring(cast(cdValue as varchar(25)),1,1)
order by substring(cast (cdValue as varchar(25)),1,1))
as Desc_sup,
Desc_Main
from tb_LookupCode l;
cdValue Desc_sup Desc_Main
------- -------- ------------------
100000 FAQ FAQ
100000 FAQ Philip Fund
100000 FAQ LTF&RMF
200000 Article Article
200000 Article Financial Article
我想从 cdValue
中分离到 Desc_Main 和 Desc_Sup- 100000 是主要的并且有来自 cdValue 的 sup 值是 '101000','102000'
- 200000 是主要的并且有来自 cdValue 的 sup 值是 '201000','202000'
我想在这个table
中展示我的脚本
SELECT
cdValue
, Desc_sup = CASE Left(cdValue,3)
WHEN '100' THEN cdDesc_th
WHEN '200' THEN cdDesc_th
ELSE cdDesc_th END
,
Desc_Main = CASE Left(cdValue,3)
WHEN '101' THEN cdDesc_th
WHEN '102' THEN cdDesc_th
WHEN '103' THEN cdDesc_th
WHEN '104' THEN cdDesc_th
WHEN '105' THEN cdDesc_th
WHEN '106' THEN cdDesc_th
WHEN '201' THEN cdDesc_th
WHEN '202' THEN cdDesc_th
ELSE cdDesc_th END
FROM SellingAgent..tb_LookupCode (NOLOCK)
WHERE cdType='Cat_FAQ'
请帮我使用Case-when 非常感谢
如果你的DBMS版本是2012+
那么,可以使用first_value()
函数
with tb_LookupCode(cdValue, Desc_sup, Desc_Main) as
(
select 100000, 'FAQ','FAQ' union all
select 101000, 'Philip Fund','Philip Fund' union all
select 102000, 'LTF&RMF','LTF&RMF' union all
select 200000, 'Article','Article' union all
select 201000, 'Financial Article','Financial Article'
)
select first_value(cdValue) over
(partition by substring(cast(cdValue as varchar(25)),1,1)
order by cdValue)
as cdValue,
first_value(Desc_sup) over
(partition by substring(cast(cdValue as varchar(25)),1,1)
order by substring(cast (cdValue as varchar(25)),1,1))
as Desc_sup,
Desc_Main
from tb_LookupCode l;
cdValue Desc_sup Desc_Main
------- -------- ------------------
100000 FAQ FAQ
100000 FAQ Philip Fund
100000 FAQ LTF&RMF
200000 Article Article
200000 Article Financial Article