ChristinaPupo
asked on
Need help on select with group by statement
I am trying to figure out how to group some results from a survey.
I have 4 tables: SurveyResults, SurveyAnswers, SurveyQuestions, and Users
SurveyResults
---------------
results_id
UserID
question_id
answer_id
date_inserted
SurveyAnswers
---------------
answer_id
question_id
answer
anwser_type
order_in_question
SurveyQuestions
---------------
question_id
survey_id
question
question_type
default_answer
order_in_survey
Users
---------------
UserID
Company
Phone
I have this select statement that works but does not group properly:
sub db_select_grid_SurveyResul ts
grid_SurveyResults_sql = "SELECT " & _
"Users.Company," & _
"Users.Phone," & _
"SurveyResults.results_id, " & _
"SurveyResults.UserID, " & _
"SurveyResults.answer_valu e, " & _
"SurveyResults.dt_inserted , " & _
"SurveyAnswers.question_id , " & _
"SurveyAnswers.answer_id, " & _
"SurveyAnswers.answer, " & _
"SurveyAnswers.order_in_qu estion, " & _
"SurveyQuestions.question, " & _
"SurveyQuestions.survey_id FROM (((SurveyResults LEFT JOIN SurveyAnswers ON SurveyAnswers.answer_id = SurveyResults.answer_id) " & _
"LEFT JOIN SurveyQuestions ON SurveyQuestions.question_i d = SurveyAnswers.question_id) " & _
"LEFT JOIN Users On SurveyResults.UserID=Users .UserID) ORDER BY SurveyResults.UserID, SurveyAnswers.order_in_que stion" & _
"" & _
"" & _
""
end sub
if grid_SurveyResults_sql <> "" then
cmd.CommandText = grid_SurveyResults_sql
rs.Filter = ""
if survey_id <> "" then rs.Filter = "survey_id = " & to_sql(survey_id,"number")
rs.CursorLocation = 3
rs.CacheSize = 5
rs.Open cmd
if not rs.EOF then
rs.MoveFirst
rs.PageSize = 30
totalPages = rs.PageCount
rs.AbsolutePage = curPage
num_recs = rs.RecordCount
results = true
else
results = false
rs.Close
end if
else
results = false
end if
The above SQL produces:
UserID Company Phone Date Question Answer
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ------
1 Company1 111-222-3333 10/27/2008 How many dogs? 7
1 Company1 111-222-3333 10/27/2008 How many cats? 3
1 Company1 111-222-3333 10/27/2008 How many birds? 1
2 Company2 111-222-3333 11/02/2008 How many dogs? 5
2 Company2 111-222-3333 11/02/2008 How many cats? 3
2 Company2 111-222-3333 11/02/2008 How many birds? 1
3 Company3 111-222-3333 10/16/2008 How many dogs? 1
3 Company3 111-222-3333 10/16/2008 How many cats? 3
3 Company3 111-222-3333 10/16/2008 How many birds? 1
Need data displayed like:
UserID Company Phone Date Question Answer
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ------
1 Company1 111-222-3333 10/27/2008 How many dogs? 7
How many cats? 3
How many birds? 1
2 Company2 111-222-3333 11/02/2008 How many dogs? 5
How many cats? 3
How many birds? 1
3 Company3 111-222-3333 10/16/2008 How many dogs? 1
How many cats? 3
How many birds? 1
Each user has multiple questions and answers. Can anyone help me modify my select statement (or code) to group the results properly?
I have 4 tables: SurveyResults, SurveyAnswers, SurveyQuestions, and Users
SurveyResults
---------------
results_id
UserID
question_id
answer_id
date_inserted
SurveyAnswers
---------------
answer_id
question_id
answer
anwser_type
order_in_question
SurveyQuestions
---------------
question_id
survey_id
question
question_type
default_answer
order_in_survey
Users
---------------
UserID
Company
Phone
I have this select statement that works but does not group properly:
sub db_select_grid_SurveyResul
grid_SurveyResults_sql = "SELECT " & _
"Users.Company," & _
"Users.Phone," & _
"SurveyResults.results_id,
"SurveyResults.UserID, " & _
"SurveyResults.answer_valu
"SurveyResults.dt_inserted
"SurveyAnswers.question_id
"SurveyAnswers.answer_id, " & _
"SurveyAnswers.answer, " & _
"SurveyAnswers.order_in_qu
"SurveyQuestions.question,
"SurveyQuestions.survey_id
"LEFT JOIN SurveyQuestions ON SurveyQuestions.question_i
"LEFT JOIN Users On SurveyResults.UserID=Users
"" & _
"" & _
""
end sub
if grid_SurveyResults_sql <> "" then
cmd.CommandText = grid_SurveyResults_sql
rs.Filter = ""
if survey_id <> "" then rs.Filter = "survey_id = " & to_sql(survey_id,"number")
rs.CursorLocation = 3
rs.CacheSize = 5
rs.Open cmd
if not rs.EOF then
rs.MoveFirst
rs.PageSize = 30
totalPages = rs.PageCount
rs.AbsolutePage = curPage
num_recs = rs.RecordCount
results = true
else
results = false
rs.Close
end if
else
results = false
end if
The above SQL produces:
UserID Company Phone Date Question Answer
--------------------------
1 Company1 111-222-3333 10/27/2008 How many dogs? 7
1 Company1 111-222-3333 10/27/2008 How many cats? 3
1 Company1 111-222-3333 10/27/2008 How many birds? 1
2 Company2 111-222-3333 11/02/2008 How many dogs? 5
2 Company2 111-222-3333 11/02/2008 How many cats? 3
2 Company2 111-222-3333 11/02/2008 How many birds? 1
3 Company3 111-222-3333 10/16/2008 How many dogs? 1
3 Company3 111-222-3333 10/16/2008 How many cats? 3
3 Company3 111-222-3333 10/16/2008 How many birds? 1
Need data displayed like:
UserID Company Phone Date Question Answer
--------------------------
1 Company1 111-222-3333 10/27/2008 How many dogs? 7
How many cats? 3
How many birds? 1
2 Company2 111-222-3333 11/02/2008 How many dogs? 5
How many cats? 3
How many birds? 1
3 Company3 111-222-3333 10/16/2008 How many dogs? 1
How many cats? 3
How many birds? 1
Each user has multiple questions and answers. Can anyone help me modify my select statement (or code) to group the results properly?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
ASKER