Dear Phil:
First, an impression. If you're used to working with T-SQL, stick with
that. It's superior. Yeah, I'd know I have to duck when I say that. But
Jet isn't on a par with MSDE or full SQL Server. No way.
OK, next question. Is the data in the INSID and OIN columns, taken
together, unique? Can it be duplicate? If it can be duplicate, would it be
unique if we add Role and/or Type?
Are you familiar with ranking within a group. Something like:
SELECT INSID, OIN, Role, Type
(SELECT COUNT(*)
FROM YourTable T1
WHERE T1.INSID = T.INSID
AND T1.OIN < T.OIN) AS Rank
FROM YourTable T
Such a ranking would be used to identify into which set of columns (your
columns sets of Type#/ID# numbered 1-5) each row of data would be placed.
If I were putting this into T-SQL, I'd write:
SELECT INSID, OIN,
(SELECT Type
FROM YourTable T1
WHERE T1.INSID = T.INSID
HAVING (SELECT COUNT(*)
FROM YourTable T2
WHERE T2.INSID = T1.INSID
AND T2.OIN < T1.OIN) = 0) AS Type1
FROM YourTable T
This uses a 2 level nested correlated subquery (whew!) to extract the Type
of the row with the lowest (rank = 0) row within the INSID group.
Similarly, you can extract the ID1 value from the OIN column. Then,
changing the Rank from 0 to 1, you can extract the next Type/ID values for
"2", then "3", "4", and "5".
However, if INSID / OIN are not, together, unique (and preferably guaranteed
to be so by constraint, i.e. unique index) then the subquery will find two,
or more, rows, which generates an error. If you could provide uniqueness by
adding additional columns, as I suggested earlier, then this can be
overcome. Otherwise, you're asking the query to arbitrarily specify which
row to place in a rank ahead of another which has tied with it. No can do.
Queries are picky about being asked to arbitrarily choose between to equal
options.
The above, I believe, is within the capacity of Jet to run, as well. I
particularly made the reference from the inner subquery to be to the
intermediate subquery when I wrote:
WHERE T2.INSID = T1.INSID
AND T2.OIN < T1.OIN) = 0) AS Type1
If you use this:
WHERE T2.INSID = T.INSID
AND T2.OIN < T1.OIN) = 0) AS Type1
then you have made a reference to the outer query from the innermost, a 2
level jump so to speak, and I have never seen Jet accept this one. T-SQL
has absolutely no problem with this, by comparison, so I consider it to be
"good SQL" but it exceeds Jet limitations. Interesting little trick, eh?
Try it both ways with Jet and see!
More about you how could easily continue to use T-SQL to do your work, even
if the data is in a Jet database, if you request me to advise you on this.
Just let me know and I'll add that discussion.
Do you feel we're getting anywhere?
Tom Ellison