-2

I have two tables named

style:

===================
   id  |  style
===================
    1  |  x
===================
    2  |  y

size :

===========================================
   id  |  size_name  | style_id  | Quantity
===========================================
    1  |  2T         |  1        |  200
===========================================
    2  |  3T         |  1        |  100
===========================================
    3  |  4T         |  2        |  250
===========================================
    4  |  2T         |  2        |  500

Using this two tables I want a report like below:

=============================================
  Style  |              size                      
=============================================
         |    2T   |    3T    |    4T    |    
=============================================
   x     |    200  |    100   |    0     |
=============================================
   y     |    500  |     0    |   250    |

Thanks in advance.Please help

Wolverine
  • 71
  • 3
  • 11
  • 2
    then start with what you can do, then post what is not working for you on your code. – Prix Jun 29 '13 at 09:10
  • It's pretty easy if you break down the output and take one row at a time..think logically. You will get the solution. To nudge you in a right direction, use `DISTINCT sizename` query to get the second row of your customizable table – asprin Jun 29 '13 at 09:16
  • i dint try anything coz i just dont kno what to do – Wolverine Jun 29 '13 at 09:18
  • can you write the query? @asprin – Wolverine Jun 29 '13 at 09:19
  • We at stackoverflow expect people to show some effort, you do know how to connect to a database ? you do know how to loop and read the values ? start by writing what you know until you get to the part you don't know then post back what you have done. [**Also take your time to visit the TOUR**](http://stackoverflow.com/about) – Prix Jun 29 '13 at 09:19
  • Now please see this.i post what i tried before. – Wolverine Jun 29 '13 at 09:51
  • @Wolverine - Did you see the dynamic query? – Himanshu Jansari Jun 29 '13 at 10:16

2 Answers2

1

Static query (if you have known and limited numbers of size_name)

SELECT st.Style
  ,SUM(CASE WHEN s.size_name = '2T' THEN Quantity ELSE 0 END) AS `2T`
  ,SUM(CASE WHEN s.size_name = '3T' THEN Quantity ELSE 0 END) AS `3T`
  ,SUM(CASE WHEN s.size_name = '4T' THEN Quantity ELSE 0 END) AS `4T`
FROM Size s
JOIN Style st ON s.style_id = st.id
GROUP BY st.Style;

Dynamic query (if you do not know the number of size_name)

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(CASE WHEN `size_name` = ''',
      `size_name`,
      ''' THEN Quantity ELSE 0 END) AS `',
      `size_name`, '`'
    )
  ) INTO @sql
FROM Size;

SET @sql = CONCAT('SELECT st.Style , ', @sql, '
                  FROM Size s
                  JOIN Style st ON s.style_id = st.id
                   GROUP BY st.Style 
                  ');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Output:

╔══════╦═════╦═════╦═════╗
║ TYLE ║ 2T  ║ 3T  ║ 4T  ║
╠══════╬═════╬═════╬═════╣
║ x    ║ 200 ║ 100 ║   0 ║
║ y    ║ 500 ║   0 ║ 250 ║
╚══════╩═════╩═════╩═════╝

See this SQLFiddle

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

try this

  select 
  style , 
  max(case when size_name = '2T' then Quantity else 0 end) as '2T' ,
  max(case when size_name = '3T' then Quantity else 0 end) as '3T' ,
  max(case when size_name = '4T' then Quantity else 0 end) as '4T'
  from style inner join size
  on size.style_id = style.id
  group by style.id

demo here

echo_Me
  • 36,552
  • 5
  • 55
  • 77