1

Here's the code:

SELECT  RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,  
          ip.dblItemPAmount, MAX(ip.dtmItemPasOf) AS EXR 
FROM TBLREQUESTDETAILS RD,tblitem I,tblvendor v,tblitemunit iu,tblitemprice ip` 
WHERE RD.strReqDItemCode = I.strItemCode 
AND RD.strReqDItemUnitCode = iu.strItemUnitCode 
AND RD.strReqDVendCode = v.strVendCode 
AND i.strItemCode = ip.strItemPItemCode 
and RD.strReqDReqHCode = 'RQST121' 
GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName, ip.dblItemPAmount
ORDER BY EXR desc ;

AND Here's The result: enter image description here

What should I do If I want to fetch the current price for each itemname,vendorname and itemunit?? I want to fetch only those rows who's price is the Latest... Help me please those with boxes are the rows that i want to fetch

1
  • I think you must remove RD.INTREQDQUANTITY from group by if you want the result for this combination (itemname,vendorname and itemunit) Commented Oct 15, 2016 at 7:17

1 Answer 1

1

You can use where in the grouped value (and use explicict join notatio)

SELECT  RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,  
          ip.dblItemPAmount, ip.dtmItemPasOf AS EXR 
FROM TBLREQUESTDETAILS RD
INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode 
INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode 
INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode 
INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode 
WHERE RD.strReqDReqHCode = 'RQST121' 
and ( RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName, ip.dtmItemPasOf) 

    in ( SELECT  RD.INTREQDQUANTITY, I.strItemName, v.strVendName, iu.strItemUnitName,  
          MAX(ip.dtmItemPasOf) 
        FROM TBLREQUESTDETAILS RD
        INNER JOIN tblitem I ON RD.strReqDItemCode = I.strItemCode 
        INNER JOIN tblvendor v ON D.strReqDVendCode = v.strVendCode 
        INNER JOIN tblitemunit iu ON RD.strReqDItemUnitCode = iu.strItemUnitCode 
        INNER JOIN tblitemprice ip ON i.strItemCode = ip.strItemPItemCode 
        WHERE RD.strReqDReqHCode = 'RQST121'  
        GROUP BY RD.INTREQDQUANTITY,I.strItemName,v.strVendName,iu.strItemUnitName 
    )
 ORDER BY EXR desc ;

(and use explicict join notation .. i think is more readable)

Sign up to request clarification or add additional context in comments.

8 Comments

0 rows returned :(
I mean only returns the row with the highest dtmPAsOf
Looking to you sample seems which is the column you want .. ?
I want to fetch those that are inside the black box, green box and red box.Which are row which contains the latest price base on date
I'm sorry but The highest dtmPAsOf is not the latest date ... and the the related rows should contain the last price (alias dblItemPAmount) based on date ... ? ..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.