MajP
You've got your good things, and you've got mine.
- Local time
- Today, 04:08
- Joined
- May 21, 2018
- Messages
- 9,126
I have a query that has a subquery and works fine. It looks at a percentage in table one for a given ID and compares it to the percentages in table 2 for a given id and returns another value where the difference between the percentages is min.
Gives results like this
I want to make that into a CrossTab with Reg_Number as row header, and NewEPD_TYPE as Column Headers and show Nearest.
The Cross tab complains about the fields within the Subquery. I tried with and without table aliases and get the same error.
I assume this is a limitation to the crosstab where I am trying to transform a value based on a subquery? Can the subquery (join) be done differently?
Code:
SELECT A.reg_number,
A.epd_type AS NewEPD_Type,
A.epd_percent,
(SELECT TOP 1 B.val
FROM table2 AS B
WHERE B.epd = A.epd_type
ORDER BY Abs(B.[perc] - A.epd_percent)) AS Nearest
FROM tblnew AS A;
Gives results like this
I want to make that into a CrossTab with Reg_Number as row header, and NewEPD_TYPE as Column Headers and show Nearest.
Code:
TRANSFORM First(qryvlookup.[Nearest]) AS firstofnearest
SELECT qryvlookup.[reg_number]
FROM qryvlookup
GROUP BY qryvlookup.[reg_number]
PIVOT qryvlookup.[NewEPD_Type];
The Cross tab complains about the fields within the Subquery. I tried with and without table aliases and get the same error.
I assume this is a limitation to the crosstab where I am trying to transform a value based on a subquery? Can the subquery (join) be done differently?