0

So I have table with cars and color on said cars. Color is shown as an integer, but i want to map it in the select query so that the respective color is shown instead. A mapping table doesn´t exist and I don´t have write access to the database so I can`t create a mapping table. The query is only meant for a dashboard which only allows SQL queries.

I have a table like this:

| **Color**   | **Car**    |     
| 1       | BMW    |    
| 2       | Nissan   |   
| 1       | Tesla   |     

And this query

SELECT "Color", count(*)
FROM "Cars"
GROUP BY "Color"

Which will give this result:

| Color | Count |    
|  1  |   2  |        
|  2  |   1  |

But what I want is this:

| Color | Count |    
| white  |   2  |    
| green  |   1  |

How can i write a query like this?

3
  • 1
    Join to the colors table that is the reference table with the correct name. Commented Jul 2, 2020 at 14:01
  • That would be the optimal. But said table doesn´t exist and I don´t have write access to the database so I can`t create a mapping table. Updated question with information Commented Jul 2, 2020 at 14:03
  • 2
    Gordon was not claiming you have to use a mapping table, but you should use JOIN Commented Jul 2, 2020 at 14:06

2 Answers 2

2

If you really do not have a reference table, then you will need to do something like this:

with color_names as (
  select car,
         color,
         case color
           when 1 then 'green'
           when 2 then 'white'
           else 'unknown'
         end as color_name
    from your_table
)
select color_name, count(*) 
  from color_names
 group by color_name;
Sign up to request clarification or add additional context in comments.

1 Comment

Had to make a small change, with color_names as ( ....
2

Ok so "said table doesn´t exist and I don´t have write access". There is a very simple solution here: Go talk to your DBA and ask for it. One of the stupidest ideas in IT today is developers not talking to DBAs (almost as stupid as DBA not responding to developers). Contrary to popular belief you are on the same team.
However, in the event she/he doesn't respond (thus you unable to accomplish @Gordon goal of not repeating logic) but there is a work around - at least until the DBA comes to their sinces. You create a template CTE which can be pulled into queries as needed.

with color_names (id, name) as
     ( values (1,'White')
            , (2,'Green')
     ) 

Make sure this is stored in a location accessible to the entire team. This can the be pulled in whenever needed.

Then again, on second thought, you're a developer so develop: create a function that returns a table. You can then use this function almost exactly as if it were at table and it has a massive benefit - a single point of maintenance for all queries using it. Not quite the same as just updating a table, but massively better than having to update multiple queries.

Both are incorporated in fiddle.

Comments

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.