Oracle SQL 打印 table 中的第一个和最后一个条目?
Oracle SQL print first and last entry in a table?
所以我需要在 table 中打印最老和最年轻的结果,我的代码是
SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client
order by (client.clientdob) DESC
FETCH first 1 row only
SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client
order by (client.clientdob) ASC
FETCH first 1 ROW ONLY;
这 2 个单独 return 是正确的值,但我不能同时将它们都设置为 运行,看来我不能在这个设置中使用 Union,如果可能的话,单个 select 我可以获得第一个和最后一个的语句会更好。我也在使用 SQL 开发人员,如果有办法让这两个都显示在同一个 SQL 查询结果 window 中,那很好。
你可以试试下面的-
SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client where
client.clientdob=(select max(client.clientdob) from client)
or
client.clientdob=(select min(client.clientdob) from client)
您可以像下面这样使用 UNION ALL
:
select c1.* from (select client.clientfirstname, client.clientlastname, client.clientdob, row_number() over (order by client.clientdob ASC) as rn FROM client) c1 where c1.rn =1
union all
select c2.* from (select client.clientfirstname, client.clientlastname, client.clientdob, row_number() over (order by client.clientdob DESC) as rn FROM client) c2 where c2.rn =1
我们可以使用解析函数row_number()
得到想要的结果,
select *
from
(
select cl.clientfirstname
, cl.clientlastname
, cl.clientdob
, row_number() over (order by cl.clientdob desc) max_rn
, row_number() over (order by cl.clientdob asc) min_rn
FROM client cl
) t
where (max_rn = 1 or min_rn = 1)
所以我需要在 table 中打印最老和最年轻的结果,我的代码是
SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client
order by (client.clientdob) DESC
FETCH first 1 row only
SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client
order by (client.clientdob) ASC
FETCH first 1 ROW ONLY;
这 2 个单独 return 是正确的值,但我不能同时将它们都设置为 运行,看来我不能在这个设置中使用 Union,如果可能的话,单个 select 我可以获得第一个和最后一个的语句会更好。我也在使用 SQL 开发人员,如果有办法让这两个都显示在同一个 SQL 查询结果 window 中,那很好。
你可以试试下面的-
SELECT client.clientfirstname, client.clientlastname, client.clientdob
FROM client where
client.clientdob=(select max(client.clientdob) from client)
or
client.clientdob=(select min(client.clientdob) from client)
您可以像下面这样使用 UNION ALL
:
select c1.* from (select client.clientfirstname, client.clientlastname, client.clientdob, row_number() over (order by client.clientdob ASC) as rn FROM client) c1 where c1.rn =1
union all
select c2.* from (select client.clientfirstname, client.clientlastname, client.clientdob, row_number() over (order by client.clientdob DESC) as rn FROM client) c2 where c2.rn =1
我们可以使用解析函数row_number()
得到想要的结果,
select *
from
(
select cl.clientfirstname
, cl.clientlastname
, cl.clientdob
, row_number() over (order by cl.clientdob desc) max_rn
, row_number() over (order by cl.clientdob asc) min_rn
FROM client cl
) t
where (max_rn = 1 or min_rn = 1)