dbTalk Databases Forums  

Create view with substitution for values in one column

comp.databases.postgresql comp.databases.postgresql


Discuss Create view with substitution for values in one column in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Julia Jacobson
 
Posts: n/a

Default Create view with substitution for values in one column - 08-28-2010 , 02:10 PM






Dear PostgreSQL users,

Presume you had a table of employees and the incentive payments they
received additionally to their salaries.
Now you would like to give someone the information, whether an employee
received an incentive or not, but not the exact height of the payment.
Is it possible to create a view of the table which converts the column
containing the numeric values of the incentives to a column of the data
type boolean?

Thanks in advance,
Julia

Reply With Quote
  #2  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-28-2010 , 02:34 PM






On Sat, 28 Aug 2010 21:10:33 +0200, Julia Jacobson wrote:

Quote:
Dear PostgreSQL users,

Presume you had a table of employees and the incentive payments they
received additionally to their salaries. Now you would like to give
someone the information, whether an employee received an incentive or
not, but not the exact height of the payment. Is it possible to create a
view of the table which converts the column containing the numeric
values of the incentives to a column of the data type boolean?

Thanks in advance,
Julia
Of course, just convert it to the data type boolean:


mgogala=# select 5::boolean;
bool
------
t
(1 row)

Time: 74.110 ms
mgogala=# select 0::boolean;
bool
------
f
(1 row)

Time: 0.252 ms

If the numbers are float or double numbers, you can always be creative
with casts:

mgogala=# select cast(3.14 as int)::boolean;
bool
------
t
(1 row)


So, the select would look something like this:

select ename, empno,job,mgr,sal,cast(bonus as int)::boolean
from emp
where deptno=10
order by sal desc;


--
http://mgogala.byethost5.com

Reply With Quote
  #3  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-28-2010 , 02:41 PM



Julia Jacobson wrote on 28.08.2010 21:10:
Quote:
Dear PostgreSQL users,

Presume you had a table of employees and the incentive payments they
received additionally to their salaries. Now you would like to give
someone the information, whether an employee received an incentive or
not, but not the exact height of the payment. Is it possible to
create a view of the table which converts the column containing the
numeric values of the incentives to a column of the data type
boolean?

select case
when bonus is not null and bonus > 0 then true
else false
end
from ...

Thomas

Reply With Quote
  #4  
Old   
Julia Jacobson
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-28-2010 , 02:50 PM



Quote:
So, the select would look something like this:

select ename, empno,job,mgr,sal,cast(bonus as int)::boolean
from emp
where deptno=10
order by sal desc;
Thanks a lot for your answer.
Unfortunately, there is one exception where your solution doesn't work:
If the bonus is less than 1 dollar, for example 0.50 dollar,
your query would indicate that the employee didn't get a bonus.
In this case,
Quote:
select case
when bonus is not null and bonus > 0 then true
else false
end
from employees
would be better.

Reply With Quote
  #5  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-28-2010 , 02:55 PM



On Sat, 28 Aug 2010 21:41:42 +0200, Thomas Kellerer wrote:

Quote:
Julia Jacobson wrote on 28.08.2010 21:10:
Dear PostgreSQL users,

Presume you had a table of employees and the incentive payments they
received additionally to their salaries. Now you would like to give
someone the information, whether an employee received an incentive or
not, but not the exact height of the payment. Is it possible to create
a view of the table which converts the column containing the numeric
values of the incentives to a column of the data type boolean?


select case
when bonus is not null and bonus > 0 then true else false
end
from ...

Thomas
This solution is better than mine. My solution doesn't handle null
values, despite being more elegant.



--
http://mgogala.byethost5.com

Reply With Quote
  #6  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-28-2010 , 02:58 PM



On Sat, 28 Aug 2010 21:50:10 +0200, Julia Jacobson wrote:

Quote:
Thanks a lot for your answer.
Unfortunately, there is one exception where your solution doesn't work:
If the bonus is less than 1 dollar, for example 0.50 dollar, your query
would indicate that the employee didn't get a bonus.
For all practical purposes, the employee who cannot even get twinkies
from a vending machine for the amount of his or her bonus, did not, in
fact, get a bonus, he was a victim of a cruel prank. My solution also
doesn't handle null values, which is a more significant issue.



--
http://mgogala.byethost5.com

Reply With Quote
  #7  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-28-2010 , 06:39 PM



On Sat, 28 Aug 2010 19:55:01 +0000, Mladen Gogala wrote:

Quote:
This solution is better than mine. My solution doesn't handle null
values, despite being more elegant.
Of course, that can be fixed by using the "coalesce" function, but then
the elegance is lost.



--
http://mgogala.byethost5.com

Reply With Quote
  #8  
Old   
Bob Badour
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-28-2010 , 07:57 PM



Mladen Gogala wrote:

Quote:
On Sat, 28 Aug 2010 19:55:01 +0000, Mladen Gogala wrote:

This solution is better than mine. My solution doesn't handle null
values, despite being more elegant.

Of course, that can be fixed by using the "coalesce" function, but then
the elegance is lost.
Elegance is always lost as soon as one introduces null. That part's not
your fault.

Reply With Quote
  #9  
Old   
Robert Klemme
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-29-2010 , 04:41 AM



On 28.08.2010 21:41, Thomas Kellerer wrote:
Quote:

Julia Jacobson wrote on 28.08.2010 21:10:
Dear PostgreSQL users,

Presume you had a table of employees and the incentive payments they
received additionally to their salaries. Now you would like to give
someone the information, whether an employee received an incentive or
not, but not the exact height of the payment. Is it possible to
create a view of the table which converts the column containing the
numeric values of the incentives to a column of the data type
boolean?

select case
when bonus is not null and bonus > 0 then true
else false
end
from ...
I would have suggested CASE as well. But is the NULL check really
needed? IMHO "bonus > 0" can only be true if bonus is not 0. This is
basic SQL standard, isn't it?

Btw, I am not yet familiar with PostgreSQL's security model but the
whole exercise might be moot if you cannot prevent access to the base
table for those who are only allowed to see the flag version of the
incentive.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Reply With Quote
  #10  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Create view with substitution for values in one column - 08-29-2010 , 05:25 AM



Robert Klemme wrote on 29.08.2010 11:41:
Quote:
I would have suggested CASE as well. But is the NULL check really
needed? IMHO "bonus > 0" can only be true if bonus is not 0. This is
basic SQL standard, isn't it?
True

Quote:
Btw, I am not yet familiar with PostgreSQL's security model but the
whole exercise might be moot if you cannot prevent access to the base
table for those who are only allowed to see the flag version of the
incentive.
Just revoke the SELECT privilege on the table and grant the (only) on the view.

Regards
Thomas

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.