简体   繁体   English

Access SQL:Having子句

[英]Access SQL: Having clause

I am trying to get a distinct count of users who have a repeat rate greater than one (in Excel for example I would use the countif formula to do this). 我试图获得重复率大于1的用户的不同数量(例如,在Excel中,我将使用countif公式来执行此操作)。

I am using Ms-access (2016) and can't seem to get this to work. 我正在使用Ms-access(2016),似乎无法正常工作。 The results of the following query gives me the same results for the count of userName and Repeat Rate. 以下查询的结果为我提供了相同的用户名和重复率计数结果。

SELECT host, department, count([userName]) AS ['Distinct Users'], sum(Logins) AS ['Total Logins'], count([Repeat Rate]) AS ['Repeat Users']
FROM (SELECT DISTINCT department, [userName], host,[Repeat Rate], Logins FROM Sheet1)  AS x
GROUP BY department, host
ORDER BY host, department,
HAVING COUNT([Repeat Rate]) > 1;

Any help is greatly appreciated, thanks! 非常感谢任何帮助,谢谢!

edit: 编辑:

+------+----------+------------+-------------+--+
| host | username | Department | Repeat Rate |  |
+------+----------+------------+-------------+--+
| x    | Kyle     | D1         |           1 |  |
| x    | Max      | D1         |           4 |  |
| x    | Will     | D1         |           2 |  |
| x    | Will     | D1         |           2 |  |
+------+----------+------------+-------------+--+

With the above table for example, I would want it to say for host x in Department D1, there are 3 distinct users, and 2 users with a repeat rate greater than 1. 以上面的表为例,我想对部门D1中的主机x说,有3个不同的用户,和2个重复率大于1的用户。

+------+------------+----------------+-------------+
| host | Department | Distinct Users | Repeat Rate |
+------+------------+----------------+-------------+
| x    | D1         |              3 |           2 |
+------+------------+----------------+-------------+

Try this 尝试这个

SELECT host, department, count([userName]) AS ['Distinct Users'], sum(IIF([Repeat Rate]>1,1,0)) AS ['Repeat Users']
FROM (SELECT DISTINCT host, department, [userName], [Repeat Rate] FROM Sheet1)  AS x
GROUP BY department, host;

You might try something like this: 您可以尝试如下操作:

SELECT x.host, x.department, max(users.unique_users) AS ['Distinct Users'], 
    sum(Logins) AS ['Total Logins'], max(repeats.unique_repeats) AS ['Repeat Users']
FROM (SELECT DISTINCT department, [userName], host,[Repeat Rate], Logins FROM Sheet1)  AS x
left join (select host, department, count(userName) as unique_users from (select distinct 
    host, department, [userName] from Sheet1)) as users on users.host = x.host and 
    users.department = x.department
left join (select host, department, count(userName) as unique_repeats 
    from (select distinct host, department, [userName] from Sheet1 where [Repeat Rate] > 1)) 
        as repeats on users.host = x.host and users.department = x.department
GROUP BY department, host
ORDER BY host, department;

Counting unique values is difficult in Access SQL. 在Access SQL中很难计算唯一值。 Normally you would use count(distinct var) as the other suggest, but that is not available to you. 通常,您会使用count(distinct var)作为其他建议,但这对您不可用。

I'm not sure about the join you have shown in your post first part, but, considering your example only, the following query seems to resolve your problem 我不确定您在帖子第一部分中显示的联接,但是,仅考虑您的示例,以下查询似乎可以解决您的问题


SELECT HOST, DEPARTMENT, SUM(DISTINCT_USER) AS DISTINCT_USER, MAX(CNT) AS REPEAT_RATE
FROM (
    SELECT HOST, DEPARTMENT, COUNT(DISTINCT USERNAME) AS DISTINCT_USER, REPEAT_RATE, COUNT(*) AS CNT
    FROM 
    GROUP BY HOST, DEPARTMENT, REPEAT_RATE
) A
GROUP BY HOST, DEPARTMENT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM