Edit: I use Microsoft SQL Server Management Studio to run my queries. Microsoft SQL Server Standard Edition (64-bit) I guess.
I've got a complex query to run. I had no idea how to do it, so I went and write a query that's around 500 rows and many joins. Not how things should be dome.
My data structure is:
id user_id question_id answer1
1 1 1 a
2 1 2 c
3 1 3 a
4 2 1 c
5 2 2 a
... ... ... ...
There are over 700 users. Each user has answered around 60 questions (times 2, there are 2 answers to each question, but this is irrelevant if I can get a nice query for the first answer). There are some questions I have no interest in (and should actually skip).
My current result is the following (good result, but the query is way too heave):
user_id q1 q2 q3 q4 q8 q9 ... q60
1 a b c d b a ... a
2 b a c a c b ... w
3 y a w ... ...
So, basically I'd like to create an array, say [1,2,3,4,8,9], saying I am interested in those question id's, and query so I get those columns as the above example. I have no idea how to do this.
My current query is in the likes of:
SELECT C.user_id, Q1, Q2, Q3, Q4, Q8, ...
FROM (
SELECT A.user_id, Q1, // and here tons of unions