unknown
2009-09-11 10:59:22 UTC
I have a fairly complicated report, which I have no doubt
can be redone in a simpler way (please do if you have the
time!). The SQL is below, and works without a problem. The
issue I have is this is one out of 3 queries which I union
together, all returning the same data in the first 2
columns, and different data in the other 5 columns.
My plan was to union the 3 queries together with this one in
the middle. Then I would add a retrieval argument:
WHERE ab1.as_code = :As_Code
I planned to add these to all 3 queries once union'd.
However, this particular one will not accept it. The error
message I get is:
"The multi-part identifier "t4.tr_location_changed" could
not be bound"
Any help is much appreciated!
SELECT 'Column 1' = a1.as_code ,
'Column 2' = a1.as_desc ,
'Column 3' =
(
CASE
WHEN t1.tr_from_loc_id IS NULL THEN
(
CASE
WHEN
(
SELECT COUNT(t2.trace_no)
FROM trace AS t2
WHERE t2.as_id = t1.as_id AND
t2.trace_no < t1.trace_no AND
t2.tr_location_changed = 'Y'
) = 0 THEN 'Asset Created'
ELSE
(
SELECT l3.objectkey
FROM location AS l3 JOIN trace AS t3
ON l3.objectid = t3.tr_to_loc_id
WHERE t3.trace_no =
(
SELECT MAX(t4.trace_no)
FROM trace AS t4
WHERE t4.trace_no < t1.trace_no AND
t4.as_id = t1.as_id AND
t4.tr_location_changed = 'Y'
)
)
END
)
ELSE l1.objectkey
END
),
'Column 4' = l2.objectkey ,
'Column 5' = t1.tr_trace_by ,
'Column 6' = t1.ddate ,
'Column 7' = 'Location Change'
FROM assets AS a1 JOIN trace AS t1
ON a1.as_id = t1.as_id
JOIN location AS l1
ON ISNULL(t1.tr_from_loc_id, -1) = l1.objectid
JOIN location AS l2
ON ISNULL(t1.tr_to_loc_id, -1) = l2.objectid
WHERE a1.as_code = :Asset_Code <<<<<<<This line breaks
it
can be redone in a simpler way (please do if you have the
time!). The SQL is below, and works without a problem. The
issue I have is this is one out of 3 queries which I union
together, all returning the same data in the first 2
columns, and different data in the other 5 columns.
My plan was to union the 3 queries together with this one in
the middle. Then I would add a retrieval argument:
WHERE ab1.as_code = :As_Code
I planned to add these to all 3 queries once union'd.
However, this particular one will not accept it. The error
message I get is:
"The multi-part identifier "t4.tr_location_changed" could
not be bound"
Any help is much appreciated!
SELECT 'Column 1' = a1.as_code ,
'Column 2' = a1.as_desc ,
'Column 3' =
(
CASE
WHEN t1.tr_from_loc_id IS NULL THEN
(
CASE
WHEN
(
SELECT COUNT(t2.trace_no)
FROM trace AS t2
WHERE t2.as_id = t1.as_id AND
t2.trace_no < t1.trace_no AND
t2.tr_location_changed = 'Y'
) = 0 THEN 'Asset Created'
ELSE
(
SELECT l3.objectkey
FROM location AS l3 JOIN trace AS t3
ON l3.objectid = t3.tr_to_loc_id
WHERE t3.trace_no =
(
SELECT MAX(t4.trace_no)
FROM trace AS t4
WHERE t4.trace_no < t1.trace_no AND
t4.as_id = t1.as_id AND
t4.tr_location_changed = 'Y'
)
)
END
)
ELSE l1.objectkey
END
),
'Column 4' = l2.objectkey ,
'Column 5' = t1.tr_trace_by ,
'Column 6' = t1.ddate ,
'Column 7' = 'Location Change'
FROM assets AS a1 JOIN trace AS t1
ON a1.as_id = t1.as_id
JOIN location AS l1
ON ISNULL(t1.tr_from_loc_id, -1) = l1.objectid
JOIN location AS l2
ON ISNULL(t1.tr_to_loc_id, -1) = l2.objectid
WHERE a1.as_code = :Asset_Code <<<<<<<This line breaks
it