SQL Query for generating possible combinations

Dear Experts,

I have a table with three fields: SeriesID, PartTypeID and PartID.
(showing records only for SeriesID = 1)
1      1      3
1      1      7
1      2      6
1      2      8
1      3      4
1      4      5

I'm looking for a query that will return all possible combinations of PartID subject to constraints that 1) every combination must have same Series and 2) one and only one of each PartType.  


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Given the data sample you have posted, what would such a data set look like?
Rey Obrero (Capricorn1)Commented:
Gustav BrockCIOCommented:
As to your specification 1) and 2), there isn't anything to combine - only to exclude - as this is the result you will obtain:

1      1      3
1      2      6
1      3      4
1      4      5

all possible combinations of PartID &
1) every combination must have same Series
2) one and only one of each PartType.  

 PartID (6 of)

PartTypeID (4 of)

SeriesID (1 of)

Cartesian Product
6*4*1 = 24 combinations
from (select distinct PartID from tbl)
cross join (select distinct PartTypeID from tbl)
cross join (select SeriesID from tbl where SeriesID = 1)

Open in new window

Don't think the intention is that Rule 2 overrides all others, but if it does then you would exclude some PartID's (which ones you exclude might be up for debate).

nb: in Access you need more parentheses that I have shown (I think).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomfolinsbeeAuthor Commented:
Apologies for the late reply, I must have missed the response in my inbox.

This is what I did:
I made four copies of the table, and joined them on seriesID, then filtered on the PartTypeID (x 4).  This seemed to give the desired result, but I will try the query you suggested so that the solution would be dynamic.

Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.