HackerRank笔记 - SQL Server
生活随笔
收集整理的這篇文章主要介紹了
HackerRank笔记 - SQL Server
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- Easy
- 1. 返回ID為偶數的行 - `(id % 2) = 0`
- 2. 返回(向上/向下)取整/四舍五入結果
- 1) `ROUND(num,len)`
- 2) `CAST(num AS DECIMAL(38,len))`
- 3) 向上取整`CEILING()`/ 向下取整`FLOOR()`
- 4) 整理四舍五入/(向上/向下)取整相關方法 ??
- 3. 畫三角形
- 循環輸出 `WHILE(con) BEGIN ... END`
- `varchar` vs `char`
- Medium
- 1. 在兩個數之間 LEFT JOIN [table] ON A.col `BETWEEN`B.col1 `AND`B.col2
- 2. 首字母`LEFT(string,num)` / 大小寫`UPPER()` `LOWER()`
- 3. `PIVOT`
- 4. 數學函數
Easy
1. 返回ID為偶數的行 - (id % 2) = 0
select city from station where (id % 2) = 0Weather Observation Station 3
2. 返回(向上/向下)取整/四舍五入結果
1) ROUND(num,len)
select COUNTRY.Continent,round(avg(CITY.Population),0) from CITY left join COUNTRY on CITY.CountryCode = COUNTRY.Code where COUNTRY.Continent is not null group by COUNTRY.ContinentAverage Population of Each Continent
2) CAST(num AS DECIMAL(38,len))
select cast(sum(LAT_N) as decimal(38,2)),cast(sum(LONG_W) as decimal(38,2)) from STATIONWeather Observation Station 2
3) 向上取整CEILING()/ 向下取整FLOOR()
select cast(CEILING(avg(convert(float,salary)) - avg(convert(float,replace(salary,'0','')))) as decimal(38,0)) from EMPLOYEESThe Blunder
4) 整理四舍五入/(向上/向下)取整相關方法 ??
(借助The Blunder的數據)
-- the type of SALARY is INT select sum(salary),count(salary) from EMPLOYEES -- >> 80935 20 -- Then the avg salary should be 4046.75 -- And the avg(salary) returns an INT select avg(salary) from EMPLOYEES -- >> 4046 -- Which means it is ignoring the decimals not rounding select avg(convert(float,salary)) from EMPLOYEES -- >> 4046.75 -- Round up / Round down select CEILING(avg(convert(float,salary))) from EMPLOYEES -- >> 4047.0 select FLOOR(avg(convert(float,salary))) from EMPLOYEES -- >> 4046.0-- Round with some 0s remained select round(avg(convert(float,salary))) from EMPLOYEES -- >> 4047.0 -- CONVERT() it select convert(int,round(avg(convert(float,salary)),0)) from EMPLOYEES -- >> 4047-- Round select cast(avg(convert(float,salary))as decimal(38,0)) from EMPLOYEES -- >> 4047 -- But CAST(... AS INT) ignores the decimals select cast(avg(convert(float,salary))as INT) from EMPLOYEES -- >> 4046總結:
1 四舍五入:
2 向上/向下取整:
3. 畫三角形
循環輸出 WHILE(con) BEGIN ... END
declare @row int declare @begin int, @starline varchar(40), @star char(2) set @row = 1; set @star = '*'; while(@row<=20)beginset @begin = 20;set @starline = '';while(@begin >= @row)beginset @starline = @starline + @star;set @begin = @begin - 1;endprint @starline;set @row = @row + 1;endvarchar vs char
Medium
1. 在兩個數之間 LEFT JOIN [table] ON A.col BETWEENB.col1 ANDB.col2
select case when grade<8 then null else name end as name, grade, marks from (select * from Students S left join Grades G on S.marks between G.min_mark and G.max_mark) t order by grade desc,nameThe Report
2. 首字母LEFT(string,num) / 大小寫UPPER() LOWER()
select name+'('+left(occupation,1)+')' from OCCUPATIONS order by name;select 'There are a total of '+convert(varchar,a.num)+' '+lower(a.occupation)+'s.' from (select occupation, count(name) num from OCCUPATIONS group by occupation) a order by num,occupation;The PADS
3. PIVOT
select min(Doctor), min(Professor),min(Singer), min(Actor) from( select ROW_NUMBER() OVER(PARTITION By Doctor,Actor,Singer,Professor order by name asc) AS Rownum, case when Doctor=1 then name else Null end as Doctor, case when Actor=1 then name else Null end as Actor, case when Singer=1 then name else Null end as Singer, case when Professor=1 then name else Null end as Professor from occupations pivot ( count(occupation) for occupation in(Doctor, Actor, Singer, Professor) ) as p ) temp group by Rownum ;Occupations
PIVOT 語句格式 (摘自MS SQL Docs FROM - Using PIVOT and UNPIVOT):
SELECT <non-pivoted column>, --不進行透視的列[first pivoted column] AS <column name>, --透視的列[second pivoted column] AS <column name>, --透視的列... --...[last pivoted column] AS <column name> --透視的列 FROM (<SELECT query that produces the data>) --數據AS <alias for the source query> PIVOT ( <aggregation function>(<column being aggregated>) --聚合函數(聚合的列) FOR [<column that contains the values that will become column headers>] --將作為列名的那列數據IN ( [first pivoted column], [second pivoted column], --透視的列... [last pivoted column]) ) AS <alias for the pivot table> --透視表別名 <optional ORDER BY clause>; --(可選)排序從句理解:from 之后的才是一個整體,把數據源pivot后再select
4. 數學函數
Sql Server函數全解(二) 數學函數
總結
以上是生活随笔為你收集整理的HackerRank笔记 - SQL Server的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派安装CentOS
- 下一篇: 数据库知识点