I have a subquery that gets the max of quote_date and requery the result to
get the max of quote_id for the same part_id, vendor_id and quote_date. Thi
s
query is working fine and I want to save the result in a temp table but the
first 3 codes won't work. Thanks for the help.
SELECT v1.*, vc.CNT_VEND
INTO #vend1_pass1
FROM(
SELECT * FROM MA_VEND_CNT vc
JOIN(
SELECT DISTINCT mq.PROP_ITEM_ID, mq.TOTAL_QTY, MAX(q.QUOTE_ID) AS QUOTE_ID
FROM MA_QUOTE_PROP q
JOIN
(SELECT PROP_ITEM_ID, TOTAL_QTY, VEND_ID, MAX(QT_DT) AS MAX_QT_DT FROM
MA_QUOTE_PROP
GROUP BY PROP_ITEM_ID, TOTAL_QTY, VEND_ID
)mq
ON q.PROP_ITEM_ID = mq.PROP_ITEM_ID
AND q.TOTAL_QTY = mq.TOTAL_QTY
and q.VEND_ID = mq.VEND_ID
and q.QT_DT = mq.MAX_QT_DT
GROUP BY mq.PROP_ITEM_ID, mq.TOTAL_QTY
)v1
--ORDER BY mq.PROP_ITEM_ID, mq.TOTAL_QTY) v1
ON v1.PROP_ITEM_ID = vc.PROP_ITEM_ID
AND v1.TOTAL_QTY = vc.TOTAL_QTY
WHERE vc.CNT_VEND=1
)you want to alias it.
SELECT v1.*, vc.CNT_VEND
INTO #vend1_pass1
FROM(
SELECT * FROM MA_VEND_CNT vc
JOIN(
SELECT DISTINCT mq.PROP_ITEM_ID, mq.TOTAL_QTY, MAX(q.QUOTE_ID) AS QUOTE_ID
FROM MA_QUOTE_PROP q
JOIN
(SELECT PROP_ITEM_ID, TOTAL_QTY, VEND_ID, MAX(QT_DT) AS MAX_QT_DT FROM
MA_QUOTE_PROP
GROUP BY PROP_ITEM_ID, TOTAL_QTY, VEND_ID
)mq
ON q.PROP_ITEM_ID = mq.PROP_ITEM_ID
AND q.TOTAL_QTY = mq.TOTAL_QTY
and q.VEND_ID = mq.VEND_ID
and q.QT_DT = mq.MAX_QT_DT
GROUP BY mq.PROP_ITEM_ID, mq.TOTAL_QTY
)v1
--ORDER BY mq.PROP_ITEM_ID, mq.TOTAL_QTY) v1
ON v1.PROP_ITEM_ID = vc.PROP_ITEM_ID
AND v1.TOTAL_QTY = vc.TOTAL_QTY
WHERE vc.CNT_VEND=1
)XXX
-oj
"danlin" <danlin@.discussions.microsoft.com> wrote in message
news:73BB96F3-0359-43CE-A21D-E47D9CFD9B9F@.microsoft.com...
>I have a subquery that gets the max of quote_date and requery the result to
> get the max of quote_id for the same part_id, vendor_id and quote_date.
> This
> query is working fine and I want to save the result in a temp table but
> the
> first 3 codes won't work. Thanks for the help.
>
> SELECT v1.*, vc.CNT_VEND
> INTO #vend1_pass1
> FROM(
> SELECT * FROM MA_VEND_CNT vc
> JOIN(
> SELECT DISTINCT mq.PROP_ITEM_ID, mq.TOTAL_QTY, MAX(q.QUOTE_ID) AS QUOTE_ID
> FROM MA_QUOTE_PROP q
> JOIN
> (SELECT PROP_ITEM_ID, TOTAL_QTY, VEND_ID, MAX(QT_DT) AS MAX_QT_DT FROM
> MA_QUOTE_PROP
> GROUP BY PROP_ITEM_ID, TOTAL_QTY, VEND_ID
> )mq
> ON q.PROP_ITEM_ID = mq.PROP_ITEM_ID
> AND q.TOTAL_QTY = mq.TOTAL_QTY
> and q.VEND_ID = mq.VEND_ID
> and q.QT_DT = mq.MAX_QT_DT
> GROUP BY mq.PROP_ITEM_ID, mq.TOTAL_QTY
> )v1
> --ORDER BY mq.PROP_ITEM_ID, mq.TOTAL_QTY) v1
> ON v1.PROP_ITEM_ID = vc.PROP_ITEM_ID
> AND v1.TOTAL_QTY = vc.TOTAL_QTY
> WHERE vc.CNT_VEND=1
> )
>
Wednesday, March 28, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment