你能帮我解决这个Oracle SQL查询吗

n7taea2i  于 2022-10-04  发布在  Oracle
关注(0)|答案(2)|浏览(120)

返回姓名中字符最少和最多的员工(全名)。如果有很多,请只选择按字母顺序排在第一位的那些。因此,只有两名员工将被返还。你将投射出他们的名字。(全名)、其名称的大小以及其名称的ASCII值。

以下是我所做的:

SELECT FIRST_NAME ||' '|| LAST_NAME FULL_NAME ,
       (LENGTH(FIRST_NAME ||' '|| LAST_NAME)-1) Taille_caractere , 
       (ascii(FIRST_NAME ||' '|| LAST_NAME)-1)  Valeur_ASCII
  FROM EMPLOYEES;

但现在我想展示两个员工,一个不太有个性,一个更有个性

gywdnpxw

gywdnpxw1#

您可以将行row_number()两次,然后取出终端箱

select Taille_caractere, Valeur_ASCII
from (
    SELECT FIRST_NAME ||' '|| LAST_NAME FULL_NAME ,
           (LENGTH(FIRST_NAME ||' '|| LAST_NAME)-1) Taille_caractere , 
           (ascii(FIRST_NAME ||' '|| LAST_NAME)-1)  Valeur_ASCII,
      row_number() over(order by LENGTH(FIRST_NAME ||' '|| LAST_NAME), FIRST_NAME ||' '|| LAST_NAME) rn1,
      row_number() over(order by LENGTH(FIRST_NAME ||' '|| LAST_NAME) DESC, FIRST_NAME ||' '|| LAST_NAME) rn2,
      FROM EMPLOYEES
) t
where rn1 = 1 or rn2 = 1;
bihw5rsg

bihw5rsg2#

这是给你的:

WITH cteEmployees
  AS (SELECT FIRST_NAME,
             LAST_NAME,
             FIRST_NAME || ' ' || LAST_NAME AS FULL_NAME,
             LENGTH(FIRST_NAME || ' ' || LAST_NAME) AS FULL_NAME_LENGTH
        FROM EMPLOYEES),
  cteLeast AS
     (SELECT *
        FROM (SELECT *
                FROM cteEmployees
                ORDER BY FULL_NAME_LENGTH ASC,
                         LAST_NAME ASC,
                         FIRST_NAME ASC)
        WHERE ROWNUM = 1),
  cteMost AS
     (SELECT *
        FROM (SELECT *
                FROM cteEmployees
                ORDER BY FULL_NAME_LENGTH DESC,
                         LAST_NAME ASC,
                         FIRST_NAME ASC)
        WHERE ROWNUM = 1)
SELECT *
  FROM cteLeast
UNION ALL
SELECT *
  FROM cteMost

我把决定名字的ASCII值的问题留给你们,因为我不明白那是什么意思。

相关问题