我有一些值设计编号,这些编号增加或更改了最后几个字符,例如30200XXX X000更改为30200XXX X010。它们存储在同一列的表中,出于显示目的,旧编号早于旧编号,我希望新编号显示在同一行中旧编号的右列。有什么办法可以在SQL中做到这一点?
这是我用来提取数据的简单查询
SELECT DISTINCT COALESCE (LATEST_DCC_NO,")
, RTRIM(A.PRT_NO)
, COALESCE(SUBSTR(C.TGT_DC_PRT_NAME,1,20),'')AS "PART NAME"
, A.SUP_NO
, B.SUP_NME
, A.TGT_SHPTO_CDE
, A.MTC_MODEL
, A.EFF_BG_DTE
,A.EFF_END_DTE
,A.PRT_QTY
FROM XXX.XXA1 A LEFT OUTER JOIN XXX.XRT1 C ON C.TGT_XC_PRT_NO = A.PRT_NO
, PXX.PUXSM1 B
WHERE SUBSTR(A.MTX_MDE,1,1) IN('K')
AND A.SUP_NO = B.SUP_NO
ORDER BY 2
这就是我目前所得到的
OLD/ NEW PRT_NUM
3000TKK Z003
3000TKK Z004
3000XXX X010
3000XXX X020
51000ZA K001
51000ZA K002
这是我想要的结果
OLD_PRT_NUM NEW_PRT_NUM
3000TKK Z003 3000TKK Z004
3000XXX X010 3000XXX X020
51000ZA K001 51000ZA K002
答案 0 :(得分:0)
假设few last characters
是空格后面的那些字符。
with tab (PRT_NUM) as (values
'3000TKK Z003'
, '3000TKK Z004'
, '3000XXX X010'
, '3000XXX X020'
, '51000ZA K001'
, '51000ZA K002'
)
select
-- substr(PRT_NUM, 1, 7) || ' ' || min(substr(PRT_NUM, 9)) OLD_PRT_NUM
--, substr(PRT_NUM, 1, 7) || ' ' || max(substr(PRT_NUM, 9)) NEW_PRT_NUM
substr(PRT_NUM, 1, locate(' ', PRT_NUM)-1) || ' ' || min(substr(PRT_NUM, locate(' ', PRT_NUM)+1)) OLD_PRT_NUM
, substr(PRT_NUM, 1, locate(' ', PRT_NUM)-1) || ' ' || max(substr(PRT_NUM, locate(' ', PRT_NUM)+1)) NEW_PRT_NUM
from tab
group by
--substr(PRT_NUM, 1, 7)
substr(PRT_NUM, 1, locate(' ', PRT_NUM)-1);