I am very new to SQL Server and I am attempting to create span criteria for surveys about our company which will spit out an average depending on the date span given. The detail from the surveys include a survey start date which can be utilized. Here is the sql from the initial table and query building upon it without the date criteria/function:
snippet from surveyTb:
SurveyID | SurveyCallID | SurveyStart | SurveyQ1 | SurveyQ5 | SurveyQ9 |
1 | 15 | 6/6/13 1:09 PM | 10 | 10 | 10 |
2 | 19 | 6/6/13 2:15 PM | 10 | 10 | 10 |
4 | 27 | 6/7/13 11:20 AM | 10 | 10 | 10 |
8 | 38 | 6/10/13 12:46 PM | 10 | 10 | 10 |
10 | 44 | 6/11/13 1:36 PM | 10 | 10 | 10 |
etc...
Query without any time criteria:
SELECT 'Company-Avg Overall Service Rating-ExistingPt' AS ID
, ROUND(AVG(CAST(s.SurveyQ1 AS float)),2) AS DataPoint
FROM [dbo].[SurveyTb] AS s
UNION ALL
SELECT 'Company-Avg Most Recent Transaction Rating-ExistingPt' AS ID
, ROUND(AVG(CAST(s.SurveyQ5 AS float)),2) AS DataPoint
FROM [dbo].[SurveyTb] AS s
UNION ALL
SELECT 'Company-Avg Recommendation of Service Rating-ExistingPt' AS ID
, ROUND(AVG(CAST(s.SurveyQ9 AS float)),2) AS DataPoint
FROM [dbo].[SurveyTb] AS s
RESULT:
ID | DataPoint | ||
Company-Avg Overall Service Rating-ExistingPt | 9.18 | ||
Company-Avg Most Recent Transaction Rating-ExistingPt | 9.44 | ||
I would like to try and have a daily(yesterday), weekly, monthly, MTD, quarterly, QTD, and yearly & YTD to trend out over time. I appreciate any help I can get! |