如何获取3条记录之间的中位数? -SQL

时间:2018-09-20 13:24:25

标签: sql median

请协助我,我需要选择790,因为它在中间,但无法弄清楚。

proxy.on('proxyReq', function(proxyReq, req, res, options) {
    var dataReq = new Buffer('');
    req.on('data', function (data) {
        // incomming post body
        dataReq = Buffer.concat([dataReq, data]);
    });

    req.on('end', function () {
        try {
            // make changes
            req.query = JSON.parse(dataReq.toString());
            req.query.size = 1;

        } catch (e) {
            console.error(e.message);
        }
    }).on('error', (e) => {
        console.error(`Got error: ${e.message}`);
    });
    // send new body
    // req.query is undefined, because event 'data', 'end' did not fire yet
    proxyReq.write(JSON.stringify(req.query))
});

提前谢谢

1 个答案:

答案 0 :(得分:0)

这应该有助于获得您想要的东西。从表中获取平均值,然后获得与平均值差异最小的第一条记录。

SELECT TOP 1 id, value FROM yourTable
ORDER BY ABS(value - (SELECT AVG(value) FROM yourTable)) 

或使用变量,具体取决于表的大小

DECLARE @median DECIMAL
SELECT @median = AVG(value) FROM yourTable

SELECT TOP 1 * FROM yourTable 
ORDER BY ABS( value - @median)