(id % 2) = 0
select city from station where (id % 2) = 0
Weather Observation Station 3
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.Continent
Average Population of Each Continent
CAST(num AS DECIMAL(38,len))
select cast(sum(LAT_N) as decimal(38,2)),cast(sum(LONG_W) as decimal(38,2)) from STATION
Weather Observation Station 2
CEILING()
/ 向下取整FLOOR()
select cast(CEILING(avg(convert(float,salary)) -
avg(convert(float,replace(salary,'0','')))) as decimal(38,0))
from EMPLOYEES
The Blunder
(借助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 四舍五入:
CONVERT(INT, ROUND(num, 0))
CAST(num AS DECIMAL(38, len))
2 向上/向下取整:
CEILING()
/ 向下取整FLOOR()
CAST(num AS INT)
(忽略小数 = 向下取整)WHILE(con) BEGIN ... END
declare @row int
declare @begin int, @starline varchar(40), @star char(2)
set @row = 1;
set @star = '*';
while(@row<=20)
begin
set @begin = 20;
set @starline = '';
while(@begin >= @row)
begin
set @starline = @starline + @star;
set @begin = @begin - 1;
end
print @starline;
set @row = @row + 1;
end
varchar
vs char
varchar(n)
可变长度字符串数据,n
不指定时默认为1。char(n)
固定长度字符串数据,若不足位后面补空字符串,n
不指定时默认为1。BETWEEN
B.col1 AND
B.col2select 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,name
The Report
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
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
ABS(x)
和返回圆周率的函数PI()
SQRT(x)
RAND()
和RAND(x)
ROUND(x,y)
SIGN(x)
CEILING(x)
和FLOOR(x)
POWER(x,y)
、SQUARE(x)
、和EXP(x)
LOG(x)
和LOG10(x)
RANDIANS(x)
和DEGREES(x)
SIN(x)
和反正弦函数ASIN(x)
COS(x)
和反余弦函数ACOS(x)
TAN(x)
,反正切函数ATAN(x)
和余切函数COT(x)
Sql Server函数全解(二) 数学函数