1

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

表结构如下

调查表

SurveyId, SurveyName, Datetime

调查信息

Id, SurveyId, First name, Last name DateTime

样本数据

调查表

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

调查信息

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

4 回答 4

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)
于 2012-05-03T05:45:37.230 回答
0

以下选择语句

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

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

于 2012-05-03T05:23:24.023 回答
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)
于 2012-05-03T05:23:56.163 回答
0
SELECT stab.Name, COUNT(si.ID)
FROM SurveyTable as stab INNER JOIN SurveyInfo si ON si.SurveyId = stab.SurveyId
GROUP BY stab.Name
于 2012-05-03T05:25:10.073 回答