tsql 不同的计数

发布于 2024-12-06 17:17:30 字数 608 浏览 0 评论 0原文

我正在使用 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 个或更多事件的次数?

I am using SSMS 2008 and am trying to select count of consumers who are part of two different events. Probably this is a simple query, but it is currently not returning expected count. Here is my T-SQL code which better explains what I tried:

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

So this query above returns 0 records. if I run following query, I get all records:

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

So I can see where some of these records are where there is same quarter, same person, but 2 or more events for that person. Now, how can I count number of times that same person is part of 2 or more events?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-12-13 17:17:30

注意:快速思考,所以我确信这可以优化:

分解问题。

  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)

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

NOTE: Quick off the top of my head, so I am sure this can be optimized:

Break the problem down.

  1. Find people who have more than one event for the quarter in question
  2. Get the program quarter, event name and consumer for each consumer found in the above subquery

Following this, the subquery is something like

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

And the full query something like:

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

I would then think of how to optimize this down without the subquery.

旧竹 2024-12-13 17:17:30

我已经使用您的结构创建了一些数据,这些数据将生成一些示例输出,如果您愿意,可以将其复制并粘贴到 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)

I've used your structure to create some data that will generate some example output, copy and paste into SSMS if you want to.

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

RESULTS

    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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文