we have been reviewing one of our scripts and real...
# suiteql
s
we have been reviewing one of our scripts and realized our SQL is wrong but not sure how o fix . THis sql will only return the amount billed on the po but we need the amount billed on that ( NextTransactionLineLink.nextdoc = 18348140) so if there is one bill per P.O. it works fine but if not there is trouble
Copy code
SELECT
    transactionLine.transaction,
    transactionLine.uniquekey,
    transactionLine.memo,
    transactionLine.item as item_id,
    NVL(transactionLine.quantityshiprecv,0) recive,
    transactionLine.quantitybilled,
    NVL(transactionLine.units,0),
    NVL((SELECT conversionrate FROM unitsTypeUom WHERE internalid = transactionLine.units),0) AS  conversionrate,
NVL(transactionLine.quantityonshipments,0) AS on_boat ,
transactionLine.* ,

    FROM transactionLine
    INNER JOIN NextTransactionLineLink ON
      NextTransactionLineLink.previousline = transactionLine.id AND
      NextTransactionLineLink.previousdoc = transactionLine.transaction
      WHERE NextTransactionLineLink.nextdoc = 18348140
c
@Sim Greenbaum What's wrong with it?
s
i just edit the message
so if there is one bill per P.O. it works fine but if there are two bills per po it will give me the total
c
To make sure I understand, the nextdoc (18348140) is a specific bill?
s
yes
c
And this issue is coming when you have more than one bill associated with a particular PO line?
If that's the case, you would need to get the quantity from the bill instead of the PO line, because one PO line could have an associated line on more than one Bill. For example, let's say the PO line is for qty 100. Bill 18348140 could be for qty 20 and Bill 777777777 could be for qty 80. The query you have above will pull the full quantitybilled from any PO line that is associated with Bill 18348140.
s
correct but i need the uniquekey from the po to create an inbound shipment and qty from the bill
hence I'm confused how to have my cake and eat it
we are creating a inbound shipment off th ebill
c
Join the transactionline table again as something like
v_bill
and get the quantity from the matching bill line
instead of getting the quantitybilled from the PO
Something like this (untested)
s
so add another join after the where ?
c
No, sorry. I missed the WHERE due to its matching indentation to the lines above. Put the JOIN where a join belongs.
edited the snippet
s
thowing syntax error
c
I'll poke at it when I can spend some time to run it in my environment
Looking at it, I goofed the CTE. Fixed in the snippet. I can't guarantee there won't be another issue though.
s
what does CTE stand for ?
c
Oh, also, since you're selecting transactionLine.*, you might want to rename that v_bill.quantity to something other than quantity or quantitybilled
Common Table Expression - that WITH statement at the top
s
turns out that for some reason THE SQL EDITOR IS THROWING THE SYNTAX ERORR NOT YOUR CODE !!!
the issue this will only pull the first line of the bill
c
When I run this query in my environment, it pulls all bill lines - e.g. on a bill with 2 lines, both appear. Is it possible that on the bill you're referencing a line was added that's not on the PO?
s
i am having two isssues first it is only pulling one line of the bill both bill and po have 10 lines 2nd for example if i run this 2nd bill it is chossing the amount of the first biill
c
Did you change the ID in both places in the query?
when looking at a second bill
s
lo am i sleep
hold on
my old rule never write sql at 6pm when you usally leave work at 530
c
It's a solid rule 😄
s
thanx that was close