>>I see another problem in your query. You already got r.*, why you're getting r.Status again at the end of it?
>
>Like I said, I used what you gave me earlier. I don't understand alot of what you have here so I can't comment. All I want it the first record from each batch. I seems to me that this should be alot easier to do than this.
Try now - I believe it should work
select * from (SELECT r.*,
>>>> p.PartNo,
>>>> p.Description,
>>>> u.FullName,
>>>> w.Caption AS Warehouse,
>>>> Row_Number() OVER (Partition by BatchID ORDER BY p.PartNo, r.SerialNo DESC) as RowNum
>>>> FROM Returns r
>>>> JOIN Parts p ON p.PartId = r.PartId
>>>> JOIN Warehouses w ON w.WarehouseId = r.WarehouseId
>>>> JOIN Users u ON u.UserId = r.UserId
>>>> WHERE r.WarehouseId = @WarehouseId AND
>>>> (@Status IS NULL or r.Status = @Status)) OrderedResultS
>>>> where OrderedResultS.RowNum = 1
>>>> ORDER BY BatchId
The query is very simple - I was just making some typos.
Or review the link I posted in other message for alternative solutions.
If it's not broken, fix it until it is.
My Blog