TSQL 不同记录计数

发布于 2024-12-06 20:39:35 字数 2404 浏览 0 评论 0原文


我正在使用 SSMS 2008,并试图从我的表中获取 Event_names 的不同计数。但相反,当实际有 2 条记录和其他无效值时,它会返回 count = 1。我想要的是他们所属的不同 event_names 数量的总计数/消费者。此外,同一消费者的某些事件计数是不同的。但我想要一个不同的计数/消费者。以下是我的无效查询返回的部分数据:

消费者计划注册日期 event_name Event_Ct

B, Tiffany  2010-09-27 12:00:00.000 Comprehensive Clinical Assessment   1
B, Tiffany  2010-09-27 00:00:00.000 Telemedicine Comprehensive Clinical Assessment  1
B, Nickolas Tyan    2010-12-07 15:00:00.000 Comprehensive Clinical Assessment   1
B, Nickolas Tyan    2010-12-07 00:00:00.000 Telemedicine Comprehensive Clinical Assessment  1
B, Jack 2011-06-13 08:30:00.000 Comprehensive Clinical Assessment   1
B, Jack 2011-01-03 00:00:00.000 Medication Management   1
B, Victoria Lynn    2010-11-10 00:00:00.000 Telemedicine Comprehensive Clinical Assessment  3
B, Victoria Lynn    2010-12-28 00:00:00.000 Telemedicine Psychiatric Assessment 3
B, Victoria Lynn    2011-01-07 00:00:00.000 Telemedicine Psychiatric Progress Note  2
B, Victoria Lynn    2011-02-08 00:00:00.000 Telemedicine Psychiatric Progress Note  2

这是我用于上述操作的 T-SQL:

SELECT consumer, [Program Enrollment Date], event_name, [Program Quarter]
INTO #INITIATIONS
FROM #consumer_initiations   
WHERE consumer IN 
(SELECT DISTINCT Consumer 
FROM #consumer_initiations
GROUP BY Consumer 
HAVING Count(DISTINCT event_name) > 1)
ORDER BY consumer, event_name

SELECT A.consumer, A.[Program Enrollment Date], A.event_name, count(A.event_name)
FROM #INITIATIONS A 
JOIN #INITIATIONS B ON A.consumer = B.consumer AND A.event_name <> B.event_name AND A.[Program Enrollment Date] <> B.[Program Enrollment Date]
GROUP BY A.consumer, A.event_name, a.[Program Enrollment Date]

我也尝试了此查询,它为只有 2 条记录的消费者返回了正确的计数,但是对于那些> 2 条记录,它返回的计数太高:

SELECT A.consumer, count(A.event_name)
FROM #INITIATIONS A 
JOIN #INITIATIONS B ON A.consumer = B.consumer AND A.event_name <> B.event_name AND A.[Program Enrollment Date] <> B.[Program Enrollment Date]
GROUP BY A.consumer

感谢 Chris,这是有效的解决方案:

Select I.consumer, [Program Enrollment Date], event_name, countPerConsumer.[Number of Events] 
From #INITIATIONS i 
Join (Select Consumer, count(distinct event_name) as [Number of Events] From #INITIATIONS GROUP BY Consumer) countPerConsumer  
    on countPerConsumer.Consumer 

= i.consumer


I am using SSMS 2008 and am trying to get a distinct count of Event_names from my table. But instead, it is returning count = 1 when there are actually 2 records and other invalid values. What I want is the total count / consumer for their number of distinct event_names they are part of. Also, some of the Event Counts for the same consumer are different. But I want one distinct count / consumer. Here is a portion of the data returned by my invalid query:

consumer Program Enrollment Date event_name Event_Ct

B, Tiffany  2010-09-27 12:00:00.000 Comprehensive Clinical Assessment   1
B, Tiffany  2010-09-27 00:00:00.000 Telemedicine Comprehensive Clinical Assessment  1
B, Nickolas Tyan    2010-12-07 15:00:00.000 Comprehensive Clinical Assessment   1
B, Nickolas Tyan    2010-12-07 00:00:00.000 Telemedicine Comprehensive Clinical Assessment  1
B, Jack 2011-06-13 08:30:00.000 Comprehensive Clinical Assessment   1
B, Jack 2011-01-03 00:00:00.000 Medication Management   1
B, Victoria Lynn    2010-11-10 00:00:00.000 Telemedicine Comprehensive Clinical Assessment  3
B, Victoria Lynn    2010-12-28 00:00:00.000 Telemedicine Psychiatric Assessment 3
B, Victoria Lynn    2011-01-07 00:00:00.000 Telemedicine Psychiatric Progress Note  2
B, Victoria Lynn    2011-02-08 00:00:00.000 Telemedicine Psychiatric Progress Note  2

And here is the T-SQL I used for the above:

SELECT consumer, [Program Enrollment Date], event_name, [Program Quarter]
INTO #INITIATIONS
FROM #consumer_initiations   
WHERE consumer IN 
(SELECT DISTINCT Consumer 
FROM #consumer_initiations
GROUP BY Consumer 
HAVING Count(DISTINCT event_name) > 1)
ORDER BY consumer, event_name

SELECT A.consumer, A.[Program Enrollment Date], A.event_name, count(A.event_name)
FROM #INITIATIONS A 
JOIN #INITIATIONS B ON A.consumer = B.consumer AND A.event_name <> B.event_name AND A.[Program Enrollment Date] <> B.[Program Enrollment Date]
GROUP BY A.consumer, A.event_name, a.[Program Enrollment Date]

I also tried this query, which returned correct counts for consumers with only 2 records, but for those with > 2 records, it returned too high of counts:

SELECT A.consumer, count(A.event_name)
FROM #INITIATIONS A 
JOIN #INITIATIONS B ON A.consumer = B.consumer AND A.event_name <> B.event_name AND A.[Program Enrollment Date] <> B.[Program Enrollment Date]
GROUP BY A.consumer

Thanks to Chris, here is the solution that worked:

Select I.consumer, [Program Enrollment Date], event_name, countPerConsumer.[Number of Events] 
From #INITIATIONS i 
Join (Select Consumer, count(distinct event_name) as [Number of Events] From #INITIATIONS GROUP BY Consumer) countPerConsumer  
    on countPerConsumer.Consumer 

= i.consumer

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

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

发布评论

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

评论(1

深爱不及久伴 2024-12-13 20:39:35

我想我已经明白你想要做什么。如果没有,我表示歉意。像这样的东西应该对您有帮助:

Select consumer, [Program Enrollment Date], event_name, countPerConsumer.[Number of Events]
From #INITIATIONS i
Join (Select Consumer, count(distinct event_name) as [Number of Events] 
        group by Consumer)countPerConsumer 
    on countPerConsumber.consumer = i.consumer

这个想法是您加入一个根据每个消费者的不同事件计数而构建的表。您应该能够保留构建 #INITIATIONS 的部分,并将最后一个查询替换为上面的查询。希望这有帮助。

I think I've grasped what your trying to do. If not I appolgize. something like this should help you:

Select consumer, [Program Enrollment Date], event_name, countPerConsumer.[Number of Events]
From #INITIATIONS i
Join (Select Consumer, count(distinct event_name) as [Number of Events] 
        group by Consumer)countPerConsumer 
    on countPerConsumber.consumer = i.consumer

the idea is your joining a table built on the fly of a count of distinct events for each consumer. you should be able to keep the piece where you build #INITIATIONS and just replace the last query with the above. hope this helps.

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