SQL Query to find missing rows between two related tables

Job ID: 37012829

Budget: $10 – $30 USD

I'm looking for a query to find missing linenumbers. Table one is our sales order details table, table 2 are the inspection pictures taken of the sales order line numbers.

GNC_QRP_ShippingPictures has multiple rows (1 for each picture taken) so #TempTable just gets one of each

so_details has multiple linnum also, I only need one of them each linnum has at lets one delnum

CREATE TABLE #TempTable (
DateOnly DATE,
OrderNumber varchar(8),
LineNumber varchar(2),
PictureTitle VARCHAR(255),
LineCabName VARCHAR(255),
RowRank INT
);

INSERT INTO #TempTable (DateOnly, OrderNumber, LineNumber, PictureTitle, LineCabName, RowRank)
SELECT
CONVERT(DATE, pic.BoxedDate) AS DateOnly,
pic.OrderNumber,
pic.LineNumber,
pic.PictureTitle,
pic.LineCabName,
ROW_NUMBER() OVER (PARTITION BY CONVERT(DATE, pic.BoxedDate), pic.OrderNumber, pic.LineNumber ORDER BY CONVERT(DATE, pic.BoxedDate), pic.OrderNumber, pic.LineNumber) AS RowRank
FROM GNC_QRP_ShippingPictures pic
WHERE CONVERT(DATE, pic.BoxedDate) = '2023-08-04';



---------------------------------------------------------------------------------
SELECT sd.*
FROM so_detail sd
LEFT JOIN #TempTable sp ON sd.ORDNUM_28 = sp.OrderNumber AND sd.LINNUM_28 = sp.LineNumber
WHERE sp.OrderNumber IS NULL AND sp.LineNumber IS NULL

and sd.DELNUM_28 = '01'
;

drop table #tempTable

_____________________________________________________

Gives me EVERYTHING in sd, I only want ordnum, linnum's from sd that match the ordernumber, linenumbers in GNC_QRP_ShippingPictures