Sql server 选择计数多表sql server返回1值
我的多重计数有问题 这是我的桌子Sql server 选择计数多表sql server返回1值,sql-server,Sql Server,我的多重计数有问题 这是我的桌子 table1 ID UserId Username 1 a aa 2 b bb 3 c cc 4 d dd table2 ID UserId2 note 1 1 n1 2 1 n2 3 2 n3 4 2 n4 table3 ID UserId3 testname 1 1 test1 2 1
table1
ID UserId Username
1 a aa
2 b bb
3 c cc
4 d dd
table2
ID UserId2 note
1 1 n1
2 1 n2
3 2 n3
4 2 n4
table3
ID UserId3 testname
1 1 test1
2 1 test2
3 2 test3
4 2 test4
5 3 test 5
table4
ID UserId filename
1 1 filea
2 1 fileb
3 2 filec
4 2 filed
我有这样的疑问
select ID, userID, username,
(select count(distinct table2.ID) from table2 where table2.userID2 = ID group by userID,username) as note_count ,
(select count(distinct table3.ID) from table3 where table3.userID3 = ID group by userID,username) as PO_count,
select filename from table4 where table3.userID = table1.ID group by filename) as FileCount
from table1 ORDER BY userID
表3中的select count运行良好,但对于count table2,每行只返回1个值。我需要表4中按文件名列出的filecount组,怎么做?子查询出错。请尝试以下操作:
SELECT
t1.*,
note_count = ISNULL(DISTINCT COUNT(t2.ID), 0),
PO_count = ISNULL(DISTINCT COUNT(t3.ID), 0)
FROM table1 t1
LEFT JOIN table2 t2
ON t2.UserId2 = t1.ID
LEFT JOIN table3 t3
ON t3.UserId3 = t1.ID
GROUP BY
t1.ID, t1.UserId, t1.Username
ORDER BY t1.UserId
或者您可以使用交叉应用:
这里有一些问题:- 在两个子查询中,实际上不需要GROUPBY语句。 子查询的WHERE子句中的ID应具有别名,以便它们正确链接到表1。 乙二醇 按照惯例,保持表和列名的大小写一致是一个好主意。如果要在SQL Server的区分大小写安装上安装查询,则不会编译此查询。
我不明白你为什么要使用countDISTINCT ID。这应该是你的密钥,而且每个记录都不同。这会让你需要这个
SELECT
u.ID,
u.UserName,
(SELECT COUNT(*)
FROM table2
WHERE UserId2 = u.Id) AS note_count,
(SELECT COUNT(*)
FROM table3
WHERE UserId3 = u.Id) AS test_count,
(SELECT COUNT(*)
FROM table4
WHERE UserId4 = u.Id) AS file_count
FROM
table1 u
另一方面,例如,如果用户1可以多次使用test1,但您只需要testname的不同计数,那么您可以将test\u count查询更改为
(SELECT COUNT(DISTINCT testname)
FROM table3
WHERE UserId3 = u.Id) AS test_count,
文件和便笺也一样请尝试以下操作
SELECT
ID,
userID,
username,
(SELECT COUNT(t2.ID) FROM table2 t2 WHERE t2.userID2 = t1.ID) AS note_count,
(SELECT COUNT(t3.ID) FROM table3 t3 WHERE t3.userID3 = t1.ID) AS PO_count,
LTRIM(ISNULL(STUFF((SELECT
', ' + filename + ' (' + CAST(COUNT(filename) AS VARCHAR) + ')'
FROM
table4 t4
WHERE
t4.userID = t1.ID
GROUP BY
filename
FOR XML PATH('')
)
, 1, 1,''),'-'))
AS FileCount
FROM
table1 t1
ORDER BY
t1.UserId
我认为子查询中不需要GROUP BY,我建议您使用限定ID。这适用于两个子查询,但例如:从table3中选择countdistinct table3.ID,其中table3.userID3=table1.ID作为PO_countthis nice,但我需要从table4中选择countfilename GROUP BY filename,我需要按文件名对组进行计数求和。请尝试更新的查询。检查它是否有用。如果您不想这样做,您需要在外部查询中使用JOIN with Filecount查询。但将显示重复的用户详细信息行。
(SELECT COUNT(DISTINCT testname)
FROM table3
WHERE UserId3 = u.Id) AS test_count,
SELECT
ID,
userID,
username,
(SELECT COUNT(t2.ID) FROM table2 t2 WHERE t2.userID2 = t1.ID) AS note_count,
(SELECT COUNT(t3.ID) FROM table3 t3 WHERE t3.userID3 = t1.ID) AS PO_count,
LTRIM(ISNULL(STUFF((SELECT
', ' + filename + ' (' + CAST(COUNT(filename) AS VARCHAR) + ')'
FROM
table4 t4
WHERE
t4.userID = t1.ID
GROUP BY
filename
FOR XML PATH('')
)
, 1, 1,''),'-'))
AS FileCount
FROM
table1 t1
ORDER BY
t1.UserId