表格模式具有单列,其中包含以下值:
NewsletteridPattern
------------
%50%
%51%
表B具有以下值:
SubscriberId NewsletterIdCsv
------------ -----------------
47421584 51
45551047 50,51
925606902 50
47775985 51
我有以下查询,它基本上使用模式计算逗号分隔值:
SELECT *
FROM TABLEB t WITH (nolock)
JOIN Patterns p ON (t.NewsletteridPattern LIKE p.pattern)
问题是计数不正确,例如我的模式有%50%和%51%,因此表B中的行号2应该被计算两次,但是我的查询只有一次,我该如何那个?
编辑:
我忘了在原始查询中添加DISTINCT导致问题:
SELECT Count(Distinct Subscriberid)
FROM TABLEB t WITH (nolock)
JOIN Patterns p ON (t.NewsletteridPattern LIKE p.pattern)
答案 0 :(得分:1)
我嘲笑你的数据:
create table #pattern (pattern varchar(50))
insert into #pattern values ('%50%')
insert into #pattern values ('%51%')
create table #subscriber (id varchar(50), newsletter varchar(50))
insert into #subscriber values ('47421584', '51')
insert into #subscriber values ('45551047', '50,51')
insert into #subscriber values ('925606902', '50')
insert into #subscriber values ('47775985', '51')
SELECT pattern, COUNT(*) AS Counter
FROM #subscriber t WITH (nolock)
JOIN #pattern p ON (t.newsletter LIKE p.pattern)
GROUP BY pattern
我的select语句返回:
pattern Counter
------- -------
%50% 2
%51% 3
你的最终目标是什么?您是否只关心按模式计算行数,还是尝试按模式选择行?