Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble designing a query 1

Status
Not open for further replies.

teach314

Technical User
Jul 29, 2011
183
CA
hello - I need assistance with a query (or possible VBA). Here's the situation...

I have a table like this: (each X value appears at least twice)


[pre]
X Y
==========
1000 1
1000 31

1004 4
1004 18

1023 8
1023 79
1023 83

1101 5
1101 8
etc...[/pre]


I need to prepare a non-normalized table that shows PAIRS of Y values having the same X value, like this...


[pre]
X Y1 Y2
===================
1000 1 31
1004 4 18

1023 8 79
1023 8 83
1023 79 83


1101 5 8
etc...[/pre]


Almost all the data comes in groups of exactly 2 Y values, where a XTab can accomplish the goal.
But if a triplet of Y values appears, I need to list the resulting 3 pairs (as in X = 1023 above).
There are even a few rare cases where a quartet of Y values appear, so my final output would need
to list the resulting 6 possible pairings.


I'd really appreciate some help with this.

Many thanks
Teach314

 
Hi,

So what's the business case for this requirement?

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
hi Skip - well, I don't know about business case, but it's a small part of a research project where I need to identify PAIRS, then use those as inputs for further work.

If the objection is to the non-normalization, I'm fine with that. The only important thing is the IDENTIFICATION of PAIRS form the input table.
A much better output would be something like...

[pre]
X PairID Rank Y
==========================
1000 1 1 1
1000 1 2 31

1004 1 1 4
1004 1 2 18

1023 1 1 8
1023 1 2 79

1023 2 1 8
1023 2 2 83

1023 3 1 79
1023 3 2 83


1101 1 1 5
1101 1 2 8

etc...
[/pre]


Thanks for any hints
Teach314
 
Well now you have really doubled down on the doubtfulness of your requirement.

You have stated two different output results!

This is why I asked for a business case, in order to determine what is required. This includes the logic needed achieve an end result. You apparently have not solidified your thought process.

You must provide a clear, concise, cohesive and complete requirement not just an example, because if I went with your first example, the result would have not REALLY met the requirement behind your second example. And looking at your second example, I might interpret what I think you meant and still miss the mark. I need more than an example now.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
hi again Skip

I think I misinterpreted your 1st request for a 'business case' as asking for a justification for presenting my output in non-normalized form. So, I tried to restate the problem using a normalized output table, requiring a few extra fields as shown above. In fact, either of these suggested outputs would meet my requirements.

Skip - you asked me to provide 'the logic needed to achieve an end result', but that is precisely what I'm in need of! I'm fairly certain I can write the code, but I need some hints as to how I could go about achieving my goal.

In the spirit of your '4 Cs approach: Clear, Concise, Cohesive, and Complete', let me have one more shot at being clear. I'll return to the original posting...

INPUT: a table with 2 Long fields (X and Y), ordered by X, then Y. The PK is (X,Y). X values usually appear exactly twice, matched with 2 DISTINCT Y values. Occasionally, X values are matched with 3, or, rarely, 4 DISTINCT Y values.).

OUTPUT: (I'll go back to the original output request here) I need a table with three fields, X, Y1, Y2. (all Long, PK is (X, Y1, Y2))

The X column holds all DISTINCT values of X, in ASC order. The Y1 and Y2 fields hold a PAIR of Y values associated with that X. It is convenient for Y1 < Y2.
In the vast majority of cases, there are just 2 Y values associated with the X value, so all of this information is shown in one record.
BUT, if the X value is associated with, say, 3 Y values, then we need n(n-1)/2 = 3 records to hold all of the three possible PAIRS. (the order of records shown in my orignal posting works well, but isn't mandatory).
In very few instances, the X value is associated with 4 Y values, so we'd need n(n-1)/2 = 6 records to hold all of the six possible PAIRS of Y values.



Thanks in advance for any guidance...
Teach314




 
Code:
SELECT A.x, A.Y AS Y1, B.Y AS Y2
FROM tbl2 AS A, tbl2 AS B
WHERE [A].[x]=[B].[x] AND [A].[Y]<[B].[y]
ORDER BY A.x, A.Y, B.Y;
 
Thanks MajP. That works perfectly, nicely handling cases where, say, 4 Y values need to be split into 6 Pairs.
Teach314
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top