Solved

Create view in postgresql

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
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…

708 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

18 Experts available now in Live!

Get 1:1 Help Now