0

I have the following query, selected for a single transaction id, to sum up the total of cost of sales from our accounting system. This query will need to sum up costs across all tranid's but I'm just selecting one particular tranid below.

The problem is for every transaction, I'm getting multiple transaction numbers as the system stores vendor bills that are deleted and replaced for a given transaction (or tran id).

  • If I remove transaction_number and sum up the data, I get 45k here instead of 22k
  • In other tranid's, there may be multiple Cost of Sales lines that need to be added but still only for a single VendBill which is the one with the greatest number for a given tranid.
  • A single tranid can have 1 or more Vendbills, all ending in a number with the biggest numbered vendbill being the only one that should be counted for a given tranid (in this case, only vendbill34246 should be counted so that tranid 330167473 would equal 22,867 vs 45k when added up with all other tranids)

The original query is

select ift.tranid, ift.transaction_number, a.full_name, sum(tl.amount) 
tranamt
   from ns.tbill ift
    inner join ns.transaction_lines tl on ift.transaction_id = tl.transaction_id
    inner join ns.accounts a on tl.account_id = a.account_id
    inner join ns.locations l on l.location_id = ift.location_id 
 where tranid = '330167473' and a.full_name like '%Cost of Sales%' 
 group by ift.tranid, l.full_name, ift.transaction_number

and the result I'm getting now is

tranid      transaction_number  full_name   tranamt
330167473   VENDBILL34246   Cost of Sales   22867.92
330167473   VENDBILL34429   Cost of Sales   22867.92

The desired result would be as follows

330167473   VENDBILL34429   Cost of Sales   22867.92

I only want to return one line, but am not sure how to apply Max to transaction_number such that only one line is returned for a given tranid. As you can, it's currently duplicating the amount for this particular record and does so when adding up all tranid's for records that have multiple vendbill's.

ATTEMPTS I'VE MADE SO FAR

Applying MAX in the following way produces the error "An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference." so it's unclear how to properly apply max in this case.

select ift.tranid, ift.transaction_number, a.full_name, sum(tl.amount) 
tranamt
  from ns.tbill ift
    inner join ns.transaction_lines tl on ift.transaction_id = 
     tl.transaction_id
    inner join ns.accounts a on tl.account_id = a.account_id
    inner join ns.locations l on l.location_id = ift.location_id 
where tranid = '330167473' and a.full_name like '%Cost of Sales%' and 
  ift.transaction_number >= (select max(ift.transaction_number) from 
  ns.tbill)
group by ift.tranid, a.full_name, ift.transaction_number

If I just add MAX to transaction_number itself, I get a single VENDBILL but the transaction amount is still 44k.

select ift.tranid, ift.transaction_number, a.full_name, sum(tl.amount) tranamt
 from ns.tbill ift
   inner join ns.transaction_lines tl on ift.transaction_id = 
     tl.transaction_id
   inner join ns.accounts a on tl.account_id = a.account_id
   inner join ns.locations l on l.location_id = ift.location_id 
     where tranid = '330167473' and a.full_name like '%Cost of Sales%' 
     and ift.transaction_number >= (select max(ift.transaction_number) 
     from ns.tbill)
   group by ift.tranid, a.full_name, ift.transaction_number

Result:

 330167473  VENDBILL34429   Cost of Sales   45735.84

Help with the right logic here would be great

RalphBiggerton
  • 179
  • 5
  • 19
  • [Here](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) is a great place to start. – Sean Lange Dec 03 '18 at 19:18
  • Thank you - I've revised my question to follow the format – RalphBiggerton Dec 03 '18 at 20:45
  • I believe you asked this already this morning where it was flagged as a duplicate to https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results – JNevill Dec 03 '18 at 20:48
  • @JNevill - I reviewed the question posted as the duplicate, and this is not the same question as the one posted so I clarified further – RalphBiggerton Dec 03 '18 at 20:49
  • As for your WHERE clause, I believe `ift.transaction_number = (select max(transaction_number) from ns.tbill WHERE ift.tranid = tbill.tranid)` should do the trick. – JNevill Dec 03 '18 at 20:49
  • Is there some reason I'm not seeing why this isn't a simple SELECT TOP 1... solution??? – Tab Alleman Dec 03 '18 at 20:52
  • @TabAlleman - yes, the number of Top N rows will vary among tranid's - some will have more vendbill rows than others, so I need to isolate the vendbill within a given tranid that has the highest number and sum up several cost of sales fields for the given vendbill for that particular tranid – RalphBiggerton Dec 03 '18 at 20:54
  • @Danimov82 Ok, but is there some reason you can't use your current query as a CTE or derived table and do a SELECT TOP 1 on that query to get your desired result? – Tab Alleman Dec 03 '18 at 20:58
  • @TabAlleman - So for a given tranid, you're correct in that I do just want 1 transaction_number to be used for the cost of sales sum and therefore could have a sub query that filters out the list of transaction numbers to just the vend bill with the largest number. It's no different I imagine than using MAX if this was an integer I was referring to. I'm not sure though, how that query might look and would appreciate your help in crafting a working example given the queries I've shared, if you could assist in providing that example as a potential answer / response. – RalphBiggerton Dec 03 '18 at 21:04

1 Answers1

0

@JNevill posted the following answer in the comments, which solved my issue

ift.transaction_number = (select max(transaction_number) from ns.tbill 
WHERE ift.tranid = tbill.tranid)
RalphBiggerton
  • 179
  • 5
  • 19