如果SQL Server中的上一个单元格值和当前单元格值相同,请选择为空

时间:2018-02-06 05:20:10

标签: sql sql-server stored-procedures

我有一种情况是根据SQL Server存储过程中的用户名从表中删除重复值。我的表格如下图所示:

Table looks like this

我期待输出如下:

enter image description here

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

使用支持 SQL 2012 + LAG功能,如下所示,我在表格中使用ID作为主键

async function test() {
const data= [];
for (let i = 0; i <= 5; i = i + 1) {
    const payload: any = await postExampleApi(i);
        data.push(payload);
    }
    return data;
}

export const getData =  test().then((data) => {
    console.log(data);
    return data;
});

<强>输出:

enter image description here

答案 1 :(得分:0)

您通常会在表示层中处理此问题,但您可以尝试以下查询:

SELECT
    Date,
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY User ORDER BY Date) = 1
         THEN User ELSE '' END AS User,
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY User ORDER BY Date) = 1
         THEN [Source IP] ELSE '' END AS [Source IP],
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY User ORDER BY Date) = 1
         THEN Destination ELSE '' END AS Destination,
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY User ORDER BY Date) = 1
         THEN Port ELSE '' END AS Port,
    CASE WHEN ROW_NUMBER() OVER (PARTITION BY User ORDER BY Date) = 1
         THEN Duration ELSE '' END AS Duration
FROM yourTable;

注意:从您的示例输出中不清楚您是否期望所有记录都Source IP。如果没有,那么只需修改我的查询。

如果您的SQL Server版本不支持LAG,那么这可能是一个可行的选择。

答案 2 :(得分:0)

ID  NAME    DTE
1   RAJIV   2018-02-06 11:04:13.257
2           2018-02-06 11:04:13.257
3   RAVI    2018-02-06 11:04:13.257
4           2018-02-06 11:04:13.257
5   AJAY    2018-02-06 11:04:13.257

选择声明:

localization = navigator.language;

setInterval(() => {
    if(this.localization !== navigator.language) {
        // call your get method
    }
},1000);

输出:

{{1}}