tsql 不同计数子查询

发布于 2024-12-07 22:52:35 字数 891 浏览 0 评论 0原文

我正在使用 SSMS 2008,我需要使用子查询来返回唯一记录/客户端的计数。我该怎么做?目前,我正在返回整个数据集的所有唯一记录,而不是每个客户端。这是我目前的伪代码:

    SELECT A.Program, A.PEOPLE_ID, K.EVENT_NAME, A.Program2, A.Program3
     (SELECT COUNT(DISTINCT K.EVENT_NAME) 
     FROM #TEMP1 A, evolv_cs.dbo.facility_view F, evolv_cs.dbo.people_x N, event_view K WITH (NOLOCK)
     WHERE F.group_profile_id = A.group_profile_id AND 
        K.event_definition_id = a.event_definition_id AND
        A.people_id = N.people_id
     GROUP BY K.EVENT_NAME) as DistinctEvent
     FROM #TEMP1 A
     JOIN event_view K WITH (NOLOCK) on K.event_definition_id = A.event_definition_id
     WHERE @START_DATE BETWEEN A.Enrolled_Date AND DATEADD(D, 14, A.Enrolled_Date)
     AND (@SERVICE IS NULL OR @SERVICE = K.event_name)
 GROUP BY 
A.Program, A.PEOPLE_ID, K.EVENT_NAME, A.Program2, A.Program3

好的,我现在编辑了上面的查询。我仍然想要每个客户端的 event_name 。

I am using SSMS 2008 and I need to use a subquery to return the count of unique records / client. How do I do this? Currently I am returning all unique records over the whole dataset and not per client. Here is my pseudocode currently:

    SELECT A.Program, A.PEOPLE_ID, K.EVENT_NAME, A.Program2, A.Program3
     (SELECT COUNT(DISTINCT K.EVENT_NAME) 
     FROM #TEMP1 A, evolv_cs.dbo.facility_view F, evolv_cs.dbo.people_x N, event_view K WITH (NOLOCK)
     WHERE F.group_profile_id = A.group_profile_id AND 
        K.event_definition_id = a.event_definition_id AND
        A.people_id = N.people_id
     GROUP BY K.EVENT_NAME) as DistinctEvent
     FROM #TEMP1 A
     JOIN event_view K WITH (NOLOCK) on K.event_definition_id = A.event_definition_id
     WHERE @START_DATE BETWEEN A.Enrolled_Date AND DATEADD(D, 14, A.Enrolled_Date)
     AND (@SERVICE IS NULL OR @SERVICE = K.event_name)
 GROUP BY 
A.Program, A.PEOPLE_ID, K.EVENT_NAME, A.Program2, A.Program3

OK, I edited the above query now. I still want event_name per client.

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

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

发布评论

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

评论(1

您要在此处使用的是用于不同计数的 GROUP BY 子句,而不是连接的子查询。以下两个查询应该可以满足您的需求。

SELECT A.PEOPLE_ID, COUNT(DISTINCT K.EVENT_NAME)
   FROM #TEMP1 A, event_view K WITH (NOLOCK)
   WHERE K.event_definition_id = a.event_definition_id
         AND @START_DATE BETWEEN A.Enrolled_Date AND DATEADD(D, 14, A.Enrolled_Date)
         AND (@SERVICE IS NULL OR @SERVICE = K.event_name)
   GROUP BY A.PEOPLE_ID

SELECT K.EVENT_NAME, COUNT(DISTINCT A.PEOPLE_ID)
   FROM #TEMP1 A, event_view K WITH (NOLOCK)
   WHERE K.event_definition_id = a.event_definition_id
         AND @START_DATE BETWEEN A.Enrolled_Date AND DATEADD(D, 14, A.Enrolled_Date)
         AND (@SERVICE IS NULL OR @SERVICE = K.event_name)
   GROUP BY K.EVENT_NAME

如果您需要将查询结果合并到一个结果集中,您应该可以使用 UNION 来实现。

What you want to use here is the GROUP BY clause for your distinct counts, instead of the joined subqueries. The following two queries should give you what you want.

SELECT A.PEOPLE_ID, COUNT(DISTINCT K.EVENT_NAME)
   FROM #TEMP1 A, event_view K WITH (NOLOCK)
   WHERE K.event_definition_id = a.event_definition_id
         AND @START_DATE BETWEEN A.Enrolled_Date AND DATEADD(D, 14, A.Enrolled_Date)
         AND (@SERVICE IS NULL OR @SERVICE = K.event_name)
   GROUP BY A.PEOPLE_ID

SELECT K.EVENT_NAME, COUNT(DISTINCT A.PEOPLE_ID)
   FROM #TEMP1 A, event_view K WITH (NOLOCK)
   WHERE K.event_definition_id = a.event_definition_id
         AND @START_DATE BETWEEN A.Enrolled_Date AND DATEADD(D, 14, A.Enrolled_Date)
         AND (@SERVICE IS NULL OR @SERVICE = K.event_name)
   GROUP BY K.EVENT_NAME

If you need to combine the results of the queries into a single result set, you should be able to do so with a UNION.

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