Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This should work.</p> <pre><code>DECLARE @ids TABLE(n int); INSERT INTO @ids (n) VALUES (1000); INSERT INTO @ids (n) VALUES (2000); INSERT INTO @ids (n) VALUES (3000); SELECT il.* INTO #InvoiceLineQuery FROM [InvoiceLine] il INNER JOIN @ids i ON il.invoiceId = i.n SELECT letter.text id, CASE WHEN pos.maxPos = pos.minPos THEN pos.maxPos ELSE NULL END AS position, letter.text, CASE WHEN price.maxPrice = price.minPrice THEN price.maxPrice ELSE NULL END AS price, 0 AS isRecordDeleted, CASE WHEN moveUP.text IS NOT NULL THEN 'yes' ELSE 'no' END AS moveUP, CASE WHEN moveDOWN.text IS NOT NULL THEN 'yes' ELSE 'no' END AS moveDown FROM (SELECT il.text FROM #InvoiceLineQuery il GROUP BY il.text HAVING count(il.position) = (SELECT count(1) FROM @ids)) letter INNER JOIN (SELECT il.text, MAX(il.position) maxPos, MIN(il.position) minPos FROM #InvoiceLineQuery il GROUP BY il.text) pos on letter.text = pos.text INNER JOIN (SELECT il.text, MAX(il.price) maxPrice, MIN(il.price) minPrice FROM #InvoiceLineQuery il GROUP BY il.text) price on letter.text = price.text LEFT JOIN (SELECT upNeighbour.text FROM (SELECT mainLine.text text, upLine.text upText FROM #InvoiceLineQuery mainLine INNER JOIN #InvoiceLineQuery upLine ON mainLine.position = upLine.Position + 1 AND mainLine.invoiceId = upLine.invoiceId) upNeighbour GROUP BY upNeighbour.text,upNeighbour.upText HAVING count(upNeighbour.upText) = (SELECT count(1) FROM @ids)) moveUP ON letter.text = moveUP.text LEFT JOIN (SELECT downNeighbour.text FROM (SELECT mainLine.text text, downLine.text downText FROM #InvoiceLineQuery mainLine INNER JOIN #InvoiceLineQuery downLine ON mainLine.position + 1 = downLine.Position AND mainLine.invoiceId = downLine.invoiceId) downNeighbour GROUP BY downNeighbour.text,downNeighbour.downText HAVING count(downNeighbour.downText) = (SELECT count(1) FROM @ids)) moveDOWN ON letter.text = moveDOWN.text DROP TABLE #InvoiceLineQuery </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload