选择Count multiple table sql server返回1值

时间:2015-06-18 02:45:32

标签: 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       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

来自table3的select计数运行良好,但是对于count table2,只返回每行的1值。我需要table4中的文件名组的filecount组,怎么做?我在子查询上有错误。

4 个答案:

答案 0 :(得分:0)

请改为尝试:

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

或者您可以使用CROSS APPLY

SQL Fiddle

SELECT 
    t1.*,
    t2.note_count,
    t3.po_count
FROM table1 t1
CROSS APPLY(
    SELECT note_count = COUNT(*)
    FROM table2
    WHERE UserId2 = t1.ID
)t2
CROSS APPLY(
    SELECT po_count = COUNT(*)
    FROM table3
    WHERE UserId3 = t1.ID
)t3

答案 1 :(得分:0)

这里有很多问题: -

  1. 您不需要在两个子查询中使用GROUP BY语句。
  2. 子查询的ID子句中的WHERE应该设置别名,以便它们与table1正确关联。
  3. 例如

    SELECT ID
       ,UserId
       ,Username
       ,(SELECT count(DISTINCT table2.ID)
           FROM table2
          WHERE table2.UserId2 = t.ID) AS note_count
       ,(SELECT count(DISTINCT table3.ID)
           FROM table3
          WHERE table3.UserId3 = t.ID
          ) AS PO_count
    FROM table1 t
    ORDER BY UserId 
    
    1. 作为惯例,最好保持表和列名称的大小一致。如果要在区分大小写的SQL Server安装上安装查询,则此查询将无法编译。

答案 2 :(得分:0)

我不明白为什么你要做count(DISTINCT 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

另一方面,如果例如User 1可以多次使用test1,但您只需要testname的非重复计数,则可以将test_count查询更改为

(SELECT COUNT(DISTINCT testname)
    FROM table3
    WHERE UserId3 = u.Id) AS test_count,

文件和备注相同

答案 3 :(得分:0)

尝试以下

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