我有下表:
表1:
filename File_ID date version Broker1_file_12312 5093 20180108 1 Broker1_file_1sa3e_Renamed 3956 20180108 2 Broker2_file_d3223f 3109 20180108 1 Broker2_file_j3df 4526 20180108 2 Broker2_file_dj3498_interest 3574 20180108 3 Broker3_file_fj934f 5603 20180108 1 Broker3_file_jdf34f 5225 20180108 1 Broker3_file_K9d043 5926 20180108 1 Broker4_file_c34km 4865 20180108 1 Broker5_file_cf4k3 3212 20180108 1
我需要根据条件在输出中选择'filename'和'file_ID'
下面的代码有效,但是我无法合并“不喜欢”功能,并选择最新版本
SELECT t1.file_id, vals.val FROM table1 t1 INNER JOIN (VALUES ('Broker1'), ('Broker2'), ('Broker3'),('Broker4'),('Broker'),('Broker5')) Vals(val) ON t1.column1 LIKE '%' + vals.val + '%'
我需要一个输出,使其不选择file_ID,其中filename包含Broker1的字符串“ Renamed”和Broker2的“ interest”。 另外,我需要选择最新版本。
因此从上面需要基于以下注释的输出:
filename File_ID date version Comment Boker1_file_12312 4007 20180108 1 To be shown in output Boker1_file_1sa3e_Renamed 5147 20180108 2 Not be shown in output Boker2_file_d3223f 3496 20180108 1 Not be shown in output Boker2_file_j3df 3880 20180108 2 To be shown in output Boker2_file_dj3498_interest 5817 20180108 3 Not be shown in output Boker3_file_fj934f 4541 20180108 1 Not be shown in output Boker3_file_jdf34f 5818 20180108 2 Not be shown in output Boker3_file_K9d043 4910 20180108 3 To be shown in output Boker4_file_c34km 3101 20180108 1 Not be shown in output Boker5_file_cf4k3 5209 20180108 1 Not be shown in output
最终输出:
val File_ID Boker1 4007 Boker2 3880 Boker3 4910 Boker4 3101 Broker error1 Broker5 error2
我正在尝试从以下代码中添加最新版本:
select file_id from table1 as t
where date = '20190108'
and filename like ('%Broker1%')
and t.version = (select max(version) from table1 as t2
where t2.date = t.date
and t2.filename like t.filename
)
答案 0 :(得分:0)
要过滤出文件名既不匹配模式'%Broker1%Renamed%'
也不匹配模式'Broker2%interest'
的Recret,您可以在查询中添加另一个JOIN
。
SELECT vals.val broker_name, t1.file_id
FROM
table1 t1
INNER JOIN (VALUES ('Broker1'), ('Broker2'), ('Broker3'),('Broker4')) Vals(val)
ON t1.column1 LIKE '%' + vals.val + '%'
INNER JOIN (VALUES ('Broker1%Renamed'), ('Broker2%interest')) NotVals(val)
ON t1.filename NOT LIKE '%' + NotVals.val + '%'
编辑
如果您正在寻找每个经纪人的最高版本的file_id
,则此查询虽然很难看,但应该可以完成工作:
WITH cte AS (
SELECT vals.val broker_name, t1.file_id, t1.version
FROM
table1 t1
INNER JOIN (VALUES ('Broker1'), ('Broker2'), ('Broker3'),('Broker4')) Vals(val)
ON t1.column1 LIKE '%' + vals.val + '%'
INNER JOIN (VALUES ('Broker1%Renamed'), ('Broker2%interest')) NotVals(val)
ON t1.filename NOT LIKE '%' + NotVals.val + '%'
)
SELECT *
FROM
cte
INNER JOIN (SELECT broker_name, MAX(version) version FROM cte GROUP BY broker_name) ctemax
ON ctemax.broker_name = cte.broker_name
AND ctemax.version = cte.version
答案 1 :(得分:0)
只需再插入一个内部联接
SELECT t1.file_id, vals.val, max(version) over (partition by t1.file_id) version
FROM
table1 t1
INNER JOIN (VALUES ('Broker1'), ('Broker2'), ('Broker3'),('Broker4')) Vals(val)
ON t1.column1 LIKE '%' + vals.val + '%'
INNER jOIN (VALUES ('Broker1'), ('Broker2'), ('Broker3'),('Broker4')) Vals2(val)
ON t1.column1 NOT LIKE '%' + vals2.val + '%'
或分组依据
SELECT t1.file_id, vals.val, max(version)
FROM
table1 t1
INNER JOIN (VALUES ('Broker1'), ('Broker2'), ('Broker3'),('Broker4')) Vals(val)
ON t1.column1 LIKE '%' + vals.val + '%'
INNER jOIN (VALUES ('Broker1'), ('Broker2'), ('Broker3'),('Broker4')) Vals2(val)
ON t1.column1 NOT LIKE '%' + vals2.val + '%'
group by t1.file_id, vals.val