Forums Register Login

Need Some Help with a join Query

+Pie Number of slices to send: Send
I have the following:



I need to write a query such that I find all the header rows that contain details with 2 specific products. I have the following so far:



This isn't getting what I expect. I want header rows with at least 2 detail rows. Of those 2, each one should point to the product IDs I am specifying.

Thanks.
+Pie Number of slices to send: Send
Does the answer have to involve joins? Because I would do something like this:


I'm sure that code could be recast in terms of joins, but I wouldn't because I find it more readable the way I wrote it.
+Pie Number of slices to send: Send
 

Gregg Bolinger wrote:I want header rows with at least 2 detail rows. Of those 2, each one should point to the product IDs I am specifying.


You can add "group by header.id having count(detail.header_id)>=2" at the end of your query...May you get your answer???...
+Pie Number of slices to send: Send
My take on it with joins (the table needs to be joined twice):

For simplicity I've omitted the join to the products table. Assuming the transaction_detail does not contain product_ids which would not be present in the products table, that join is not necessary (won't eliminate any rows).

(Hope I didn't get it wrong...)
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 995 times.
Similar Threads
JPA question
Querying tables with a one-to-many foreign key relationship
Adding an element to a Treeset( was Plz clear My doubt ......)
JOIN syntax for HQL . Need help!
HQL Query
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:36:39.