SQL查询模式选择

时间:2019-02-08 12:28:58

标签: sql sql-server

表部分

enter image description here

我需要从特定日期的Assoc.Ref列的前7个字符相同的行中选择所有内容。

结果示例

enter image description here

1 个答案:

答案 0 :(得分:5)

您需要聚合:

app.get('/page/', (req, res) => {
    res.writeHead(200, { /* . . . */ });
    res.write('\n');
    req.on("close", () => {
        // do something when the connection is closed
    });
});

如果您希望每行恰好有两行,请改用SELECT t.col FROM table t GROUP BY t.col HAVING COUNT(*) > 1;

如果需要所有行,则可以使用Windows函数:

COUNT(*) = 2

编辑:对问题进行更新后,您可能需要SELECT t.* FROM (SELECT t.*, COUNT(*) OVER(PARTITION BY col) AS cnt FROM table t ) t WHERE t.cnt > 1;

LEFT()

如果SELECT t.* FROM (SELECT t.*, COUNT(*) OVER(PARTITION BY CAST(Date_created AS date), LEFT(associated_ref, 7)) AS cnt FROM table t ) t WHERE t.cnt > 1 AND CAST(t.Date_created AS date) = '2019-02-08'; 没有Date_created,则不需要对话。只需使用time