Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql没有返回正确的计数_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

sql没有返回正确的计数

sql没有返回正确的计数,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我有如下sql表hr\U注册人类别: RegistrantID CategoryID 1 47 4 276 4 275 4 278 4 274 4 277 16276 276 16276 275 16276 278 16295 276 16295 275 16295 278 16295 274 16295 277 16292 276 16292 275 16292 278 16292 274 16292 277 1

我有如下sql表hr\U注册人类别:

RegistrantID    CategoryID
1   47
4   276
4   275
4   278
4   274
4   277
16276   276
16276   275
16276   278
16295   276
16295   275
16295   278
16295   274
16295   277
16292   276
16292   275
16292   278
16292   274
16292   277
16293   276
16293   275
16293   278
16293   274
16293   277
16294   276
16294   275
16294   278
16294   274
16294   277
16303   276
16303   275
16303   278
16303   274
16303   277
16302   276
16302   275
16302   278
16302   274
16302   277
16303   276
16303   275
16303   278
16303   274
16303   277
16304   276
16304   275
16304   278
16304   274
16304   277
26  276
26  275
26  278
26  274
26  277
16305   276
16305   275
16305   278
16305   274
16305   277
29  276
29  275
29  278
29  274
29  277
16306   276
16306   275
16306   278
16306   274
16306   277
16306   276
16306   275
16306   278
16306   274
16306   277
16307   276
16307   275
16307   278
16307   274
16307   277
16307   276
16307   275
16307   278
16307   274
16307   277
CategoryID
1
2
3
4
5
6
...
..
..
700
另一个表格hr_分类如下:

RegistrantID    CategoryID
1   47
4   276
4   275
4   278
4   274
4   277
16276   276
16276   275
16276   278
16295   276
16295   275
16295   278
16295   274
16295   277
16292   276
16292   275
16292   278
16292   274
16292   277
16293   276
16293   275
16293   278
16293   274
16293   277
16294   276
16294   275
16294   278
16294   274
16294   277
16303   276
16303   275
16303   278
16303   274
16303   277
16302   276
16302   275
16302   278
16302   274
16302   277
16303   276
16303   275
16303   278
16303   274
16303   277
16304   276
16304   275
16304   278
16304   274
16304   277
26  276
26  275
26  278
26  274
26  277
16305   276
16305   275
16305   278
16305   274
16305   277
29  276
29  275
29  278
29  274
29  277
16306   276
16306   275
16306   278
16306   274
16306   277
16306   276
16306   275
16306   278
16306   274
16306   277
16307   276
16307   275
16307   278
16307   274
16307   277
16307   276
16307   275
16307   278
16307   274
16307   277
CategoryID
1
2
3
4
5
6
...
..
..
700
但当我使用下面的查询时,它在结果中返回相同的计数

SELECT  COUNT(RC.RegistrantID)
            FROM dbo.hr_Registrants R INNER JOIN dbo.hr_RegistrantCategories RC ON R.RegistrantID = RC.RegistrantID
            WHERE R.Deleted = 0 
            AND RC.CategoryID IN (SELECT CategoryID FROM dbo.hr_Categories)


            SELECT C.CategoryID,
            (
           SELECT  COUNT(DISTINCT RC.RegistrantID)
            FROM dbo.hr_Registrants R INNER JOIN dbo.hr_RegistrantCategories RC ON R.RegistrantID = RC.RegistrantID
            WHERE R.Deleted = 0 
            AND RC.CategoryID IN (SELECT CategoryID FROM dbo.hr_Categories C1 WHERE C1.Deleted = 0)


        )AS RegistrantsCount
        FROM dbo.hr_Categories C
结果是:

CategoryID  RegistrantsCount
1   13
2   13
3   13
4   13
5   13
6   13
7   13
8   13

内部查询与外部查询不相关,因此它为每行返回相同的值

SELECT C.CategoryID,
        (
       SELECT  COUNT(DISTINCT RC.RegistrantID)
        FROM dbo.hr_Registrants R INNER JOIN dbo.hr_RegistrantCategories RC ON R.RegistrantID = RC.RegistrantID
        WHERE R.Deleted = 0 
        AND RC.CategoryID IN (SELECT CategoryID FROM dbo.hr_Categories C1 WHERE C1.Deleted = 0)

        AND RC.CategoryID = C.CategoryID -- correlate with the outer query

)AS RegistrantsCount
    FROM dbo.hr_Categories C

我认为这个查询符合您的要求。我使用的是
LEFT JOIN
,因为我想您也想知道计数是否为0?如果没有,则可以将其设置为
内部联接

SELECT C.CategoryID, COUNT(RC.RegistrantID) AS RegistrantsCount
    FROM dbo.hr_categories C
    LEFT JOIN dbo.RegistrantCategories RC
        ON RC.CategoryID = C.CategoryID
    LEFT JOIN dbo.hr_Registrants R
        ON R.RegistrantID = RC.RegistrantID
        AND R.Deleted = 0
    WHERE C.Deleted = 0
    GROUP BY C.CategoryID

不管它在做什么。。。您希望它做什么?按hr_类别表的类别分组注册人您能解释一下这个答案的错误吗?我真的很想知道,为了修复(或删除)itI没有否决投票,但我猜这是因为所有子查询都可能被认为是次优的,而group by可能更好。谢谢@BobVale。但我认为优化查询的唯一方法是在真实数据上对其进行分析,并查看执行计划。例如,查询优化器将子查询转换为哈希匹配的频率令人惊讶