Select 只查询 return 没有分组依据的最小日期时间?
Select query to return only the min datetime without group by?
我的table显示如下,
|_______________|____________|_____________________|______________________|
| ENTRY POINT | NAME | DATE OF ENTRY | DATETIME OF ENTRY |
|_______________|____________|_____________________|______________________|
| CHECKIN | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:00 |
|_______________|____________|_____________________|______________________|
| CHECKIN 1 | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:10 |
|_______________|____________|_____________________|______________________|
| CHECKIN 2 | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:15 |
|_______________|____________|_____________________|______________________|
| CHECKIN 10 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:07:10 |
|_______________|____________|_____________________|______________________|
| CHECKIN 4 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:07:10 |
|_______________|____________|_____________________|______________________|
| CHECKIN 5 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:09:15 |
|_______________|____________|_____________________|______________________|
预期输出只是第一个值,它具有如下每天的最小输入日期时间,
|_______________|____________|_____________________|______________________|
| ENTRY POINT | NAME | DATE OF ENTRY | DATETIME OF ENTRY |
|_______________|____________|_____________________|______________________|
| CHECKIN | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:00 |
|_______________|____________|_____________________|______________________|
| CHECKIN 10 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:07:10 |
|_______________|____________|_____________________|______________________|
如果我尝试使用最短的进入日期时间,它可能会让我使用 group by
'ENTRY POINT' 和其他列,这些列基本上给我的输出与我的输入相同。
请帮我解决这个问题。
您可以使用window函数row_number()
。这是 demo.
select
ENTRY_POINT,
NAME,
DATE_OF_ENTRY,
DATETIME_OF_ENTRY
from
(
select
*,
row_number() over (order by DATETIME_OF_ENTRY) as rnk
from myTable
) t
where rnk = 1
您需要 window 个分析函数,其中 DENSE_RANK()
很适合包含所有关系(每个 [=13] 具有相同 [DATETIME OF ENTRY]
值的记录=] 和 [DATE OF ENTRY]
组合 )。
需要找出按每个 [NAME]
和 [DATE OF ENTRY]
分组(分区)的升序 [DATETIME OF ENTRY]
值的第一个记录:
SELECT [ENTRY POINT], [NAME], [DATE OF ENTRY], [DATETIME OF ENTRY]
FROM
(
SELECT T.*,
DENSE_RANK() OVER
(PARTITION BY [NAME], [DATE OF ENTRY] ORDER BY [DATETIME OF ENTRY]) AS dr
FROM T -- <your table>
) TT
WHERE dr = 1;
我的table显示如下,
|_______________|____________|_____________________|______________________|
| ENTRY POINT | NAME | DATE OF ENTRY | DATETIME OF ENTRY |
|_______________|____________|_____________________|______________________|
| CHECKIN | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:00 |
|_______________|____________|_____________________|______________________|
| CHECKIN 1 | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:10 |
|_______________|____________|_____________________|______________________|
| CHECKIN 2 | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:15 |
|_______________|____________|_____________________|______________________|
| CHECKIN 10 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:07:10 |
|_______________|____________|_____________________|______________________|
| CHECKIN 4 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:07:10 |
|_______________|____________|_____________________|______________________|
| CHECKIN 5 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:09:15 |
|_______________|____________|_____________________|______________________|
预期输出只是第一个值,它具有如下每天的最小输入日期时间,
|_______________|____________|_____________________|______________________|
| ENTRY POINT | NAME | DATE OF ENTRY | DATETIME OF ENTRY |
|_______________|____________|_____________________|______________________|
| CHECKIN | CHOCOTACO | 05/07/2018 | 05/07/2018 00:01:00 |
|_______________|____________|_____________________|______________________|
| CHECKIN 10 | CHOCOTACO | 06/07/2018 | 06/07/2018 00:07:10 |
|_______________|____________|_____________________|______________________|
如果我尝试使用最短的进入日期时间,它可能会让我使用 group by
'ENTRY POINT' 和其他列,这些列基本上给我的输出与我的输入相同。
请帮我解决这个问题。
您可以使用window函数row_number()
。这是 demo.
select
ENTRY_POINT,
NAME,
DATE_OF_ENTRY,
DATETIME_OF_ENTRY
from
(
select
*,
row_number() over (order by DATETIME_OF_ENTRY) as rnk
from myTable
) t
where rnk = 1
您需要 window 个分析函数,其中 DENSE_RANK()
很适合包含所有关系(每个 [=13] 具有相同 [DATETIME OF ENTRY]
值的记录=] 和 [DATE OF ENTRY]
组合 )。
需要找出按每个 [NAME]
和 [DATE OF ENTRY]
分组(分区)的升序 [DATETIME OF ENTRY]
值的第一个记录:
SELECT [ENTRY POINT], [NAME], [DATE OF ENTRY], [DATETIME OF ENTRY]
FROM
(
SELECT T.*,
DENSE_RANK() OVER
(PARTITION BY [NAME], [DATE OF ENTRY] ORDER BY [DATETIME OF ENTRY]) AS dr
FROM T -- <your table>
) TT
WHERE dr = 1;