提前谢谢。
我有一个table1:
ID || Location
ABC-999-8710-12345-000 || THYW.A
ABC-900-8560-54321-100 || UJNC.E
ABC-997-0090-98765-010 || QTHB.P
ABC-923-2340-67890-001 || OMYB.Y
ABC-009-8000-24680-000 || EVHO.N
ABC-009-8000-26954-010 || IUTO.N
我有另一张桌子2:
ID || Location
ABC012345-00 || THYW.A
ABC054321-10 || UJNC.E
ABC098765-01 || QTHB.P
ABC067890-00 || OMYB.Y
ABC024680-00 || EVHO.N
ABC012678-01 || IUTO.N
我需要输出表格为:
ID || Location || OutPut
ABC-999-8710-12345-000 || THYW.A || Success
ABC-900-8560-54321-100 || UJNC.E || Success
ABC-997-0090-98765-010 || QTHB.P || Success
ABC-923-2340-67890-001 || OMYB.Y || Success
ABC-009-8000-24680-000 || EVHO.N || Success
ABC-009-8000-26954-010 || IUTO.N ||
我需要获取字符串的一部分,如果它与table2中的ID匹配,那么我需要将列输出更新为成功。
这是我写的更新查询:
update INT
set Output= 'Success'
FROM
[dbo].[table1] INT
join [dbo].[table2] SHB
on INT.ID=SHB.ID
答案 0 :(得分:2)
使用substring提取字符串并加入它们。
update INT
set Output= 'Success'
join [dbo].[table2] SHB
on concat(substring(INT.ID,1,3), '0', substring(INT.ID,14,8))=SHB.ID
答案 1 :(得分:2)
您可以简单地使用此查询。
假设 - 表1中的ID模式保持不变。
SELECT a.ID, b.Location,
CASE WHEN b.Location IS NOT NULL
THEN 'Success' ELSE 'Failed' END as Output
FROM table1 a
LEFT JOIN table2 b
ON LEFT(a.ID,3) + '0' + SUBSTRING(a.ID,14,5) +
SUBSTRING(a.ID,19,3) = b.ID
如果您只想更新查询
UPDATE a
SET Output = (CASE WHEN b.Location IS NOT NULL
THEN 'Success' ELSE 'Failed' END)
FROM table1 a
LEFT JOIN table2 b
ON LEFT(a.ID,3) + '0' + SUBSTRING(a.ID,14,5) +
SUBSTRING(a.ID,19,3) = b.ID
答案 2 :(得分:1)
您也可以使用print (a.map(s))
0 100
1 100
2 100
3 100
4 100
5 200
6 200
7 200
8 200
Name: Week, dtype: int64
print (df2.index.to_series().groupby(a).transform('size'))
0 5
1 5
2 5
3 5
4 5
5 4
6 4
7 4
8 4
dtype: int64
功能使用Left Join
,如下所示:
Substring()
您的SELECT T.ID,
T.Location,
CASE
WHEN T1.Location IS NULL
THEN ''
ELSE 'Success'
END AS 'Update'
FROM
(
SELECT *
FROM table1
) T
LEFT JOIN table2 T1 ON SUBSTRING(T1.ID, 5, 5) = SUBSTRING(T.ID, 14, 5);
将如下所示:
Update Query
期望的结果:
UPDATE INT SET INT.Output = CASE
WHEN SHB.Location IS NULL
THEN ''
ELSE 'Success'
END
FROM table1 INT
LEFT JOIN table2 SHB ON SUBSTRING(SHB.ID, 5, 5) = SUBSTRING(INT.ID, 14, 5);