如何在 SQL 服务器的结果中删除 returns 'Null' 的行
How to remove the rows that returns 'Null' in the results in SQL Server
我有一个查询,可以根据需要提供结果。但我不希望在最终结果中显示 'Null' 的行。
SELECT DISTINCT In_Time,
Out_Time,
"Duration" = CASE
WHEN DATEDIFF (hh,In_Time, Out_Time) >= 2
THEN 'Duration was GREATER than 2 hours'
END
FROM
CASES C
显示的结果:
In_Time
Out_Time
Duration
2021-04-01 09:05:21
2021-04-01 09:28:46
NULL
2021-04-01 09:31:37
2021-04-01 10:33:27
NULL
2021-04-01 08:56:00
2021-04-01 11:46:11
duration was GREATER than 2 hours
2021-04-02 12:50:00
2021-04-02 14:52:26
duration was GREATER than 2 hours
2021-04-02 16:10:57
2021-04-02 17:05:21
NULL
如何删除 returns 为空的行并得到如下结果
预期结果:
In_Time
Out_Time
Duration
2021-04-01 08:56:00
2021-04-01 11:46:11
duration was GREATER than 2 hours
2021-04-02 12:50:00
2021-04-02 14:52:26
duration was GREATER than 2 hours
也许最简单的方法就是为此添加一个 WHERE
子句:
WHERE DATEDIFF (hh,In_Time, Out_Time) >= 2
而在原来的SQL中:
SELECT DISTINCT In_Time,
Out_Time,
CASE
WHEN DATEDIFF (hh,In_Time, Out_Time) >= 2 THEN 'Duration was GREATER than 2 hours'
END AS [Duration]
FROM CASES C
WHERE DATEDIFF (hh,In_Time, Out_Time) >= 2
我建议:
SELECT DISTINCT In_Time, Out_Time,
'Duration was GREATER than 2 hours' as Duration
FROM CASES C
WHERE DATEDIFF(hour, In_Time, Out_Time) >= 2;
不过,包括字符串 SELECT
似乎是多余的,因为所有值都是相同的。
另一种方法是将 DURATION
的定义放在 FROM
子句中,这样您就可以在多个地方使用该值:
SELECT DISTINCT c.In_Time, c.Out_Time, v.Duration
FROM CASES C CROSS APPLY
(VALUES (CASE WHEN DATEDIFF(hour, c.In_Time, c.Out_Time) >= 2
THEN 'Duration was GREATER than 2 hours'
END)
) v(Duration)
WHERE v.Duration IS NOT NULL;
感谢您的回答。
我在使用以下查询时得到了答案
SELECT DISTINCT In_Time,
Out_Time,
"Duration" =
CASE
WHEN DATEDIFF (hh,In_Time, Out_Time) >= 2 THEN 'Duration was GREATER than 2 hours'
END
FROM CASES C
WHERE DATEDIFF(hour, In_Time, Out_Time) >= 2
我有一个查询,可以根据需要提供结果。但我不希望在最终结果中显示 'Null' 的行。
SELECT DISTINCT In_Time,
Out_Time,
"Duration" = CASE
WHEN DATEDIFF (hh,In_Time, Out_Time) >= 2
THEN 'Duration was GREATER than 2 hours'
END
FROM
CASES C
显示的结果:
In_Time | Out_Time | Duration |
---|---|---|
2021-04-01 09:05:21 | 2021-04-01 09:28:46 | NULL |
2021-04-01 09:31:37 | 2021-04-01 10:33:27 | NULL |
2021-04-01 08:56:00 | 2021-04-01 11:46:11 | duration was GREATER than 2 hours |
2021-04-02 12:50:00 | 2021-04-02 14:52:26 | duration was GREATER than 2 hours |
2021-04-02 16:10:57 | 2021-04-02 17:05:21 | NULL |
如何删除 returns 为空的行并得到如下结果
预期结果:
In_Time | Out_Time | Duration |
---|---|---|
2021-04-01 08:56:00 | 2021-04-01 11:46:11 | duration was GREATER than 2 hours |
2021-04-02 12:50:00 | 2021-04-02 14:52:26 | duration was GREATER than 2 hours |
也许最简单的方法就是为此添加一个 WHERE
子句:
WHERE DATEDIFF (hh,In_Time, Out_Time) >= 2
而在原来的SQL中:
SELECT DISTINCT In_Time,
Out_Time,
CASE
WHEN DATEDIFF (hh,In_Time, Out_Time) >= 2 THEN 'Duration was GREATER than 2 hours'
END AS [Duration]
FROM CASES C
WHERE DATEDIFF (hh,In_Time, Out_Time) >= 2
我建议:
SELECT DISTINCT In_Time, Out_Time,
'Duration was GREATER than 2 hours' as Duration
FROM CASES C
WHERE DATEDIFF(hour, In_Time, Out_Time) >= 2;
不过,包括字符串 SELECT
似乎是多余的,因为所有值都是相同的。
另一种方法是将 DURATION
的定义放在 FROM
子句中,这样您就可以在多个地方使用该值:
SELECT DISTINCT c.In_Time, c.Out_Time, v.Duration
FROM CASES C CROSS APPLY
(VALUES (CASE WHEN DATEDIFF(hour, c.In_Time, c.Out_Time) >= 2
THEN 'Duration was GREATER than 2 hours'
END)
) v(Duration)
WHERE v.Duration IS NOT NULL;
感谢您的回答。
我在使用以下查询时得到了答案
SELECT DISTINCT In_Time,
Out_Time,
"Duration" =
CASE
WHEN DATEDIFF (hh,In_Time, Out_Time) >= 2 THEN 'Duration was GREATER than 2 hours'
END
FROM CASES C
WHERE DATEDIFF(hour, In_Time, Out_Time) >= 2