sql server - Filter reponses by initial survey response -
i have table needs filtered other responses within same table. use following script works when there duplicates of 'customer.customer_id' (which @ times there are) results skewed. question whether there better way rewrite code avoid selects distinct results 'customer' table?
the example table below should return 'two' responses there duplicate entries customer_id '10' ( based on query below)
sample table:
[res_id] [question_id] [customer_id] [survey_id] [res_answer] 1 20 10 155 male 1 20 11 155 male 1 20 10 155 male 1 20 12 155 female
current query:
select responses.res_id responseid ,responses.res_col answer ,responses.res_created datecreated ,responses.res_notes note ,responses.question_id questionid ,responses.customer_id customerid ,responses.res_answer subquestion ,responses.survey_id surveyid ,responses.res_void void ,customer.res_answer filter responses, responses customer rs.customer_id = customer.customer_id , responses.survey_id ='155' , customer.survey_id = '155' , responses.question_id = '20' , cast(customer.res_answer varchar(500)) = 'male' , responses.res_void = '0'
if distinct not supported in 2005 group
select distinct responses.res_id responseid ,responses.res_col answer ,responses.res_created datecreated ,responses.res_notes note ,responses.question_id questionid ,responses.customer_id customerid ,responses.res_answer subquestion ,responses.survey_id surveyid ,responses.res_void void ,'male' responses responses.survey_id ='155' , responses.question_id = '20' , responses.res_void = '0' , exists (select 1 responses c c.res_answer = 'male' , c.survey_id = '155' , c.id = responses.id)
Comments
Post a Comment