tsql与计数有关

时间:2011-09-27 21:57:48

标签: tsql count having

我正在使用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 个答案:

答案 0 :(得分:3)

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

打破问题。

  1. 查找本季度有多个活动的人
  2. 获取上述子查询中找到的每个消费者的计划季度,事件名称和消费者
  3. 在此之后,子查询类似于

    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)
    

    我会想到如何在没有子查询的情况下优化它。

答案 1 :(得分: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)