Solved

Create view in postgresql

Posted on 2011-09-25
5
553 Views
Last Modified: 2012-08-13
Hallo i'm new to postgresql and i can't achieve this result:

table 1 "pizza":
id_pizza
name_pizza

table 2 "pizza ingredient":
id_pizza
ingredient_name
ingredient_available

example data:

table1:
1 margherita
2 wurstel
3 cheese

table2
1 tomato           true
1 mozzarella    true
2 wurstel          true
3 cheese1        true
3 chesse2        true
3 tomato           false

i would like create a view that return this result, if possible:

pizzas view:
1 margherita   available
2 wurstel         available
3 cheese         not  available


thanks to all

0
Comment
Question by:wakatanka
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:k_murli_krishna
ID: 36600445
I need a small clarification to be able to answer your question. On what basis available and not available are being decided under pizzas view. Why cheese is coming as not available?.

As far as I can understand, in table2, once one false is coming against tomato then tomato should be shown as not available. Is this correct?
0
 
LVL 22

Expert Comment

by:earth man2
ID: 36601172
something like

create view available_pizza as
  select a.pizza_name, case when
(select min(b.ingredient_available) from pizza_ingredient  b where b.id_pizza = a.id_pizza)  then
'pizza available'
else
'pizza unavailable' as pizza_availability from pizza a;
0
 

Author Comment

by:wakatanka
ID: 36710785
@earyman2

thanks for your entry, but b.ingredienti_available is a boolean and the min function cannot be used.

@k_murli_krishna:
Pizza are not available, when just 1 of his ingredient is not available, ie: if tomato is false, margherita and cheese pizza are not available. (i correct the initial schema posted).
0
 
LVL 22

Accepted Solution

by:
earth man2 earned 500 total points
ID: 36713305
drop view available_pizza;
drop table pizza;
create table pizza ( id_pizza int, pizza_name varchar(40));
drop table pizza_ingredient;
create table pizza_ingredient( id_pizza int, ingredient_name varchar(40), ingredient_available boolean );

insert into pizza values (1, 'margherita'),(2 ,'wurstel'),(3,' cheese');

insert into pizza_ingredient values (1, 'tomato',   true),
(1, 'mozzarella',    true),
(2, 'wurstel',          true ),
 (3, 'cheese1'  ,      true ),
 (3, 'chesse2',        true ),
 (3, 'tomato' ,          false );
 
create view available_pizza as
  select a.pizza_name, case when
(select bool_and(b.ingredient_available) from pizza_ingredient  b where b.id_pizza = a.id_pizza)  then
'pizza available'
else
'pizza unavailable' end pizza_availability from pizza a;

select * from available_pizza;
0
 

Author Closing Comment

by:wakatanka
ID: 36715010
it works flawless, thank you so much, you are the man.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question