Jump to content

Multiple Row -> One Row


mattbru

Recommended Posts

I need to combine multiple rows into one row for a query.  Given the table:

 

TIME  |  ID  |  STATUS

9          1        GOOD

9          2        BAD

9          3        GOOD

10        1        GOOD

10        2        GOOD

10        3        BAD

 

I would want:

 

TIME  |  ID 1  |  ID 2  |  ID 3 

9          GOOD    BAD    GOOD

10        GOOD    GOOD  BAD

 

Also, there may be a variable number of ID's per time (not just 3).

 

Any ideas?

Link to comment
Share on other sites

Hi

 

Can't see a way to do that. It would bring back a variable number of columns.

 

You could use GROUP_CONCAT to put the fields into one:-

 

SELECT `TIME` GROUP_CONCAT(`STATUS` SEPARATOR ',')
FROM SomeTable
GROUP BY `TIME`

 

You could then explode the status field in php if you wanted.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Can't see a way to do that. It would bring back a variable number of columns.

 

You could use GROUP_CONCAT to put the fields into one:-

 

SELECT `TIME` GROUP_CONCAT(`STATUS` SEPARATOR ',')
FROM SomeTable
GROUP BY `TIME`

 

You could then explode the status field in php if you wanted.

 

All the best

 

Keith

 

 

Okay, thanks.  Suppose it was limited to a specific number, say 5 ID's per time?

Link to comment
Share on other sites

Hi

 

Possibly, but cannot see a way of doing it without it being either too complicated for something simple or too slow (or both).

 

Something like this might work:-

 

SELECT DISTINT `TIME`, b.STATUS, c.STATUS, d.STATUS, e.STATUS, f.STATUS
FROM SomeTable a
LEFT OUTER JOIN (SELECT STATUS FROM SomeTable z WHERE z.`TIME` = a.`Time LIMIT 0,1) b ON a.`TIME` = b.`TIME`
LEFT OUTER JOIN (SELECT STATUS FROM SomeTable y WHERE y.`TIME` = a.`Time LIMIT 1,1) c ON a.`TIME` = c.`TIME`
LEFT OUTER JOIN (SELECT STATUS FROM SomeTable x WHERE x.`TIME` = a.`Time LIMIT 2,1) d ON a.`TIME` = d.`TIME`
LEFT OUTER JOIN (SELECT STATUS FROM SomeTable w WHERE w.`TIME` = a.`Time LIMIT 3,1) e ON a.`TIME` = e.`TIME`
LEFT OUTER JOIN (SELECT STATUS FROM SomeTable v WHERE v.`TIME` = a.`Time LIMIT 4,1) f ON a.`TIME` = f.`TIME`

 

but not a nice idea (and not tested at all).

 

All the best

 

Keith

Link to comment
Share on other sites

Try this out:

SELECT `time`
     ,IF(SUM(IF(`id`=1, `status`,0))=1,'GOOD','BAD') AS 'id1'
     ,IF(SUM(IF(`id`=2, `status`,0))=1,'GOOD','BAD') AS 'id2'
     ,IF(SUM(IF(`id`=3, `status`,0))=1,'GOOD','BAD') AS 'id3'
FROM <insert table name here>
GROUP BY `time`;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.