Monday, February 20, 2012

Question on a join

Well i am rather new to sql about a month into it and not sure if what
i am trying to do is impossible or not. On my join i am trying to get
the result of a specific products most recent date and time as well as
who made the move. I have everything fine it will give me a result of
about "30 rows" but when i add in the ACCT_CURR_OPER_INIT "Employees
Initials" It will give me all of the results instead of just the most
recent about "75 row". I know it is because there are several unique
initials in that column so it gives me the result of all of them. What
me question is though is if there is a way to be able to get the
ACCT_CURR_OPER_INIT and still be able to give me the most recent
result?
Thanks in advance for any help..
SELECT a.WH_LOC, A.MODEL_NO, A.SERIAL_NO, a.STORE_LOC, a.RSN_CODE,
ISNULL(CONVERT(VARCHAR,b.ACCT_TRANS_DATE,101),''),
ISNULL(CONVERT(VARCHAR,b.ACCT_TIME,108),'') --, ACCT_CURR_OPER_INIT
FROM PRODUCT_INV a
LEFT OUTER JOIN (
SELECT PROD, ACCT_SERIAL_NO, CAST( MAX(ACCT_TRANS_DATE) AS
ACCT_TRANS_DATE MAX(CONVERT( DATETIME, ACCT_TIME,108))) AS ACCT_TIME
--, ACCT_CURR_OPER_INIT
FROM PRODUCT_INV_ACTIVITY
GROUP BY PROD, ACCT_SERIAL_NO --, ACCT_CURR_OPER_INIT
) b
ON a.MODEL_NO = b.PROD and a.SERIAL_NO = b.ACCT_SERIAL_NO
WHERE (a.STORE_LOC = 'WH.ADJUST' OR a.RSN_CODE = 'WFD') AND a.WH_LOC =
'88'
GROUP BY a.MODEL_NO, a.SERIAL_NO, a.STORE_LOC, a.RSN_CODE,a.WH_LOC,
b.ACCT_TRANS_DATE, b.ACCT_TIME
ORDER BY a.MODEL_NO, a.SERIAL_NOSorry guys i put an old query in my original post here is the correct
one. Thanks...
SELECT a.WH_LOC, A.MODEL_NO, A.SERIAL_NO, a.STORE_LOC, a.RSN_CODE,
ISNULL(CONVERT(VARCHAR,b.ACCT_TRANS_DATE,101),''),
ISNULL(CONVERT(VARCHAR,b.ACCT_TIME,108),'') --, ACCT_CURR_OPER_INIT
FROM PRODUCT_INV a
LEFT OUTER JOIN (
SELECT PROD, ACCT_SERIAL_NO, MAX(ACCT_TRANS_DATE) AS
ACCT_TRANS_DATE MAX(CONVERT( DATETIME, ACCT_TIME,108)) AS ACCT_TIME
--, ACCT_CURR_OPER_INIT
FROM PRODUCT_INV_ACTIVITY
GROUP BY PROD, ACCT_SERIAL_NO --, ACCT_CURR_OPER_INIT
) b
ON a.MODEL_NO = b.PROD and a.SERIAL_NO = b.ACCT_SERIAL_NO
WHERE (a.STORE_LOC = 'WH.ADJUST' OR a.RSN_CODE = 'WFD') AND a.WH_LOC =
'88'
GROUP BY a.MODEL_NO, a.SERIAL_NO, a.STORE_LOC, a.RSN_CODE,a.WH_LOC,
b.ACCT_TRANS_DATE, b.ACCT_TIME
ORDER BY a.MODEL_NO, a.SERIAL_NO

No comments:

Post a Comment