Solved

Create view in postgresql

Posted on 2011-09-25
5
542 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now