Reputation: 21
The following SQL Query finds orders matching certain requirements and then creates a new order (InvNum) with the invoice lines (_btblInvoiceLines) at this stage it has only created basic information.
DECLARE @gid varchar(50) SELECT @gid = newid()
INSERT INTO InvNum (DocType,DocState,AccountID,ucIDSOrdTempInvID)
SELECT 4,1,AccountID,@gid
FROM (SELECT DISTINCT AccountID
FROM InvNum
WHERE ubIDSOrdConsolOrder = '1'
AND DocState = '1'
AND DocType <> '5') A
INSERT INTO _btblInvoiceLines (iInvoiceID, iStockCodeID, fQuantity)
SELECT AutoIndex, iStockCodeID, qty
FROM (SELECT AutoIndex, iStockCodeID, qty
FROM (SELECT AutoIndex,AccountId,ucIDSOrdTempInvID
FROM InvNum
WHERE ucIDSOrdTempInvID = @gid) T1
INNER JOIN (SELECT B.iStockCodeID,A.AccountID,sum(B.fQuantity) AS qty
FROM (SELECT *
FROM InvNum
WHERE ubIDSOrdConsolOrder = '1'
AND DocState = '1'
AND DocType <> '5') A
INNER JOIN (SELECT *
FROM _btblInvoiceLines) B ON A.AutoIndex = B.iInvoiceID
GROUP BY iStockCodeID, AccountID) T2 ON T1.AccountId = T2.AccountID) T3
After this the InvNum columns are all updated, this works fine. My issue is with the following, which is updating the _btblInvoiceLines of the new order and new lines with that of the old orders, that which matches: WHERE ubIDSOrdConsolOrder = '1' AND DocState = '1' AND DocType <> '5'
This FROM clause is what is incorrect:
SELECT D1.*
FROM (SELECT A.*
FROM _btblInvoiceLines A
INNER JOIN (SELECT max(idInvoiceLines) as idInvoiceLines, iStockCodeID
FROM _btblInvoiceLines
GROUP BY iStockCodeID) B ON A.idInvoiceLines = B.idInvoiceLines) D1
INNER JOIN (SELECT *
FROM _btblInvoiceLines I1
INNER JOIN (SELECT accountid,autoindex,ucIDSOrdTempInvID
FROM InvNum
WHERE ucIDSOrdTempInvID = @gid) I2 ON I1.iInvoiceID = I2.AutoIndex) D2 ON D1.iStockCodeID = D2.iStockCodeID
WHERE _btblInvoiceLines.idInvoiceLines = D2.idInvoiceLines
I'm at a lost of how to make D1.* pull out the information from the last original _btblInvoiceLines that was added into the new orders, D2 . The only way I believe I can do this is using the following SELECT DISTINCT AccountID,ubIDSOrdConsolOrder FROM InvNum WHERE ubIDSOrdConsolOrder = '1' AND DocState = '1' AND DocType <> '5'
but I'm at a loss of how to put this in correctly. Sorry if this is a simple question, but I have been looking and attempting to resolve this issue for 2 days now without any luck.
Thank you.
Upvotes: 1
Views: 199
Reputation: 21
I had the INNER JOIN
in the wrong order. I did the following and this now works correctly:
SELECT D1.*
FROM (SELECT DISTINCT AccountID, AutoIndex FROM InvNum WHERE ubIDSOrdConsolOrder = '1' AND DocState = '1' AND DocType <> '5') A1
INNER JOIN (SELECT * FROM _btblInvoiceLines) D1 ON A1.AutoIndex = D1.iInvoiceID----
INNER JOIN (SELECT max(idInvoiceLines) as idInvoiceLines, iStockCodeID FROM _btblInvoiceLines GROUP BY iStockCodeID) I1 ON D1.iStockCodeID = I1.iStockCodeID
INNER JOIN (SELECT * FROM _btblInvoiceLines I1 INNER JOIN (select accountid,autoindex,ucIDSOrdTempInvID FROM InvNum where ucIDSOrdTempInvID = @gid) I2 ON I1.iInvoiceID=I2.AutoIndex) D2 ---- @gid
ON D1.iStockCodeID=D2.iStockCodeID
WHERE _btblInvoiceLines.idInvoiceLines = D2.idInvoiceLines
Upvotes: 1