1

I am a PostgreSQL newbie, and I have two tables like this:

  1. Attendees(AttendeeId, Name)
  2. Couples(CoupleId, AttendeeIdMan, AttendeeIdLady)

How do I create a view like this?

Attendees_Couple(CoupleId, Name_Man, Name_Lady)

For now, I came out with something like the below, but then I got stuck.

CREATE VIEW Attendees_Couple AS
SELECT a."Name"
FROM "Attendees" a, "Couples" c
WHERE a."AttendeeID" = c."AttendeeIdMan"....

Any hint or help will be appreciated!

1 Answer 1

6

You'll have to join with Attendees twice:

CREATE VIEW "Attendees_Couple" AS
SELECT c."CoupleId",
       a1."Name" AS "Name_Man",
       a2."Name" AS "Name_Lady"
FROM "Couples" AS c
   JOIN "Attendees" AS a1 ON c."AttendeeIdMan"  = a1."AttendeeId"
   JOIN "Attendees" AS a2 ON c."AttendeeIdLady" = a2."AttendeeId";

Do yourself a favor and use lower case names!

Sign up to request clarification or add additional context in comments.

2 Comments

Oh yes, +1 for the use of lower case names.
That makes sense! Thanks a lot! So sad I am not the one who created those tables...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.