0

I am running this query:

SELECT type, COUNT(type) FROM "table"  where id = 8 GROUP BY type;

With this result:

6   3814
8   341
5   328

I'd like to have something like this, where I can specify names:

Arbitrary Name  3814
Other name       341
Test Name        328

Instead of the type column listing 6, how can I get it to have a custom name like Test Column 6, or Fun Column 5? I'm using postgres.

1
  • 2
    usually people create separate table with such names and join with it in query to display human readable names for IDs Commented Apr 20, 2015 at 16:50

2 Answers 2

1

SELECT CASE WHEN type = X THEN 'SOME TEXT' ELSE 'OTHER TEXT' END , COUNT(type) FROM "table" where id = 8 GROUP BY type;

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

Comments

1

1st ) If you have alot of "type" in your record then you should create a new table to store it information ( typeid , name , detail ) then you can get it like :

Example

SELECT typegroup.typename,Count(typeid) from 'table',typegroup where 'table'.id=8 and typegroup.typeid='table'.typeid GROUP BY 'table'.type

2nd ) If you don't have alot of "type" in your record then you can try these statement

SELECT 
   IF (type = '6','8','5',REPLACE(type, 'Arbitrary Name','Other name','Test Name')) AS type 
   FROM table 
   WHERE id = 8
GROUP BY type

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.