Search code examples
sqlsql-servervariablesiifcross-join

Setting multiple variables' value in one IIF() of a SELECT statement


I have a CROSS JOIN query that I am using to see which combination of item quantities yield the best output.

DECLARE @last_found DECIMAL(10, 2) = 0
DECLARE @calculated DECIMAL(10, 2)
DECLARE @n_count INT
DECLARE @tbl1n INT
DECLARE @tbl2n INT
DECLARE @tbl3n INT


DROP TABLE IF EXISTS #tbl1
DROP TABLE IF EXISTS #tbl2
DROP TABLE IF EXISTS #tbl3

;WITH numbers AS (
    SELECT ROW_NUMBER() OVER (ORDER BY [value]) AS n
    FROM string_split('1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20', ',')
)
SELECT n, (n * 10000 * (1 + IIF(n > 1, (0.50/19.00) * (n - 1), 0))) AS price
INTO #tbl1 FROM numbers

;WITH numbers AS (
    SELECT ROW_NUMBER() OVER (ORDER BY [value]) AS n
    FROM string_split('1,2,3,4,5,6,7,8,9,10,11,12', ',')
)
SELECT n, (n * 15000 * (1 + IIF(n > 1, (0.50/11.00) * (n - 1), 0))) AS price
INTO #tbl2 FROM numbers

;WITH numbers AS (
    SELECT ROW_NUMBER() OVER (ORDER BY [value]) AS n
    FROM string_split('1,2,3,4,5,6', ',')
)
SELECT n, (n * 20000 * (1 + IIF(n > 1, (0.50/5.00) * (n - 1), 0))) AS price
INTO #tbl3 FROM numbers


SELECT
    @n_count = (tbl1.n + tbl2.n + tbl3.n),
    @calculated = IIF(@n_count = 10, (tbl1.price + tbl2.price + tbl3.price), 0),
    @tbl1n = IIF(@calculated > @last_found, tbl1.n, @tbl1n),
    @tbl2n = IIF(@calculated > @last_found, tbl2.n, @tbl2n),
    @tbl3n = IIF(@calculated > @last_found, tbl3.n, @tbl3n),
    @last_found = IIF(@calculated > @last_found, @calculated, @last_found)
FROM #tbl1 tbl1
CROSS JOIN #tbl2 tbl2
CROSS JOIN #tbl3 tbl3


SELECT @last_found AS highest_value, @tbl1n AS tbl1n, @tbl2n AS tbl2n, @tbl3n AS tbl3n,
    t1.price AS tbl1_price, t2.price AS tbl2_price, t3.price AS tbl3_price
FROM #tbl1 t1
INNER JOIN #tbl2 t2 ON t1.n = @tbl1n AND t2.n = @tbl2n
INNER JOIN #tbl3 t3 ON t3.n = @tbl3n

As can be seen, if the query finds a value higher than the previously found highest, it is storing the combination using multiple instances of @itemN = IIF(@calculated > @last_found, tbl.n, @itemN).

Is it possible to assign all @tblXn variables in one go? I could use a CONCAT, but I think it may slow down the query, as it is a string operation.

FYI - 'n' is a value between 0 and 20.


Solution

  • You shouldn't be using variables for this at all.

    This would be much simpler written as below (without relying on undocumented/unguaranteed behaviour of assignment to variables across multiple rows)

    SELECT TOP 1 CAST(combined_price AS DECIMAL(10, 2)) AS highest_value,
                 tbl1.n                                 AS tbl1n,
                 tbl2.n                                 AS tbl2n,
                 tbl3.n                                 AS tbl3n,
                 tbl1.price                             AS tbl1_price,
                 tbl2.price                             AS tbl2_price,
                 tbl3.price                             AS tbl3_price
    FROM   #tbl1 tbl1
           CROSS JOIN #tbl2 tbl2
           CROSS JOIN #tbl3 tbl3
           CROSS APPLY (VALUES (tbl1.price + tbl2.price + tbl3.price, 
                                tbl1.n + tbl2.n + tbl3.n)) CA(combined_price, combined_n)
    WHERE  combined_n = 10
    ORDER  BY combined_price DESC