3

我正在使用 SSMS 2008 并尝试选择属于两个不同事件的消费者数量。可能这是一个简单的查询,但它目前没有返回预期的计数。这是我的 T-SQL 代码,它更好地解释了我的尝试:

select [Program Quarter], event_name, consumer
from #consumer_initiations
where [Program Quarter] = 1  --and consumer = 'Byrd, Victoria Lynn'
group by [Program Quarter], event_name, consumer
having count (distinct event_name) > 1

所以上面的这个查询返回 0 条记录。如果我运行以下查询,我会得到所有记录:

select [Program Quarter], event_name, consumer
from #consumer_initiations
where [Program Quarter] = 1

所以我可以看到其中一些记录在哪里有相同的季度,相同的人,但该人有 2 个或更多事件。现在,我如何计算同一个人参与 2 个或更多事件的次数?

4

2 回答 2

3

注意:快速离开我的头顶,所以我相信这可以优化:

分解问题。

  1. 查找在相关季度举办多个活动的人
  2. 获取上述子查询中找到的每个消费者的节目季度、事件名称和消费者

在此之后,子查询类似于

SELECT DISTINCT Consumer
FROM #consumer_initiations
WHERE Count(event_name) > 1

完整的查询类似于:

SELECT [Program Quarter], event_name, consumer   
FROM #consumer_initiations  
WHERE consumer IN (SELECT DISTINCT Consumer
FROM #consumer_initiations
WHERE Count(event_name) > 1)

然后我会考虑如何在没有子查询的情况下优化它。

于 2011-09-27T22:14:05.687 回答
1

我已经使用您的结构创建了一些数据,这些数据将生成一些示例输出,如果您愿意,可以复制并粘贴到 SSMS 中。

declare @consumer_initiations table ([Program Quarter] int null, event_name varchar(100) null, consumer varchar(50) null)
insert into @consumer_initiations ([Program Quarter], event_name, consumer) values (1, 'Event 1', 'Byrd')
insert into @consumer_initiations ([Program Quarter], event_name, consumer) values (1, 'Event 2', 'Plane')
insert into @consumer_initiations ([Program Quarter], event_name, consumer) values (2, 'Event 3', 'Train')
insert into @consumer_initiations ([Program Quarter], event_name, consumer) values (3, 'Event 4', 'Stuff')
insert into @consumer_initiations ([Program Quarter], event_name, consumer) values (1, 'Event 5', 'Plane')

select [Program Quarter], event_name, consumer
from @consumer_initiations
where [Program Quarter] = 1  --and consumer = 'Byrd, Victoria Lynn'
group by [Program Quarter], event_name, consumer
having count
(distinct event_name) > 1

select [Program Quarter], event_name, consumer
from @consumer_initiations
where [Program Quarter] = 1

-- Possibly gets more information than needed

select consumer, [Program Quarter], count(event_name) as event_count
from @consumer_initiations
where [Program Quarter] = 1
group by [Program Quarter], consumer
having COUNT(event_name) > 1 

-- Just displays consumers with more than one event

select consumer, event_count from
(
select [Program Quarter], count(event_name) as event_count, consumer
from @consumer_initiations
where [Program Quarter] = 1
group by [Program Quarter], consumer
having COUNT(event_name) > 1 
) as subq

结果

    Program Quarter event_name                                                                                           consumer
--------------- ---------------------------------------------------------------------------------------------------- --------------------------------------------------

(0 row(s) affected)

Program Quarter event_name                                                                                           consumer
--------------- ---------------------------------------------------------------------------------------------------- --------------------------------------------------
1               Event 1                                                                                              Byrd
1               Event 2                                                                                              Plane
1               Event 5                                                                                              Plane

(3 row(s) affected)

consumer                                           Program Quarter event_count
-------------------------------------------------- --------------- -----------
Plane                                              1               2

(1 row(s) affected)

consumer                                           event_count
-------------------------------------------------- -----------
Plane                                              2

(1 row(s) affected)
于 2011-09-27T22:27:08.397 回答