SQL 学习笔记

1. 问题简单描述如下:

有一个上传照片的表,字段有 用户名,上传时间,照片文件名

现在要得到最新上传照片的前10个用户名(不重复)。


select top 10 *
from (
select 用户名
,max(上传时间) as '上传时间'
from 表
group by 用户名
)t
order by 上传时间 desc


2. 在一个数据库中,有3万条数据,要查找注册电子邮箱一样的用户。

UserId UserName UserEmail
=================================================
1 ordw odks@ole.com
2 ordw odks@2le.com
3 ordw odks@cddle.com
4 ordw odks@ole.com


select * from table where UserEmail in(select UserEmail from table group by UserEmail having count(*)>1)

3. A表
id cid cname ctype cuse
1 a-1 a123 a 1
2 a-1 bcv b 5
3 a-1 vbb a 1
4 a-1 ccc b 2
b表
cid cname total
a-1 a123 500
a-1 bcv 200

我要的查詢結果是
id cid cname ctype cuse total
1 a-1 a123 a 1 500
2 a-1 bcv b 5 200
3 a-1 vbb a 1 0
4 a-1 ccc b 2 0

select a.*,total=isnull((select total from b where b.cid=a.cid and b.cname=a.cname),0) as total from a


4.表结构如下
id cost date
1 12 2005.1.1
2 13 2005.2.1
3 13 2005.3.1

现要检索如下:
检索成两个字段 2005.2.1-2005.3.1 之间的金额合计 2005.2.1前的金额合计.


select sum(case when date < '2005.2.1' then cost end ) as q_cost,sum(case when date >'2005.2.1' and date < '2005.3.1'then cost end) as h_cost from 表


5.例如产品库一张表记录各个类别中每个产品的数量,如何提取出每个类别中产品数量最多的两种

--建立测试环境
Create Table TEST
(类别 Varchar(10),
产品 Varchar(10),
数量 Int)
--插入数据
Insert TEST Values('A','A01',10)
Insert TEST Values('A','A02',13)
Insert TEST Values('A','A03',12)
Insert TEST Values('A','A05',15)
Insert TEST Values('A','A06',11)
Insert TEST Values('B','B01',20)
Insert TEST Values('B','B02',10)
Insert TEST Values('B','B03',17)
--测试

Select * from TEST A
Where Not Exists(Select 1 from TEST Where 类别=A.类别 And 数量>A.数量 Having Count(1)>1)
Order By 类别,数量 Desc

--删除测试环境
Drop Table TEST
--结果
/*
类别产品数量
A A05 15
A A02 13
B B01 20
B B03 17
*/


6.表1
ID code name phone address
1 001 张 12345 中国
2 002 李 12347 中国
3 003 刘 42342 中国
4 004 胡 76757 中国

表2
ID code linkman Tel
1 001 陈 4434232
2 003 徐 4234234
3 004 叶 6633234

表1和表2根据code 关联 输入电话查询时,查询表1和表2是否等于输入的电话,只要查询到表1或表2
中有记录更显示主表表1的记录
例如: 如果输入电话 76757 和 6633234
查询出来的结果都是 4 004 胡 76757 中国

select a.* from 表1 a,表2,b where a.code=b.code and (a.phone='你想要的号码' or b.tel='你想要的号码')


7.我要实现以下Select语句,怎么办?

数据库:
姓名,语文,数学
1 50 80
2 70 80

要选出:
1 不及格 及络
2 及格 及格


--及格标准也不告诉下.(默认60分为标准)
select
姓名,
语文=case when 语文>=60 then '及格' else '不及格' end,
数学=case when 数学>=60 then '及格' else '不及格' end
from



8.表如下:
ID Time
0 2005-10-01 09 :30
1 2005-10-01 09:31
2 2005-10-01 09 :32
3 2005-10-01 15:00:000
4 2005-10-01 18 :21:000
5 2005 10-01 18 :22:000
我想要的结果是找出一定时间范围内的Time,但是假如几条记录是3分钟之内的就只需要这几条记录的时间最早的哪一条
比如,我要找出2005-10-01 08:00 到2005-10-01 19:00:00 这段时间的 上表得出的结果是:
0 2005-10-01 09 :30
3 2005-10-01 15:00:000
4 2005-10-01 18 :21:000

declare @t table
(id int,time datetime)

insert @t
select 0,'2005-10-01 09:30' union all
select 1,'2005-10-01 09:31' union all
select 2,'2005-10-01 09:32' union all
select 3,'2005-10-01 15:00:000' union all
select 4,'2005-10-01 18:21:000' union all
select 5,'2005-10-01 18:22:000'


select * from @t a where not exists (select * from @t b where
convert(char(13),a.time,120)=convert(char(13),b.time,120) and datepart(minute,a.time)>datepart(minute,b.time))

或者
select * from @t a where
(select count(1) from @t b where
convert(char(13),a.time,120)=convert(char(13),b.time,120) and datepart(minute,a.time)>datepart(minute,b.time))<1


id time
----------- ------------------------------------------------------
0 2005-10-01 09:30:00.000
3 2005-10-01 15:00:00.000
4 2005-10-01 18:21:00.000


9.
表名:table1

字段:stcd char(30)
tm date
r5 char(10)

当中数据如下:
105 2005-10-25 21:55:00687.5
105 2005-10-25 21:50:00687.5
105 2005-10-25 21:40:00687.5
105 2005-10-25 21:35:00687.5
105 2005-10-25 21:30:00687.5
105 2005-10-25 21:25:00687.5
105 2005-10-25 21:15:00687
105 2005-10-25 20:55:00687
105 2005-10-25 20:40:00687
105 2005-10-25 20:35:00687
想返回一张表:将r5字段中每个小时中的所有数据相加返回。

select sum(r5) from table group by substring(tm,1,13)

select distinct stcd,sum(convert(float,r5)) as total
from table1
group by convert(varchar(4),DATEPART ( yy,tm))+convert(varchar(4),DATEPART ( mm,tm))+convert(varchar(4),DATEPART ( dd,tm))+convert(varchar(4),DATEPART ( hh,tm))


10.
比如
一个字段如下:
A1
A11
A2
A2.1
A2.11
A2.2

怎么实现查询后
A1
A2
A2.1
A2.2
A2.11
A11

解答:
create table #t(a varchar(20))

insert into #t values('A1')
insert into #t values('A11')
insert into #t values('A2')
insert into #t values('A2.1')
insert into #t values('A2.11')
insert into #t values('A2.2')

SELECT *
FROM qq
ORDER BY LEFT(a, 1), floor(CONVERT(float, RIGHT(a, len(a) - 1))), CONVERT(float,
isnull(RIGHT(a, len(a) - (CASE charindex('.', a) WHEN 0 THEN len(a)
ELSE charindex('.', a) END)), '0'))

(2).再增加点难度,看看能否,增加A2.2.1,A2.2.2,A2.2.11,
希望结果如下:

A1
A2
A2.1

A2.2
A2.2.1
A2.2.2
A2.2.11

A2.11
A11


create table #t(a varchar(20))

insert into #t values('A1')
insert into #t values('A11')
insert into #t values('A2')
insert into #t values('A2.1')
insert into #t values('A2.11')
insert into #t values('A2.2')
insert into #t values('A2.2.11')
insert into #t values('A2.2.1')
insert into #t values('A2.2.2')

select *from #t
order by convert(int,replace (reverse(parsename(reverse(a),1)),'A','')),
convert(int,reverse(parsename(reverse(a),2))),
convert(int,reverse(parsename(reverse(a),3))),
convert(int,reverse(parsename(reverse(a),4)))
/*
left(a,1),
convert(int,reverse(parsename(reverse(SUBSTRING(a,2,len(a)-1)),1))),
convert(int,reverse(parsename(reverse(SUBSTRING(a,2,len(a)-1)),2))),
convert(int,reverse(parsename(reverse(SUBSTRING(a,2,len(a)-1)),3))),
convert(int,reverse(parsename(reverse(SUBSTRING(a,2,len(a)-1)),4)))
*/
drop table #t

/*

A1
A2
A2.1
A2.2
A2.2.1
A2.2.2
A2.2.11
A2.11
A11


*/
查询花了多少时间
select getDate()
select * from tt
select getDate()


14.从db2 表2 里取数据,插到 db1 表1
insert db1..table1 select * from db2..table2


欢迎访问我的站点 : http://www.bt170.cn BT下载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值