Linking to PartTran is always crappy but if you choose the right index you can speed things up.
Create a subquery with PartTran and sort by date descending. In the subquery options choose the TOP 1 records. You may want to filter out non-inventory transactions (cost adj, etc). Your top-level query then links to the subquery. Use the Data Dictionary viewer to find the most efficient index to use. (Company, PartNum, TranDate or something like that.)
The query below is also a good place to start. This will bring back the most recent entered transactions in the system by partnum.
You can always make the b table more to what you are looking for by adding the TranType criteria there.
Select a.PartNum, a.TranType, a.TranDate, a.TranQty, b._RowNum
From Dbo.PartTran a
Inner Join ( -- Use Left Join if the records missing from Table B are still required
ROW_NUMBER() OVER (PARTITION BY partnum ORDER BY SysRevID DESC ) As _RowNum
) b On b.SysRowID = a.SysRowID
Where 1 =1 and b._RowNum = 1
You would recreate this SQL using the BAQ editor. It can be challenging if you don’t know SQL. But you can compare what shows up on the general tab for the BAQ this to see if you are on the right track. The one Ken Posted is a little advanced.
These are the fields you grab from part tran table in field selection, except for RowNum. That’s a calculated field.
You won’t get more than one row, or whatever rows you specified if you do that. If you use the TOP it doesn’t specify Top part, or top “insert value here”, just the overall top. So that works quite differently.
I think he’s talking about a different technique where you pull the subquery in through the calculated field, and like them through subquery criteria instead of joining tables the normal way. It works great if you are returning a single value. It doesn’t work if you want more than one value from the table you are pulling from. This thread explains that technique.
Oh no. Ask my wife, or Chris Conn. I do make mistakes… All. The. Time. Brandon is correct here. Better to use the ROW_NUMBER() OVER (PARTITION BY… logic in this case. Mine works if you’re looking at one record but not a group.