-1

I have data in mysql data base in following format-

itemName      Date      isAvailable
abc        1/12/2012    yes
xyz        1/12/2012    No
aaa        1/12/2012    yes

abc        2/12/2012    no
xyz        2/12/2012    no
aaa        2/12/2012    yes

abc        3/12/2012    no
xyz        3/12/2012    yes
aaa        3/12/2012    yes
new        3/12/2012    yes

I want to collect data into following format-

itemName   1/12/2012    2/12/2012   3/12/2012
abc       yes             no        no
xyz       no              no        yes
aaa       yes             yes       yes
new        -              -         yes

Any help?

Himanshu Jansari
  • 30,115
  • 28
  • 106
  • 129
Indra Yadav
  • 594
  • 5
  • 22

2 Answers2

0

Try this one:

SELECT itemName,
       GROUP_CONCAT(CASE Date_format(`Date`,'%Y-%d-%m') WHEN '2012-01-12' THEN `isAvailable` ELSE NULL END) AS `2012-01-12`
      ,GROUP_CONCAT(CASE Date_format(`Date`,'%Y-%d-%m') WHEN '2012-02-12' THEN `isAvailable` ELSE NULL END) AS `2012-02-12`
      ,GROUP_CONCAT(CASE Date_format(`Date`,'%Y-%d-%m') WHEN '2012-03-12' THEN `isAvailable` ELSE NULL END) AS `2012-03-12`
FROM Table1
GROUP BY `itemName`;

See this SQLFiddle

Himanshu Jansari
  • 30,115
  • 28
  • 106
  • 129
0

You are talking about a PIVOT operation when you transform rows into columns. Here is an example on SO.

Some DBMS will have special commands to deal with that (Like SQl Server that has PIVOT and UNPIVOT). I dont know if mysql has anything.

If you google "T-SQL PIVOT" you will find several examples on how to do it using just T_SQL

Community
  • 1
  • 1
Diego
  • 33,213
  • 18
  • 87
  • 131