SQL获取参与调查的用户数

时间:2012-05-03 05:19:26

标签: sql stored-procedures

我想知道如何编写SQL查询以获取参与每个月调查的用户数量

表结构如下所示

SurveyTable

SurveyId, SurveyName, Datetime

SurveyInfo

Id, SurveyId, First name, Last name DateTime

样本数据

SurveyTable

1  Survey-ABC 1 Jan 2011
2  Survey-AXC 1 Feb 2011
3  Survey-AEW 1 Mar 2011
4  Survey-AVD 1 Apr 2011
....

SurveyInfo

1  1  Peter James 1 Jan 2011
2  1  John Tom    1 Jan 2011
3  1  Harry Kin   1 Jan 2011
4  2  Amber Wills 1 Feb 2011
5  2  Tom Kin     1 Feb 2011

我想要一个像

这样的结果
  • 3位用户参加了Survey-ABC,
  • 2位用户参加了Survey-AXC

由于

4 个答案:

答案 0 :(得分:1)

SELECT 
    Count(*) AS "Number of participants", 
    SurveyTable.surveyName 
FROM 
    surveyinfo 
INNER JOIN surveytable 
    ON surveytable.surveyid = surveyinfo.surveyid 
GROUP BY 
    Year(DateTime),
    Month(Date_Time)

答案 1 :(得分:0)

以下选择声明

SELECT st.Name, COUNT(si.ID)
FROM SurveyTable.st INNER JOIN SurveyInfo si ON si.SurveyId = st.SurveyId
GROUP BY st.Name

应该为您提供参与调查的用户。

答案 2 :(得分:0)

因为您希望查看每月的号码或参与者,所以您需要通过此提取月份和组。

SELECT SurveyTable.SurveyId, SurveyTable.SurveyName, Month(SurveyInfo.DateTime) As M, COUNT(SurveyInfo.*) As Cnt
FROM SurveyTable
LEFT JOIN SurveyInfo ON SurveyInfo.SurveyId = SurveyTable.SurveyId
GROUP By SurveyTable.SurveyId, SurveyTable.SurveyName, Month(SurveyInfo.DateTime)

答案 3 :(得分:0)

SELECT stab.Name, COUNT(si.ID)
FROM SurveyTable as stab INNER JOIN SurveyInfo si ON si.SurveyId = stab.SurveyId
GROUP BY stab.Name