生成以下两个结果集:
1)。查询OCCUPATIONS中所有姓名的按字母顺序排列的列表,紧接着是每个职业的第一个字母作为括号(即:括在括号中)。例如:AnActorName(A),ADoctorName(D),AProfessorName(P)和ASingerName(S)。
2)。查询OCCUPATIONS中每个职业的发生次数。按升序对事件进行排序,并按以下格式输出:
There are total [occupation_count] [occupation]s.
表名:职业
总列数:两个='名称'和'职业',演示表如下所示:
示例输出:
Ashely(P)
Christeen(P)
Jane(A)
Jenny(D)
Julia(A)
Ketty(P)
Maria(A)
Meera(S)
Priya(S)
Samantha(D)
There are total 2 doctors.
There are total 2 singers.
There are total 3 actors.
There are total 3 professors.
我的方法:
(SELECT NAME, '(', SUBSTRING(OCCUPATION, 1, 1), ')'
FROM OCCUPATIONS ORDER BY NAME)
UNION ALL
(SELECT COUNT(*) FROM OCCUPATIONS GROUP BY OCCUPATION ORDER BY ASEC);
错误:
ERROR 1222 (21000) at line 1:
The used SELECT statements have a different number of columns
谢谢!
答案 0 :(得分:3)
您忘记使用CONCAT
功能粘贴所选数据
尝试这样的事情(也见sqlfiddle上的这个例子):
(
SELECT CONCAT(NAME, '(', SUBSTRING(OCCUPATION, 1, 1), ')') as THETEXT, '1' as SELECTNUMBER
FROM OCCUPATIONS
)
UNION ALL
(
SELECT CONCAT('There are total ', COUNT(*),' ', OCCUPATION, (IF (COUNT(*) > 1, 's',''))) as THETEXT, '2' as SELECTNUMBER
FROM OCCUPATIONS GROUP BY OCCUPATION
)
ORDER BY SELECTNUMBER ASC, THETEXT ASC;
答案 1 :(得分:2)
这将起作用!
SELECT CONCAT(Name,
CONCAT('(', LEFT(Occupation,1),')'))
FROM OCCUPATIONS
ORDER BY Name ASC;
SELECT
CONCAT("There are a total of",
" ",
COUNT(occupation),
" ",
LCASE(occupation),
"s",
".")
AS stat
FROM OCCUPATIONS
GROUP By occupation
ORDER BY COUNT(occupation) ASC, occupation;
答案 2 :(得分:1)
SELECT concat(NAME,concat("(",LEFT(occupation,1),")"))
FROM OCCUPATIONS
ORDER BY NAME ASC;
select CONCAT("There are a total of", " ",COUNT(occupation), " ",LCASE(occupation),"s",".")AS stat
from OCCUPATIONS
group by occupation
order by COUNT(occupation) ASC,occupation
答案 3 :(得分:1)
致力于hackerrank
SELECT Name || '(' || SUBSTR(Occupation,1,1) || ')'-- AS THETEXT, '1' AS SELECTNUMBER
FROM OCCUPATIONS
order by Name;
SELECT 'There are a total of' || ' ' || COUNT(*) || ' ' || lower(Occupation) || 's.'
FROM OCCUPATIONS GROUP BY OCCUPATION
ORDER BY COUNT(Occupation), lower(Occupation);
答案 4 :(得分:0)
SELECT CONCAT(NAME ,'('||SUBSTR(occupation,1,1)||')')
FROM occupations
ORDER BY NAME ASC;
SELECT CONCAT('There are a total of ',COUNT (occupation)||' '||LOWER(occupation)||'s.')
FROM occupations
GROUP BY occupation
ORDER BY COUNT(occupation),occupation ASC;
答案 5 :(得分:0)
对于 Oracle SQL 特别试试这个:
SELECT NAME || '(' || SUBSTR(OCCUPATION, 1, 1) || ')' FROM OCCUPATIONS ORDER BY NAME;
SELECT 'There are a total of ' || count(OCCUPATION) || ' ' || lower(OCCUPATION) || 's.' FROM OCCUPATIONS GROUP BY OCCUPATION ORDER BY count(OCCUPATION), OCCUPATION ASC;
答案 6 :(得分:0)
试试这个代码..
-- for Query 01
SELECT CONCAT(Name, '(', LEFT(Occupation,1),')')
FROM OCCUPATIONS
ORDER BY Name;
-- for Query 02
SELECT CONCAT('There are a total of ', COUNT(Occupation), ' ', LOWER(Occupation), 's.')
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY COUNT(Occupation), Occupation;
答案 7 :(得分:0)
请按照此代码进行 Mysql。这对我最有效。我已经尝试并得到了正确的结果。
select concat(name,'(',left(occupation,1),')') from occupations
order by name;
select concat('There are a total of ',count,' ',lower(occupation),'s.') from (select occupation, count(*) as count from occupations group by occupation)a
order by count, occupation;
谢谢
答案 8 :(得分:0)
select concat(Name,'(',substring(Occupation,1,1),')') from OCCUPATIONS order by Name;
select
concat('There are a total of ',count(*),' ',LCASE(Occupation),
If(count(*)>1,'s.',''))
from OCCUPATIONS group by Occupation order by count(*) ASC
;
答案 9 :(得分:0)
一个人可以利用MySQL中的UNION运算符来合并问题的每个部分。
注意::要将列名组合为一列,可以使用CONCAT()
函数。
SELECT DISTINCT CONCAT(name, '(', left(occupation, 1), ')') AS n
from OCCUPATIONS
union
select concat('There are a total of ', (select count(*) from OCCUPATIONS group by occupation having occupation = 'doctor'), ' doctors.')
union
select concat('There are a total of ', (select count(*) from OCCUPATIONS group by occupation having occupation = 'singer'), ' singers.')
union
select concat('There are a total of ', (select count(*) from OCCUPATIONS group by occupation having occupation = 'actor'), ' actors.')
union
select concat('There are a total of ', (select count(*) from OCCUPATIONS group by occupation having occupation = 'professor'), ' professors.')
order by n asc;
答案 10 :(得分:0)
select Name+'('+substring(Occupation,1,1)+')' from occupations order by name asc;
select concat('There are a total of',' ', count(occupation),' ',Lower(occupation),'s','.') from Occupations group by Occupation order by count(occupation), occupation asc
答案 11 :(得分:0)
/* sort answer is here */
( select concat(name,'(',left(occupation,1),')') as ocps from
Occupations) union
(select distinct concat('There are a total of
',count(occupation),' ',lower(occupation),'s.') from Occupations
group by occupation)order by ocps;
答案 12 :(得分:0)
检查以下代码。希望这有助于其100%正常工作
select concat(Name,"(",left(Occupation,1),")")
from occupations
order by Name;
select concat("There are a total of ",count(Occupation),' ',lower(occupation),"s.")
from Occupations
group by occupation
order by count(Occupation)asc, Occupation;
答案 13 :(得分:0)
select concat(name, '(',substring(occupation,1,1),')') from OCCUPATIONS order by name;
select concat('There are a total of ',count(occupation),' ',lower(occupation),'s.') as 'sentence' from OCCUPATIONS group by occupation order by count(occupation), occupation,field(occupation,'doctor','singer','actor','professor');
答案 14 :(得分:0)
select CONCAT(NAME, '(', left(OCCUPATION,1),')') from OCCUPATIONS ORDER BY NAME;
select CONCAT("There are a total of ", count(OCCUPATION), " ", lower(OCCUPATION),(if(count(OCCUPATION) >1,"s","")), ".") from OCCUPATIONS group by occupation order by count(OCCUPATION), OCCUPATION;
答案 15 :(得分:0)
SELECT CONCAT(NAME,'(',LEFT(OCCUPATION,1),')')
FROM OCCUPATIONS ORDER BY NAME;
SELECT CONCAT("There are total of ",COUNT_OCCUPATION," ",OCCUPATION,".")
FROM
(SELECT COUNT(OCCUPATION) AS COUNT_OCCUPATION,OCCUPATION
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT_OCCUPATION ASC,OCCUPATION ASC) AS OCP
答案 16 :(得分:0)
这对我有用:
select CONCAT(name,'(',left(occupation,1),')')
from OCCUPATIONS
order by name;
select CONCAT('There are a total of',' ',count(occupation),' ',lower(occupation),'s.')
from OCCUPATIONS
group by occupation
order by count(occupation), occupation;
答案 17 :(得分:0)
SELECT * FROM (
select distinct Name + '('+left(Occupation,1)+')'as a
from OCCUPATIONS
union
select ('There are total ' +cast( count( Occupation) as nvarchar) + ' '+ lower(Occupation) +'s.') as b
from OCCUPATIONS
GROUP BY OCCUPATION
) AS N
ORDER BY a
这适用于MS SQL Server。
答案 18 :(得分:0)
union要求两个查询具有相同的列数,您可以在第二个查询中添加任何一些列,使它们像第一个像SELECT COUNT(*),' col-1', ' col-2',' col-3'从
答案 19 :(得分:0)
SELECT CONCAT(name, '(', SUBSTRING(occupation, 1, 1), ')') as occ from occupations ORDER BY name;
SELECT concat('There are a total of ', COUNT(*), ' ', LCASE(occupation), 's.') from occupations GROUP BY occupation ORDER BY COUNT(*), occupation;
这很有魅力。
答案 20 :(得分:0)
(Select name||'('||substr(Occupation,0,1)||')'
from OCCUPATIONS)
Union All
(Select 'There are a total of '||count(*)||' '||lower(Occupation)||'s.'
from OCCUPATIONS group by Occupation)
order by 1;
答案 21 :(得分:0)
我刚试过hackerrank并且它有效,你不需要使用Union。
select concat(name,'(',upper(substring(occupation,1,1)),')') from occupations
order by name;
select concat("There are a total of",' ',count(occupation),' ',lower(occupation),'s',".") from occupations
group by occupation
order by count(occupation) asc;
答案 22 :(得分:0)
select concat(name,'(',left(occupation,1),')') from occupations order by name;
select concat('There are total of',' ',count(occupation),' ',lower(occupation),"s.") from occupations group by occupation order by count(occupation),occupation;
答案 23 :(得分:0)
(
SELECT CONCAT(NAME, '(', SUBSTRING(OCCUPATION, 1, 1), ')') as THETEXT
FROM OCCUPATIONS
)
UNION ALL
(
SELECT CONCAT('There are total ', COUNT(*),' ', LOWER(OCCUPATION) ,(IF (COUNT(*) > 1, 's','')), '.') as THETEXT
FROM OCCUPATIONS group by OCCUPATION
)
ORDER by THETEXT ASC;
答案 24 :(得分:0)
//Just be careful for the format!
(select concat(name, '(',left(occupation,1),')') as text
from occupations
)
union all
(
select concat(
'There are total ',
count(*), ' ', lower(occupation),
case
when COUNT(*) > 1
then 's'
end
,
'.'
) as text
from occupations
group by occupation
)
order by text asc;
答案 25 :(得分:-1)
GetParent()
答案 26 :(得分:-1)
select (name||'('||UPPER(substr(occupation,1,1))||')') from occupations order by name;
select ('There are a total of '||count(name)||' '||lower(occupation)||'s.') from occupations group by occupation order by count(name), occupation;
答案 27 :(得分:-1)
select Name + '('+ substring(OCCUPATION, 1,1) +')' from OCCUPATIONS
union
select 'There are total ' + cast(count(OCCUPATION) as varchar) +' '+
lower(OCCUPATION)+'s.' from OCCUPATIONS
group by OCCUPATION
答案 28 :(得分:-2)
value before = 23
value in while loop = 23
答案 29 :(得分:-2)
SELECT * FROM (
select distinct Name + '('+left(Occupation,1)+')'as a
from OCCUPATIONS
union
select ('There are total ' +cast( count( Occupation) as nvarchar) + ' '+ lower(Occupation) +'s.') as b
from OCCUPATIONS
GROUP BY OCCUPATION
) AS N
ORDER BY a
答案 30 :(得分:-2)
SELECT CONCAT (NAME, CONCAT ('(',LEFT(OCCUPATION,1),')'))FROM OCCUPATIONS ORDER BY NAME ASC;
SELECT CONCAT ('There are a total of ',count(OCCUPATION),' doctors.' )FROM OCCUPATIONS WHERE OCCUPATION='Doctor';
SELECT CONCAT ('There are a total of ',count(OCCUPATION),' singers.' )FROM OCCUPATIONS WHERE OCCUPATION='Singer';
SELECT CONCAT ('There are a total of ',count(OCCUPATION),' actors.' )FROM OCCUPATIONS WHERE OCCUPATION='Actor';
SELECT CONCAT ('There are a total of ',count(OCCUPATION),' professors.' )FROM OCCUPATIONS WHERE OCCUPATION='Professor';