我有一个CROSS JOIN查询,用于查看哪种物料数量组合产生最佳输出。
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
可以看出,如果查询找到的值大于先前找到的最高值,则它将使用多个@itemN = IIF(@calculated > @last_found, tbl.n, @itemN)
实例存储组合。
是否可以一次性分配所有@tblXn变量?我可以使用CONCAT,但是我认为它可能会减慢查询速度,因为它是字符串操作。
仅供参考-'n'是介于0和20之间的值。
答案 0 :(得分:1)
您可以使用const puppeteer = require('puppeteer');
const express = require('express');
const fs = require('fs');
var port = process.env.PORT || 80;
const app = express();
global.browser = null;
const setup = async function(){
console.log("Initializing Puppeteer...")
browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage=true',
'--disable-accelerated-2d-canvas=true',
'--disable-gpu',
]
});
browser.on('disconnected', setup);
}
app.get('/', (req, res) => {
setup();
console.log(`Started Puppeteer with pid ${browser.process().pid}`);
res.status(200).send('Hello, world!').end();
});
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
setup();
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// [END gae_node_request_example]
module.exports = app;
:
apply
注意:您可以进一步将值分配给变量。
答案 1 :(得分:0)
您根本不应该为此使用变量。
这将更简单地编写如下(无需依赖跨行分配变量的无证/毫无保证的行为)
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