SQL CROSS应用逗号分隔列表

时间:2015-01-29 20:23:17

标签: sql sql-server

我正在尝试仅显示符合位置条件的记录。困境是位置存储在逗号分隔列表中,这使得它变得更难。

这是代码

DECLARE @sessionStart DATETIME, 
@sessionEnd DATETIME, 
@instructorKey INT, 
@locationKey INT 

SET @instructorKey = 1
SET @sessionStart = '2015-03-01'
SET @sessionEnd = '2015-04-05'
SET @locationKey = null 
SELECT SU.sessionUnitKey, s.locationKey,s.locationKeyList,SU.sessionStart, SU.sessionEnd
FROM sessionUnit SU WITH (NOLOCK) 
INNER JOIN session S WITH (NOLOCK) ON SU.sessionKey = S.sessionKey 
left JOIN location L WITH (NOLOCK) ON (S.locationKey = L.locationKey ) 
INNER JOIN product P WITH (NOLOCK) ON S.productKey = P.productKey 
INNER JOIN users U WITH (NOLOCK) ON SU.instructorKey = U.userKey 
INNER JOIN lkup_productType PT WITH (NOLOCK) ON P.productTypeKey = PT.productTypeKey 

CROSS APPLY DelimitedSplit8K('10,260,34,102,15', ',')  ds 
inner join Location L2 on L2.locationKey = ds.Item

WHERE 1=1 
AND EXISTS ( SELECT 1 from ins_car_loc ICL WITH (NOLOCK) WHERE ICL.RptingInskey = SU.instructorKey and ICL.instructorKey = @instructorKey and ICL.endDt is NULL ) 


AND (@sessionStart IS NULL OR SU.sessionStart >= @sessionStart) AND (@sessionEnd IS NULL OR SU.sessionStart <= @sessionEnd) 
ORDER BY SU.sessionUnitKey 

,这是示例输出

sessionUnitKey  locationKey locationKeyList sessionStart    sessionEnd
171331  34  NULL    2015-03-04 07:15:00.000 2015-03-04 09:15:00.000
171331  34  NULL    2015-03-04 07:15:00.000 2015-03-04 09:15:00.000
171331  34  NULL    2015-03-04 07:15:00.000 2015-03-04 09:15:00.000
171331  34  NULL    2015-03-04 07:15:00.000 2015-03-04 09:15:00.000
171331  34  NULL    2015-03-04 07:15:00.000 2015-03-04 09:15:00.000
10:00:00.000
172374  NULL    15,10,34,102,260    2015-03-15 08:00:00.000 2015-03-15 10:00:00.000
172374  NULL    15,10,34,102,260    2015-03-15 08:00:00.000 2015-03-15 10:00:00.000
172375  NULL    4,5 2015-03-15 14:15:00.000 2015-03-15 16:15:00.000
172375  NULL    4,5 2015-03-15 14:15:00.000 2015-03-15 16:15:00.000
172375  NULL    4,5 2015-03-15 14:15:00.000 2015-03-15 16:15:00.000
172375  NULL    4,5 2015-03-15 14:15:00.000 2015-03-15 16:15:00.000
172375  NULL    4,5 2015-03-15 14:15:00.000 2015-03-15 16:15:00.000
172376  NULL    4,5 2015-03-15 16:30:00.000 2015-03-15 18:30:00.000
172376  NULL    4,5 2015-03-15 16:30:00.000 2015-03-15 18:30:00.000
172376  NULL    4,5 2015-03-15 16:30:00.000 2015-03-15 18:30:00.000
172376  NULL    4,5 2015-03-15 16:30:00.000 2015-03-15 18:30:00.000
172376  NULL    4,5 2015-03-15 16:30:00.000 2015-03-15 18:30:00.000
172377  NULL    4,5 2015-03-15 18:30:00.000 2015-03-15 20:30:00.000
172377  NULL    4,5 2015-03-15 18:30:00.000 2015-03-15 20:30:00.000
172377  NULL    4,5 2015-03-15 18:30:00.000 2015-03-15 20:30:00.000
172377  NULL    4,5 2015-03-15 18:30:00.000 2015-03-15 20:30:00.000
172377  NULL    4,5 2015-03-15 18:30:00.000 2015-03-15 20:30:00.000
172378  NULL    15,10,34,102,260    2015-03-09 06:00:00.000 2015-03-09 08:00:00.000
172378  NULL    15,10,34,102,260    2015-03-09 06:00:00.000 2015-03-09 08:00:00.000
172378  NULL    15,10,34,102,260    2015-03-09 06:00:00.000 2015-03-09 08:00:00.000

我需要的是locationKeyList带有给定条件的记录,locationKey也包含该列表

1 个答案:

答案 0 :(得分:1)

难道你不必像那样交叉申请吗?

CROSS APPLY
(
SELECT match
FROM DelimitedSplit8K('10,260,34,102,15', ',')
) as t(country)