在连接的基础上从不同的表中选择记录计数

时间:2017-10-06 23:59:33

标签: sql-server join group-by count multiple-tables

我想获取用户在不同表中输入的记录数。 DB的架构是:

+-----------------------+  
| Survey Master         |  
+----------------+------+   
| Field          | Key  |  
+----------------+------+  
| id             | PK   |  
| Username       |      |
| FamilyMasterId | FK   |
+----------------+------+

+------------+------+  
| Family Master     |  
+------------+------+   
| Field      | Key  |  
+------------+------+  
| id         | PK   |   
+------------+------+

+-----------------------+  
| Family Detail         |  
+----------------+------+   
| Field          | Key  |  
+----------------+------+  
| id             | PK   | 
| FamilyMasterId | FK   |   
+----------------+------+

+-----------------------+  
| Travel Master         |  
+----------------+------+   
| Field          | Key  |  
+----------------+------+  
| id             | PK   | 
| FamilyDetailId | FK   |   
+----------------+------+

+-----------------------+  
| Travel Detail         |  
+----------------+------+   
| Field          | Key  |  
+----------------+------+  
| id             | PK   | 
| TravelMasterId | FK   |   
+----------------+------+

我希望看到每个用户在每个表中创建的记录数量如下:

  Username   SurveyMaster   FamilyMaster   FamilyDetail   TravelMaster   TravelDetail  
 ---------- -------------- -------------- -------------- -------------- -------------- 
  User001    59             47             36             26             12            
  User002    88             76             64             42             25            
  User003    49             44             35             25             15            
  User004    77             69             55             45             37  

查看以下链接后:

  1. Find Records from Different Tables
  2. Select count(*) from multiple tables
  3. http://www.sqlines.com/mysql/how-to/join-different-tables-based-on-condition
  4. http://www.informit.com/articles/article.aspx?p=30875&seqNum=5
  5. SQL: Combine Select count(*) from multiple tables
  6. 我能够编写此查询但它在所有列中提供相同的记录:

    SELECT USERNAME, COUNT(USERNAME) SURVEYMASTER, COUNT(USERNAME) FAMILYMASTER, COUNT(USERNAME) FAMILYDETAIL, COUNT(USERNAME) TRAVELMASTER, COUNT(USERNAME) TRAVELDETAIL FROM 
    ((SELECT CREATEUSER USERNAME FROM SURVEYMASTER
    ) 
    UNION ALL
    (SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
    INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
    ) 
    UNION ALL
    (SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
    INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
    INNER JOIN FAMILYDETAIL FD ON FM.ID = FD.FAMILYMASTERID
    )
    UNION ALL
    (SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
    INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
    INNER JOIN FAMILYDETAIL FD ON FM.ID = FD.FAMILYMASTERID
    INNER JOIN TRAVELMASTER TM ON FD.ID = TM.FAMILYDETAILID
    )
    UNION ALL
    (SELECT SM.CREATEUSER USERNAME FROM SURVEYMASTER SM
    INNER JOIN FAMILYMASTER FM ON FM.ID = SM.FAMILYMASTERID
    INNER JOIN FAMILYDETAIL FD ON FM.ID = FD.FAMILYMASTERID
    INNER JOIN TRAVELMASTER TM ON FD.ID = TM.FAMILYDETAILID
    INNER JOIN TRAVELDETAIL TD ON TM.ID = TD.TRAVELMASTERID
    )
    ) T
    GROUP BY USERNAME
    ORDER BY USERNAME
    

    修改

    以下是关系描述:

    1. FamilyMasterId是SurveyMaster和FamilyDetail中的外键 表。
    2. FamilyDetailId是TravelMaster表中的外键。
    3. TravelMasterId是TravelDetail表中的外键。

1 个答案:

答案 0 :(得分:1)

如果我们考虑性能但是它可以提供所需的结果,这可能不是完美的解决方案

SELECT  sm.Username ,
        COUNT(*) SurveyMaster ,
        COUNT(FamilyMasterId) FamilyMaster ,
        fd.FamilyDetail ,
        tm.TravelMaster ,
        td.TravelDetail
FROM    SurveyMaster sm
        JOIN ( SELECT   Username ,
                        COUNT(fd.id) FamilyDetail
               FROM     SurveyMaster sm
                        JOIN FamilyMaster fm ON sm.FamilyMasterId = fm.Id
                        JOIN FamilyDetail fd ON fm.id = fd.FamilyMasterId
               GROUP BY Username
             ) fd ON sm.Username = fd.Username
        JOIN ( SELECT   Username ,
                        COUNT(tm.id) TravelMaster
               FROM     SurveyMaster sm
                        JOIN FamilyMaster fm ON sm.FamilyMasterId = fm.Id
                        JOIN FamilyDetail fd ON fm.id = fd.FamilyMasterId
                        JOIN TravelMaster tm ON fd.Id = tm.FamilyDetailId
               GROUP BY Username
             ) tm ON sm.Username = tm.Username
        JOIN ( SELECT   Username ,
                        COUNT(td.id) TravelDetail
               FROM     SurveyMaster sm
                        JOIN FamilyMaster fm ON sm.FamilyMasterId = fm.Id
                        JOIN FamilyDetail fd ON fm.id = fd.FamilyMasterId
                        JOIN TravelMaster tm ON fd.Id = tm.FamilyDetailId
                        JOIN TravelDetail td ON tm.Id = td.TravelMasterId
               GROUP BY Username
             ) td ON sm.Username = td.Username
GROUP BY sm.Username ,
        fd.FamilyDetail ,
        tm.TravelMaster ,
        td.TravelDetail;