I am still getting my head wrapped around using BAQ’s to return data.
I have a list of parts
Part A
Part B
Part C
And I need to return the most recent transaction that was a purchase. I know I can filter part tran by that trantype, but how do I only return the most recent?
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
Select SysRowID,
ROW_NUMBER() OVER (PARTITION BY partnum ORDER BY SysRevID DESC ) As _RowNum
from Dbo.PartTran
) 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.
the 1=1 is an old habit. it is something I do while testing queries. you can ignore it.
1 =1 is always true.
then I add
and partnum = ‘part’
When I am done I comment out the and partnum line. This is for when I have multiple conditions. Then I don’t have to rewrite the query when I remove the top condition.
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.