tags:

views:

26

answers:

3

Dear all,

I have a table that consists of id (auto_increment) number int (can contain values from 10 to 12) myvalue (varchar)

What I want to do is disperse the relational structure of this table for report purpose. I.e , I´d like to have something like:

id (auto_increment)

number10 (containing myvalue WHERE number=10)

number11 (containing myvalue WHERE number=11)

number12 (containing myvalue WHERE number=12)

I know that I can get the respective results by SELECT myvalue FROM mytable WHERE number = 10;

but I haven´t figured out how to write these three SELECT statements into one single table or view.

thx for any help in advance!

+1  A: 

This might do what you need. You've not explained it very well though so it might not!

SELECT user,
MIN(CASE WHEN number = 10 then myvalue end) AS number10,
MIN(CASE WHEN number = 11 then myvalue end) AS number11,
MIN(CASE WHEN number = 12 then myvalue end) AS number12
FROM table
WHERE number IN (10,11,12)
GROUP BY user 
Martin Smith
@Martin Smith and everybody else who helped. thanks so much this is exactly what worked. I am really sorry about the bad explanation. Despite all of that you figured it out! Though I have 2 questions: 1) Can you please explain why you use MIN - i´d really like to understand what happens...2) @ Vin-G how to format as code in here ? :DThx again guys!
ran2
@ran2: Indent each line by four spaces, or select a block of text and then click on the 101010 icon (ctrk-k shortcut). Though you can only do that for questions and answers (not in comments).
Vin-G
@ran2: Visit http://stackoverflow.com/editing-help for details.
Vin-G
I will, thx Vin-G. thx for helping despite stating my question so poorly! Editing help is bookmarked ;)Can you tell me, why MIN is used in this case ?
ran2
@ran2 - It isn't necessary to use `MIN`. `MAX` would have worked equally well as there should only be 1 matching record per user/number combination. It is just because of the use of GROUP BY it needs to be wrapped in an aggregate.
Martin Smith
ah.. that means any GROUP BY aggregate would have helped (e.g. MAX). Thx, i would never have figured that out...
ran2
+1  A: 

I don't get the "id number10 number11 number12" stuff, but if you want to select the rows with the number field matching a set of values, you can just do:

SELECT * FROM mytable WHERE number IN (10, 11, 12);

Or, alternatively, you can select a number range:

SELECT * FROM mytable WHERE number >= 10 AND number <= 12;

Edit 2:

Vin-G's got it. I was way off.

Lèse majesté
sorry for stating this question in such a misleading way. What you guys suggested is exactly what i have in the beginning. I want to turn this output into a table which has number10, number11, number12 as COLUMN names. In my case, i am perfectly sure that myvalue contains exactly the same amount of rows for each number
ran2
@ran2 Can you provide an example of the output you get from this stage so we can see how it needs to be pivoted? Is it on the basis of ID? Or does it only ever return exactly 3 records?
Martin Smith
+2  A: 

Something like this maybe?:

SELECT
    id,
    IF(number=10, myvalue, NULL) AS number10,
    IF(number=11, myvalue, NULL) AS number11,
    IF(number=12, myvalue, NULL) AS number12
FROM mytable
Vin-G
exactly! I think you just got what I meant. Though I can´t get to run so far, syntax wise....
ran2
Okay, not I got it. Basically it´s what I want to have. But due to the NULL I have alternating rows. I just want them to be in the same row if they belong to the same user (id)..
ran2
@martin smith: just say there is another user column. to the the select query... hmm i´d love to enter some table here... would be a lot easier :)
ran2
@ran2 - Working on the assumption that id represents userid I have updated my answer.
Martin Smith
Could you possibly show what your sql statement looks like now (and the structure of the tables involved)?
Vin-G
Oh, and just add a table in your question by formatting it as code (so they can be monospaced and easier to read).
Vin-G
HTH this helps:http://pastebin.com/5tntPics
ran2
Well that means id is not realy auto incremented then? (If it really is, then it should be unique). Or are there other tables involved in your query? If so, a group by clause would be necessary (as in Martin Smith's answer). Do post what your sql statement looks like now though. It will clarify some things a bit.
Vin-G