ACCESSsql知识整理(4)

Group
by Fage Having Count(*)>1

克结果集行数

Select
top 3 * from T_Employee order by FSalary desc

摸索按照工资从赛顶没有排序检索从第六名开始一共叔独人口的消息

select
top 3 * from T_Employee 

where
FNumber not in (select top 5 FNumber from T_Employee order by FSalary
desc)

order
by FSalary desc

sql
server2005后长了Row_Number函数简化实现,用Row_Number函数也能为此来分页

select
*

from

(

select
*,row_number() over (order by salary desc) as row_num from
T_Employee

)as
T_EmployeeWithRowNumbers

where T_EmployeeWithRowNumbers.row_num
between 6 and 8

错过丢数据更

select
distinct FDepartment from T_Employee

distinct是对普结果集进行数量再度处理的,而未是针对性各国一个列

select
distinct FDepartment,FSubCompany  from
T_Employee(distinct是针对FDepartment,FSubCompany两只列,而无是一致列)

Union(如果更的数额会剔除掉,union
all不会见去重复数据)

select
FName,Fage from T_TempEmployee

union

select
FName,Fage from T_Employee

union因为要是开展更值扫描,所以效率不如,因此要非是确定要合并重复行,那么尽管就此union
all

数字函数

ABS():求绝对值

CEILING():舍入到极致老整数。3.33拿于舍入为4,2.89将给舍入为3,-3.61以受舍入为-3

Ceiling->天花板

FLOOR():舍入到最好小平头。3.33将为舍入为3,2.89以吃舍入为2,-3.61以于舍入为-4

Floor->地板

ROUND():四放弃五可。

Round->“半径”。Round(3.1425,2)->3.14
后面的“2”指为保留2位精度

字符串函数

LEN():计算字符串长度

LOWER(),UPPER():转小写,大写

LTRIM():字符串左侧的空格去丢

RTRIM():字符串右侧的空格去丢

LTRIM(RTRIM()),字符串两度的空格去丢

SUBSTRING(string,start_position,length)

参数string为主字符串,start_position为子字符串在主字符串中开始位置,length为字符串的最好酷长

日期函数

GETDATE():取得时日子时

DATEADD(datepart,number,date),计算增加后的日子。参数date为要计算的日期,参数number为增量,参数datepart为计量单位,可选值见备注。DATEADD(DAY,3,date)为计日期date的3龙后的日期,而DATEADD(DAY,3,date)为计日期date的3龙后的日期,而DATEADD(MONTH,-8,date)为计日期date的8独月之前的日子

datepart可选值(year,quarter(季度),month,day,week,hh)

DATEDIFF(datepart,startdate,enddate):计算两只日子中的差额。datepart为计量单位,可选值参考datepart

eg:select
FName,FInDate,DateDiff(year,FIndate,getdate()) from T_Employee

DATEPART(datepart,date):返回一个日期的一定部分

select
DatePart(year,getdate())->如果是今年凡是2011年,则回2011

类型转换函数

CAST(expression
AS data_type) eg:select cast(‘123’ as int),cast(‘2008-08-08’ as
datetime)

CONVERT(date_type,expression)
eg:select convert(datetime,’2008-08-08′),convert(varchar(50),123)

SELECT
FldNumber

RIGHT(FldNumber,3)
as 后三位

CAST(RIGHT(FldNumber,3)
AS INTEGER) as 后三各之平头形式

空值处理函数

ISNULL(expression,value):如果expression不呢空则返回expression,否则回value。eg
:select isnull(FName,’佚名’) as 姓名 from T_Employee

case函数用法1

单值判断,相当给switch
case

CASE
expression

WHEN
value1 THEN returnvalue1

WHEN
value2 THEN returnvalue2

WHEN
value3 THEN returnvalue3

ELSE
defaultreturnvalue

END

eg:

SELECT FName,

(

CASE FLevel 

WHEN 1 THEN ‘VIP客户’

WHEN 2 THEN ‘高级客户’

WHEN 3 THEN ‘一般性客户’

ELSE ‘客户类型错误’

END

)as FLevelName

FROM T_Customer

select FName,

(

case

when FSalary<2000 then ‘低收入’

when FSalary>=2000 and FSalary<=5000 then ‘中等收入’

else ‘高收入’

end

) as 收入水平

from T_Employee

表中发出A、B、C三列,用SQL语句实现:当A排大于B列时选择A列否则选择B列,当B列大于C列时选B列否则选择C列

select (case when a>b then a else b end),(case when b>c then b
else c end) from t

(

我的表结构如下 duiwu     shengfu1         胜2         胜1         胜1         负2         负1         胜2         负要查询出如下结果:duiwu   胜     负1       3      12       1      2就是分别统计出每支队伍的胜负次数,这样的sql语句该怎么写啊?大虾帮帮忙啊,谢谢了

最佳答案

select a.duiwu ,a.胜,b.负 from (select count(*) 胜, duiwu from tabb where shengfu='胜' group by duiwu ) a left join (select count(*) 负, duiwu from tabb where shengfu='负' group by duiwu ) b on a.duiwu=b.duiwu --sql2000调试通过,可以自己用case when改写一下

–sum后加括号,里面放case when

select duiwu ,
sum(case when shengfu = ‘胜’ then 1 else 0 end) win,
sum(case when shengfu = ‘负’ then 1 else 0 end) lost
from 表
group by duiwu ;

一旦您用的是ACCESS数据库,可以用于下查询语句:
select duiwu,sum(iif(shengfu=”胜”,1,0)) as 胜,sum(iif(shengfu=”负”,1,0))
as 负 from 表名 group by duiwu 

)

ACCESS 1

表名:CallRecords
字段名:id,caller_id,telNum,starttime,endtime

1.select top 5 *
from CallRecords
order by datediff(second,starttime,endtime) desc

2.select sum(datediff(second,starttime,endtime))

from CallRecords
where telNum like ‘0%’

3.select top 3 caller_id

from CallRecords
where datediff(month,starttime,getdate())=0
 –或datepart(month,starttime)=datepart(month,getdate()) 应该也足以
group by caller_id
order by sum(datediff(second,starttime,endtime)) desc

4.select caller_id as 呼叫员编号,telNum as
对方号码,datediff(second,starttime,endtime) as 通话时长
from CallRecords
union all
select ‘汇总’,
sum(case when telNum like ‘0%’ then 0 else
datediff(second,starttime,endtime) end) as 市内号码总时长
sum(case when telNum like ‘0%’ then datediff(second,starttime,endtime)
else 0 end) as 市内号码总时长
from  CallRecords

索引

缔造索引的措施,在说明设计器中点击右键,选择“索引/键”-》添加-》在排被甄选索引包含的排

采取索引能增强查询效率,但是索引也是占据空间的,而且丰富、更新、删除数据的时节呢待并更新索引,因此会面回落insert、update、delete的快慢。只当经常找的字段上(一般也where)创建索引。

纵然创建了目录,仍然有或全表扫描,比如like、函数、类型转换等

相关文章