Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
danver
Starting Member
3 Posts |
Posted - 2009-09-14 : 03:54:26
|
Hello All,Can someone help a newb The first script below works however I am having trouble adding a basic join on to it.-- get the latest date for an inventory item.SELECT DOCNUMBR,ITEMNMBR,DOCDATE,TRXQTY,UOFM,UNITCOST,TRXLOCTNFROM IV30300 mWHERE DOCDATE = ( SELECT MAX (DOCDATE) FROM IV30300 WHERE ITEMNMBR = m.ITEMNMBR )and Trxloctn = 'Fleet'ORDER BY ITEMNMBR;I need to join to a table called IV00102 with the key field being ITEMNMBR and grabbing the QTYONHND field. I have tried the below script but it is returning too many values.(ie alot of duplications)Is anybody able to identify where I am going wrong?SELECT DOCNUMBR,m.ITEMNMBR,DOCDATE,TRXQTY,UOFM,UNITCOST,TRXLOCTN,QTYONHND FROM IV30300 mINNER JOIN IV00102ON m.ITEMNMBR = IV00102.ITEMNMBRWHERE DOCDATE = ( SELECT MAX (DOCDATE) FROM IV30300 WHERE ITEMNMBR = m.ITEMNMBR )and Trxloctn = 'Fleet'ORDER BY ITEMNMBR;ThanksDaniel |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-09-14 : 04:34:56
|
| Now, i'm just guessing, but I would say that you have a one to many relationship between IV30300 and IV00102.If this is the case, is there a particular record you want to get (such as the one for each itemnumber with the same date) or do you need to sum the qtyonhnd value?!?Please provide sample data (nneds to be the same type of data as the real table) and expected results. |
 |
|
|
danver
Starting Member
3 Posts |
Posted - 2009-09-14 : 08:33:57
|
| Hello RickD,The IV00300 table stores transactions for the various ITEMNMBR. I want to grab the latest transaction for each ITEMNMBR.Which I can using SELECT DOCNUMBR,ITEMNMBR,DOCDATE,TRXQTY,UOFM,UNITCOST,TRXLOCTNFROM IV30300 mWHERE DOCDATE = ( SELECT MAX (DOCDATE) FROM IV30300 WHERE ITEMNMBR = m.ITEMNMBR )and Trxloctn = 'Fleet'ORDER BY ITEMNMBR;The output is: (ie the latest dated transaction for each ITEMNMBR)DOCNUMBR ITEMNMBR DOCDATE TRXQTY UOFM UNITCOST TRXLOCTN0000016 11000 2007-11-30 00:00:00.000 -1.00000 Each 1 0.00000 FLEET 0000020 11001 2009-02-28 00:00:00.000 -1.00000 Each 1 49.04000 FLEET 0000016 11002 2007-11-30 00:00:00.000 -4.00000 Each 1 31.29000 FLEET Now I need to get various columns from IV00102 which stores the summary data for each item. ie Qty on Hand, Qty Allocated etc. The Key field is ITEMNMBR.IV00102 has the following formatITEMNMBR LOCNCODE RCRDTYPE LSRCPTDT QTYONORD QTYONHND11000 FLEET 2 1900-01-01 00:00:00.000 0.00000 2.0000011001 FLEET 2 2001-08-31 00:00:00.000 0.00000 0.00000Now I need the output to look like: (using ITEMNMBR 11000 as an example)DOCNUMBR ITEMNMBR DOCDATE TRXQTY UOFM UNITCOST TRXLOCTN QTYONHND0000016 11000 2007-11-30 00:00:00.000 -1.00000 Each 1 0.00000 FLEET 2.00000 Sorry, I hope this a bit clearer now.ThanksDaniel |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-14 : 11:29:28
|
| [code]SELECT m1.DOCNUMBR, m1.ITEMNMBR, m1.DOCDATE, m1.TRXQTY, m1.UOFM, m1.UNITCOST,m1.TRXLOCTN, i.QTYONHNDFROM IV30300 m1INNER JOIN (SELECT ITEMNMBR,MAX(DOCDATE) AS Latest FROM IV30300 GROUP BY ITEMNMBR)m2ON m2.ITEMNMBR=m1.ITEMNMBRAND m2.Latest= m1.DOCDATEINNER JOIN IV00102 iON m1.ITEMNMBR = i.ITEMNMBRWHERE Trxloctn = 'Fleet'ORDER BY m1.ITEMNMBR[/code] |
 |
|
|
danver
Starting Member
3 Posts |
Posted - 2009-09-15 : 02:48:30
|
| Hello visakh16,I tried your script but I get the same duplication issues as my script.Thanks for trying.Daniel |
 |
|
|
raj4582
Starting Member
16 Posts |
Posted - 2009-09-15 : 04:02:32
|
| The above query posted by visakh16 seems correct to me.Check that there is no data isue. |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-09-15 : 04:33:54
|
| The only way you will get duplicates is if you have more than 1 ITEMNUMBR for the same DOCDATE, if this is the case and you don't use the time part of the date, then you have issues with your data as how are you supposed to know which is the latest? |
 |
|
|
|
|
|
|
|