简体   繁体   English

equals运算符与in运算符之间的SQL计数函数差异

[英]SQL count function difference between equals operator and in operator

This question is related to following hackerrank challenge: https://www.hackerrank.com/challenges/challenges/problem 此问题与以下hackerrank挑战有关: https ://www.hackerrank.com/challenges/challenges/problem

The version that works returns the correct result set while the other returns an incomplete result set since count(*) does not work as expected with in clause 有效的版本返回正确的结果集,而另一个版本返回不完整的结果集,因为count(*)不能按预期的in子句运行

Only difference between the two code pieces is c_count instead of count(*) in the first clause of or. 这两个代码段之间唯一的区别是c_count而不是or的第一个子句中的count(*)。

Since c_count is equal to count(*), there should be no semantic difference. 由于c_count等于count(*),因此应该没有语义上的区别。 If count(*) in is wrong syntax, why does count(*) = work? 如果count(*) in的语法错误,为什么count(*) =有效?

This works: 这有效:

select count(*) as c_count ... having c_count in (select ...) or count(*) = (select ...);

This won't work: 这行不通:

select count(*) as c_count ... having count(*) in  (select ...) or count(*) = (select ...);

Here's the original queries. 这是原始查询。 /**/ comment calls out the difference between the two: /**/ comment指出两者之间的区别:

Working: 工作方式:

SELECT h.hacker_id
    ,h.name
    ,count(*) AS c_count
FROM Hackers h
JOIN Challenges c ON h.hacker_id = c.hacker_id
GROUP BY h.hacker_id, h.name
HAVING 
    /*USING c_count ALIAS*/ 
    c_count IN (
        SELECT hacker_c_counts2.c_count2
        FROM (
            SELECT count(*) AS c_count2
            FROM Challenges c2
            GROUP BY c2.hacker_id
            ) AS hacker_c_counts2
        GROUP BY hacker_c_counts2.c_count2
        HAVING count(*) = 1
        )
    OR count(*) = (
        SELECT max(hacker_c_counts.c_count1)
        FROM (
            SELECT count(*) AS c_count1
            FROM Challenges c1
            GROUP BY c1.hacker_id
            ) AS hacker_c_counts
        )
ORDER BY count(*) DESC,  h.hacker_id;

Not Working: 不起作用:

SELECT h.hacker_id
    ,h.name
    ,count(*) AS c_count
FROM Hackers h
JOIN Challenges c ON h.hacker_id = c.hacker_id
GROUP BY h.hacker_id, h.name
HAVING 
    /*USING count(*) directly*/ 
    count(*) IN (
        SELECT hacker_c_counts2.c_count2
        FROM (
            SELECT count(*) AS c_count2
            FROM Challenges c2
            GROUP BY c2.hacker_id
            ) AS hacker_c_counts2
        GROUP BY hacker_c_counts2.c_count2
        HAVING count(*) = 1
        )
    OR count(*) = (
        SELECT max(hacker_c_counts.c_count1)
        FROM (
            SELECT count(*) AS c_count1
            FROM Challenges c1
            GROUP BY c1.hacker_id
            ) AS hacker_c_counts
        )
ORDER BY count(*) DESC, h.hacker_id;

Try this .....having h.c_count in..... Use the table Alias "h" before the c_count. 尝试此.....具有h.c_count。....在c_count之前使用表别名“ h”。

Good luck. 祝好运。

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

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