INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Trouble designing a query

Trouble designing a query

(OP)
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)


    X      Y
   ==========
   1000     1
   1000    31

   1004     4
   1004    18

   1023     8
   1023    79
   1023    83

   1101     5
   1101     8
   etc... 


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


    X      Y1       Y2
   ===================
   1000     1       31
   1004     4       18

   1023     8       79
   1023     8       83
   1023    79       83

   1101     5        8
   etc... 


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

RE: Trouble designing a query

Hi,

So what's the business case for this requirement?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Trouble designing a query

(OP)
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...

   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...    
 


Thanks for any hints
Teach314

RE: Trouble designing a query

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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Trouble designing a query

(OP)
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




RE: Trouble designing a query

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; 

RE: Trouble designing a query

(OP)
Thanks MajP. That works perfectly, nicely handling cases where, say, 4 Y values need to be split into 6 Pairs.
Teach314

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close