现在我还在练习sql。并尝试解决 hackkerrank 上的挑战,但我遇到了一些疑问和问题
我接受的挑战:
“垫子”
链接:https://www.hackerrank.com/challenges/the-pads/problem?isFullScreen=true
此挑战包括根据职称计算人数,目标是将数字从小到大排序,然后按字母顺序对职业进行排序。
我尝试使用
ORDER BY
来解决这个问题,但它似乎并不影响输出上的排序数
查询:
(Select concat(name,'(',LEFT(Occupation,1),')') as name from occupations) union
(select concat('There are a total of ', count(*),' ', lower(occupation), 's.')
from occupations group by occupation order by count(*) asc, occupation asc)
输出:
Ashley(P)
Samantha(A)
Julia(D)
Britney(P)
Maria(P)
Meera(P)
Priya(D)
Priyanka(P)
Jennifer(A)
Ketty(A)
Belvet(P)
Naomi(P)
Jane(S)
Jenny(S)
Kristeen(S)
Christeen(S)
Eve(A)
Aamina(D)
There are a total of 7 professors.
There are a total of 4 actors.
There are a total of 3 doctors.
There are a total of 4 singers.
预期输出:
Aamina(D)
Ashley(P)
Belvet(P)
Britney(P)
Christeen(S)
Eve(A)
Jane(S)
Jennifer(A)
Jenny(S)
Julia(D)
Ketty(A)
Kristeen(S)
Maria(P)
Meera(P)
Naomi(P)
Priya(D)
Priyanka(P)
Samantha(A)
There are a total of 3 doctors.
There are a total of 4 actors.
There are a total of 4 singers.
There are a total of 7 professors.
链接游乐场w3school:https://www.w3schools.com/sql/trysqlserver.asp?filename=trysql_func_sqlserver_concat
我在 w3school 上尝试的查询:
SELECT concat(count(*), ' ',country)
from customers
group by country
ORDER BY count(*) asc
(Select concat(name,'(',LEFT(Occupation,1),')') as name from occupations ) union
( select concat("There are a total of ", count(Occupation)," ",lower(Occupation),"s.")
from Occupations group by Occupation order by count(Occupation) ) ORDER BY name
order by count(Occupation)
时,查询仍然有效,为什么?ORDER BY name
而不是使用`order by count(*)?我将不胜感激任何解释。
按要求订购 2 套联合
select case rowtype
when 1 then concat(name,'(',LEFT(Occupation,1),')' )
else concat ('There are a total of ',cnt,' ', lower(occupation), 's.') end name
from (
select 1 rowtype, name, 0 cnt, Occupation
from occupations
union
(
select 2, null, count(*), Occupation
from occupations
group by occupation
)
) t
order by rowtype, name, cnt, occupation
1 | 萨曼莎 | 医生 |
---|---|---|
2 | 朱莉娅 | 演员 |
3 | 玛丽亚 | 演员 |
4 | 米拉 | 歌手 |
5 | 阿什莉 | 教授 |
6 | 基蒂 | 教授 |
7 | 克里斯汀 | 教授 |
8 | 简 | 演员 |
9 | 珍妮 | 医生 |
10 | 普里亚 | 歌手 |
11 | 利亚姆 | 运动员 |
让我们拆分查询:
SELECT Name || "(" || SUBSTRING(Occupation, 1, 1) || ")" AS Jobs
FROM occupations_table
ORDER BY Name
工作 |
---|
阿什莉(P) |
克里斯汀(P) |
简(A) |
珍妮(D) |
朱莉娅(A) |
基蒂(P) |
利亚姆(S) |
玛丽亚(A) |
米拉(S) |
普里亚(S) |
萨曼莎(D) |
SELECT "There are a total of " || COUNT(*) || " " || Occupation AS Report
FROM occupations_table
GROUP BY Occupation
ORDER BY COUNT(*), Occupation
报告 |
---|
一共有1名运动员 |
一共有2位医生 |
共有2位歌手 |
共有3位演员 |
教授一共有3位 |
你不能用group by
UNION
它们,否则整个查询将会失败,但是你可以在ORDER BY
之后UNION
并且你应该得到预期的结果,或者至少我通过运行以下命令得到它sqlite 构建上的代码
SELECT Name || "(" || SUBSTRING(Occupation, 1, 1) || ")" AS Jobs
FROM occupations
UNION
SELECT "There are a total of " || COUNT(*) || " " || Occupation AS Quantity
FROM occupations
GROUP BY Occupation
ORDER BY Jobs, Quantity
工作 |
---|
阿什莉(P) |
克里斯汀(P) |
简(A) |
珍妮(D) |
朱莉娅(A) |
基蒂(P) |
利亚姆(S) |
玛丽亚(A) |
米拉(S) |
普里亚(S) |
萨曼莎(D) |
一共有1名运动员 |
一共有2位医生 |
共有2位歌手 |
共有3位演员 |
教授一共有3位 |
此解决方案有效:
SELECT Name + '('+ substring(Occupation, 1, 1) +')'
FROM OCCUPATIONS
ORDER BY Name ;
SELECT 'There are a total of ' + str(count(Occupation)) + ' '+ substring(LOWER(Occupation), 1, 1) + substring(Occupation, 2, LEN(Occupation)-1) + 's.'
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY count(Occupation)