SQL Server 2000,获取 COUNT(DISTINCT ID),条件是我无法写入 WHERE?

发布于 2024-08-23 21:57:55 字数 567 浏览 4 评论 0原文

首先,我不想使用“连接”,因为这会使我的查询更长且难以阅读。所以我需要做的必须是使用相同的 SELECT 语句。

myTable 中的列是 A、B、C、D、时间、ID 和 H

H columnd 告诉记录是“打开”还是“关闭”,这里是我的查询的样子。

SELECT 
A, 
B, 
C, 
D,
COUNT(DISTINCT ID) AS numberOfRecords,
SUM(time) / COUNT(DISTINCT ID) AS averageTimeOfAllRecords   
FROM myTable
WHERE ISNUMERIC(A)=1 AND A IN (SELECT A FROM myTable2)
GROUP BY A,B,C,D

我需要上面的查询返回带有结果的另一列: COUNT(DISTINCT ID) WHERE H = 'Open' 以便我可以获得 numberOfOpenRecords。

我无法将新条件写入“WHERE”,因为这会影响结果,例如 numberOfRecords。

希望我解释了我的问题。

感谢您的帮助。

First of all, I don't want to use a "join" because that will make my query longer and difficult to read. So what I need to do must be withing the same SELECT statement.

My columns in myTable are A, B , C , D , time, ID and H

H columnd tells if a record is 'Open' or 'Close', here how my query looks like.

SELECT 
A, 
B, 
C, 
D,
COUNT(DISTINCT ID) AS numberOfRecords,
SUM(time) / COUNT(DISTINCT ID) AS averageTimeOfAllRecords   
FROM myTable
WHERE ISNUMERIC(A)=1 AND A IN (SELECT A FROM myTable2)
GROUP BY A,B,C,D

I need the query above to return another column with a result: COUNT(DISTINCT ID) WHERE H = 'Open' so that I can get numberOfOpenRecords.

I can't write my new condition to my "WHERE", because that will effect the results, like numberOfRecords.

Hope I explained my problem.

Thanks for helps.

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

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

发布评论

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

评论(1

星軌x 2024-08-30 21:57:56

由于 count 不计算 null 值,您可以:

count(distinct case when H = 'Open' then id else null end)

Since count doesn't count null values, you could:

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