0

I have a table that actually already is neatly structured, but I need to change the output format. The initial table is of the format

Class Type Value
c1      t1    1
c2      t1    2
c3      
c1      t2    3
c2      t2    2
c3      t2    5
c2      t3    1
c3      t4    6
c1      t1    9
c2      t1    2

I need to dynamically create a table that has the different classes as rows, the types as columns and sums the values. the types are not fixed, so I can't build a static table. How can I dynamically create a table that looks like

class    t1    t2    t3    t4
c1       10     3     0     0
c2        4     2     1     0
c3        0     5     0     6

Is there a way to do that rather easily with sql?

As I was pointed to pivot, I had a look into it just to learn that mysql is not supporting this. what is the easiest and most straightforward way in sql to mimic pivot?

3
  • is the number types fixed to 4 items t1,t2,t3 and t4. if yes its easy to use a pivot Commented May 20, 2014 at 7:06
  • Since the types are not fixed, you will need a PIVOT or CROSSTAB syntax. Not all RDBMS support this type of feature. Which RDBMS are you using? Commented May 20, 2014 at 7:06
  • mysql, which unfortunately does not support pivot as I learned Commented May 20, 2014 at 10:37

2 Answers 2

1

If there're 4 types (i.e. t1, t2, t3, t4) only, try case construction, something like that:

  select class,
         sum(case 
               when Type = 't1' then
                 value
               else
                 0
             end) as t1, 
         sum(case 
               when Type = 't2' then
                 value
               else
                 0
             end) as t2,
         sum(case 
               when Type = 't3' then
                 value
               else
                 0
             end) as t3,
         sum(case 
               when Type = 't4' then
                 value
               else
                 0
             end) as t4     
    from MyTable
group by class

In general case (if t1..tn are not fixed) you have to pivot

Sign up to request clarification or add additional context in comments.

1 Comment

t'2 are not fixed. Thanks for pointing out pivot. I was not aware of this feature. a quick google search makes it look like pivot could be my solution
1

If the number types are fixed to 4 (t1,t2,t3 and t4). We can do this without PIVOT:

Try this:

SELECT class,
      COALESCE(SUM(CASE WHEN Type='t1' THEN Value END),0) AS t1,
      COALESCE(SUM(CASE WHEN Type='t2' THEN Value END),0) AS t2,
      COALESCE(SUM(CASE WHEN Type='t3' THEN Value END),0) AS t3,
      COALESCE(SUM(CASE WHEN Type='t4' THEN Value END),0) AS t4
FROM TableName
GROUP BY class

Result:

CLASS   T1  T2  T3  T4
c1      10  3   0   0
c2      4   2   1   0
c3      0   5   0   6

See result in SQL Fiddle.

4 Comments

Why MAX and not SUM? And how is this supposed to handle the dynamic list of types?
Coalesce is more general than IfNull, IsNull, Nvl; Coalesce is supported by almost every DBMS
unfortunately, t's are not fixed. but pivot seems to be the way to go. was not aware of this feature. thanks
@chrise: Its always good to learn more. Start from here or here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.