Jump to content

SQL multiple columns from one

Recommended Posts



I tried to post my question to stackoverflow, but couldn't (unknown error massage) and w3school, but i still need to be validated there to ask.

I'm trying to create a query (in SQL developer), which retruns the number of emission classes of cars, grouped by country codes. I can create it for one emission class like this:
      DISTINCT country_code,
      COUNT(emission_class) EURO0
    WHERE emission_class = 'E0'
    GROUP BY country_code;
My problem that I don't know how to count for a new column for example EURO1 emission class, from the same column (emission_class). I thought that I should create views and then add them togeather, or UNION 6 different selects. Tried with where and having clauses.
I always seems to run into some kind of a problem, then i realize that when I'm not sure how to start something properly, I should ask/research it first, so here I'm.
Thank you for the insight! (sorry still quite fresh with SQL, thank god I haven't paid anything for university, the internet is actually way better :) )

Share this post

Link to post
Share on other sites

I found a very good and simple solution, basicly just have to ask for the columns the right way :) 

SUM(CASE WHEN emission_class = 'E0' THEN 1 ELSE 0 END) AS EURO0

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


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.