Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create view in postgresql

Posted on 2011-09-25
5
Medium Priority
?
585 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
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…
Suggested Courses

604 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