0

To cut a long story short, is it possible to create a database view in postgres and at the same time change the record values for a specific column ?

CREATE VIEW ViewOfCoupons AS
SELECT *
FROM Coupons
WHERE title = 'Category_1' OR title = 'Category_2';

I want to rename the records contained in the view to have different title other than 'Category_1' or 'Category_2', lets say 'Category_3'. In fact, as I mention in the commect, 'Category_3' is a supercategory involving the other two. So I only want the rows having these values. Is this possible with CASE statement ?

2
  • Do you want to see only rows with those categories? Or all rows and only "rename" those two categories? Commented Sep 28, 2016 at 11:38
  • Yes, I want to see only rows with those two categories. In fact, 'Category_3' is a supercategory involving those two. I will edit. Thanks. Commented Sep 28, 2016 at 11:53

1 Answer 1

1
CREATE VIEW ViewOfCoupons AS
SELECT case title
         when 'Category_1' then 'Brand new category' 
         when 'Category_2' then 'The other category'
         else title
       end as title, 
       ... other columns ...
FROM Coupons
WHERE title in ('Category_1', 'Category_2');

Strictly speaking the else title is not necessary because the WHERE clause makes sure that no other values will appear in that column, but I add this nevertheless to be prepared for future changes, e.g. if someone changes the where clause but forgets to change the case as well.

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

4 Comments

I think that you don't need the WHERE clause.
@redneb: well it was present in the original query. It's unclear to me if dpesios want's only rows with those values or all rows.
@a_horse_with_no_name I see your point and thank you. But I want to get all the rows of Coupons (projecting all the columns) where the category is 'Category_1' or 'Category_2' and change it in the view.
It's already there, the answer (... other columns ...). Thanks!

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.