Skip to content
Advertisement

A homework question: is there a way to compare contents in different tables at the same time?

The part of the schema is as follows:

  • Customer (cid, name, city) (primary key: cid)
  • Purchase (cid, club, title, year, when, qnty) (primary key: cid, club, title, year, when)

I want to list distinct pairs of customers who have made a purchase of a book (title) in common. For each pair of such customers, show first the one with the larger cid. In the answer table, only report the customers’ names. The output columns’ names are namea and nameb.

I already list all pairs with different namea and nameb with the same title

In case someone needs the table creation file: create, the expected return number should be 283, but I keep getting 282 since there are 2 people with the same name.

Edited:
There will be two people with the same name, so the output pairs need to be distinct by the cid, not the name. The number of my actual output tuples matches the expected output when I SELECT cid, but it doesn’t work when I SELECT names. Since customers are identified by the cid, not the name, some customers with the same name will be filtered out if we distinct names. And this is the problem I’m working on right now.

Advertisement

Answer

Add the cids to the select list of your query and then select from it only the names:

Or you can group by c1.cid, c1.name, c2.cid, c2.name:

See the demo.
Result 283 rows.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement