1

Bit more complicated than the subject lets on. I'm basically running an SQL query that returns 4 columns of data. The last column is a number, the first 3 contain information about the number.

Specifically:

Col1: Question Text
Col2: Info Type
Col3: Banner
Col4: Average

So I'm querying a large dataset to get the average score of a bunch of items.

I might query to get the results for Q1 and Q2, where Info Type is Favourite or Second Favourite, Banners are Canada and the USA.

What I want my output table to look like is:

               |Canada           |USA
Q1 | Favourite | Average Score   | Average Score
Q1 | SecondFav | Average Score   | Average Score
Q2 | Favourite | Average Score   | Average Score
Q2 | SecondFav | Average Score   | Average Score

I'm looking for a way (maybe a plugin or util) where I can specify which data to use in rows and which in columns.

Not sure how clear that was, any help is appreciated.

1

1 Answer 1

0

You can do this in mysql, just pivot your rows into columns using CASE statement something like:

 Select col1 as 'Question', col2 as 'Info Type',
        case when Col3 = 'Canada' then col4 end as 'Canada',
        case when Col3 = 'USA' then col4 end as 'USA'
 from yourTableName
1
  • This is very helpful. Any idea on how to make it dynamic? So that whatever is in Col3 just always gets added as a new column?
    – McB
    Jan 26, 2012 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.