-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
  • 31,810
  • 31
  • 111
  • 133
Indra Yadav
  • 600
  • 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
  • 31,810
  • 31
  • 111
  • 133
  • thanks... your code is working but there is little bit change in my data i have N/A value and it is showing as 0 – Indra Yadav Dec 28 '12 at 10:07
  • @IndraYadav - N/A value in which column? Show your exact data (few) in [this SQLFiddle](http://sqlfiddle.com/#!2/20e4c/12) – Himanshu Dec 28 '12 at 10:10
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
  • 34,802
  • 21
  • 91
  • 134