我有一个名为BTSQMTAsk的表,它有两个任务。每个任务都有一个列CompletedID,是一个逗号分隔的列。我需要返回不在第二个任务中的任务的completedID(记住两者都是逗号分隔值)。请帮我找到这个答案。我被困在这个
TaskID TaskName CompletedID
1 Upload Signed SOW 1,2,3,5,4,9
2 Confirm Payment 1,5
在此,我必须比较两个任务的completedID,返回结果必须像'2,3,4,9'
- 输出
CompletedID
2,3,4,9
答案 0 :(得分:0)
尝试规范化表:分成两个表。它会让生活更轻松。
Table1 (TaskId, TaskName)
Table2 (TaskId, CompletedID)
Table2
TaskId CompletedId
1 1
1 2
1 3
1 5
1 4
1 9
2 1
2 5
然后查询变得更加容易:
SELECT CompletedId FROM Table2
WHERE TaskId = 1
AND CompletedId NOT IN
( SELECT CompletedId FROM Table2 WHERE TaskId = 2)
答案 1 :(得分:0)
此解决方案不需要您更改架构。
因此,使用2个解决方案中的任何一个来生成一个函数,该函数将从分隔的字符串返回列表:ToList() from Delimited String。然后做一些事情:
/* test data */
create table dbo.foo ( id integer, idlist varchar(100) )
insert into foo
select 1, '1,2,3,4,5'
union
select 2, '1,5'
/* end test data */
declare @arr varchar(100)
set @arr = ''
select @arr = @arr + a.arrValue + ','
from (
select count(x.arrValue) as cnt,
x.arrValue
from dbo.foo f
cross apply dbo.fnArray(f.idlist, ',') x
group by x.arrValue
having count(x.arrValue) = 1
) a
set @arr = left(@arr, len(@arr) - 1)
print @arr