I know this has been asked before, but the solutions given did not work for me unfortunately.
I have several queries (they will be 42 in total, but let's try with 2 for this example) looking into one Table and returning results with different conditions. How can I simply put the results in adjacent columns with SQL?
The queries are:
SELECT Column5 as Alias1 FROM Table WHERE Column2 = 1 AND Column3 = 1 AND Column4 =1
SELECT Column5 as Alias2 FROM Table WHERE Column2 = 1 AND Column3 = 1 AND Column4 =2
... (all combinations of values in Columns 2, 3 and 4 which happen to be 42)
SELECT Column5 as Alias42 FROM Table WHERE Column2 = 7 AND Column3 = 3 AND Column4 =3
Each of the above queries works as expected and returns one column with 44 lines. All I want to do is have the queries return the results in side by side columns (so I need 42 columns with 44 lines each).
Any ideas?
I have tried the following:
Based on this: How do i combine multiple select statements in separate columns?
SELECT TMP1.Alias1,TMP2.Alias2 FROM
(SELECT Column5 as Alias1 FROM Table WHERE Column2 = 1 AND Column3 = 1 AND Column4 =1) AS TMP1,
(SELECT Column5 as Alias2 FROM Table WHERE Column2 = 1 AND Column3 = 1 AND Column4 =2) AS TMP2
This returns 44*44 lines instead of 44.
Based on this: Merge result of two sql queries in two columns
SELECT q1.Alias1, q2.Alias2
FROM (
(SELECT Column5 as Alias1 FROM Table WHERE Column2 = 1 AND Column3 = 1 AND Column4 =1) q1)
JOIN
(SELECT Column5 as Alias2 FROM Table WHERE Column2 = 1 AND Column3 = 1 AND Column4 =2) q1) q2
ON q1.Alias1 = q2.Alias2
Doesn't work, since I don't want to join the tables with any conditions, I just want to have the results next to each other. Also, doesn't compile.
Similar to the above (suggested from a friend):
SELECT Table1.Column5, Table2.Column5
FROM Table AS Table1,
Table AS Table2
WHERE Column2 = 1 AND Column3 = 1 AND Column4 =1
AND Column2 = 1 AND Column3 = 1 AND Column4 =2
Doesn't work, since it returns 44*44 instead of 44 lines (it's unnecessarily joining tables).
Also this: How Do I Combine Multiple SQL Queries? is a combination of the above.
To give some context, I'm trying to reformat a set of data in Excel from a long form to a wide form so as to perform statistical tests on them. So I am kind of limited by the Excel SQL functionality (Access syntax).
Any help will be greatly appreciated.
EDIT:
I am not posting this as an answer, since it's not solving my problem fully with SQL, but it is solving my problem.
I used Jim Sosa's solution and modified it and I have:
select
iif([Column2]=1 AND [Column3]=1 AND [Column4]=1,Column5,null) as column1,
iif([Column2]=1 AND [Column3]=1 AND [Column4]=2,Column5,null) as column2
... (40 more iffs)
from Table
Then I get what I want, but with extra nulls. I then get rid of those nulls, like so: http://exceltactics.com/automatically-delete-blank-cells-organize-data/
and that's it.
Thank you for all the responses.
I appreciate your comments that this is not a typical SQL problem :)
Cheers
