ASP.NET SQL 总结

1.SQLSERVER服务器中,给定表 table1 中有两个字段 ID、LastUpdateDate,ID表示更新的事务号, LastUpdateDate表示更新时的服务器时间,请使用一句SQL语句获得最后更新的事务号

答:Select ID FROM table1 Where LastUpdateDate = (Select MAX(LastUpdateDate) FROM table1)

2.SQLSERVER服务器中,给定表 table1 中有两个字段 ID、LastUpdateDate,ID表示更新的事务号, LastUpdateDate表示更新时的服务器时间,请使用一句SQL语句获得最后更新的事务号

答:Select ID FROM table1 Where LastUpdateDate = (Select MAX(LastUpdateDate) FROM table1)

3.写出求某字段最大值的SQL语句.
答:select max(字段) from 表

4.单的SQL子查询,左右连接语句
答:select * from a left outer join b on
select * from a right outer join b on

5.请在SQL Server中设计表来保存一个树状结构的组织结构图(假设结构图中只有名称这一项内容需要保存),如果我想查询某一职位下的所有职位,用一个存储过程来实现,你有什么思路?
答:parent表(字段:pid,pname),child表(字段:cid,cname,pid)
 Create procedure positin_List

{

    @pid  int

    select * from tb_parent where pid_id = @pid

}

 6.对单表进行模糊查询,并对其中两列分别进行升序和降序排列(在一个Select语句中)
答:select au_id,au_lname  from dbo.authors where state like ‘%CA%’
group by au_id,au_lname
order by au_lname asc,au_id desc
 7.根据上题中的表编写一个带输入参数的存储过程,输入参数的数据类型为varchar。例如:输入参数为“20060912”,返回表中所有日期字段大于该日期的记录
create procedure returnData
@param varchar(20)
AS
select * from dbo.employee where cast(@param as datetime) < hire_date
go

8.管理业务培训信息,建立3个表:

     S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号,学员姓名,所属单位,学员年龄

     C(C#,CN)C#,CN分别代表课程编号,课程名称

     SC(S#,C#,G) S#,C#,G分别代表学号,所选的课程编号,学习成绩

(1)使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名?

答案:select s# ,sn from s where S# in(select S# from c,sc where c.c#=sc.c# and  cn=’税收基础’)

(2) 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位?

 答:select sn,sd from s,sc where s.s#=sc.s# and sc.c#=’c2’

(3) 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位?

答:select sn,sd from s where s# not in(select s# from sc where c#=’c5’)

(4)查询选修了课程的学员人数

答:select 学员人数=count(distinct s#) from sc

(5) 查询选修课程超过5门的学员学号和所属单位?

答:select sn,sd from s where s# in(select s# from sc group by s# having count(distinct  c#)>5)

 是查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的

查询语句如下:

 select  top 10 * from A where ID >(select max(ID) from (select  top 30 ID from A order by A) T) order by A

 要求是查询表A中存在ID重复三次以上的记录,完整的查询语句如下:

select * from(select count(ID) as count from table group by ID)T where T.count>3

 create table testtable1

(

 id int IDENTITY,

 department varchar(12)

)

 select * from testtable1

insert into testtable1 values('设计')

insert into testtable1 values('市场')

insert into testtable1 values('售后')

/*

结果

id  department

1   设计

2   市场

3   售后

*/

create table testtable2

(

 id int IDENTITY,

 dptID int,

 name varchar(12)

)

insert into testtable2 values(1,'张三')

insert into testtable2 values(1,'李四')

insert into testtable2 values(2,'王五')

insert into testtable2 values(3,'彭六')

insert into testtable2 values(4,'陈七')

/*

用一条SQL语句,怎么显示如下结果

id  dptID  department  name

1   1      设计        张三

2   1      设计        李四

3   2      市场        王五

4   3      售后        彭六

5   4      黑人        陈七

*/

 答案是:

 SELECT testtable2.*  , ISNULL(department,'黑人') FROM testtable1 right join testtable2 on testtable2.dptID = testtable1.ID

9.S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名

C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任

课教师

SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩

1. 找出没有选修过“李明”老师讲授课程的所有学生姓名

--实现代码:

Select SNAME FROM S Where NOT EXISTS( Select * FROM SC,C Where SC.CNO=C.CNO

AND CNAME='李明' AND SC.SNO=S.SNO)

2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩

--实现代码:

Select S.SNO,S.SNAME,AVG_SCGRADE=AVG(SC.SCGRADE) FROM S,SC,( Select SNO FROM

SC Where SCGRADE<60 GROUP BY SNO HAVING COUNT(DISTINCT CNO)>=2 )A Where

S.SNO=A.SNO AND SC.SNO=A.SNO GROUP BY S.SNO,S.SNAME

3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名

--实现代码:

Select S.SNO,S.SNAME FROM S,( Select SC.SNO FROM SC,C Where SC.CNO=C.CNO AND

C.CNAME IN('1','2') GROUP BY SNO HAVING COUNT(DISTINCT CNO)=2 )SC Where

S.SNO=SC.SNO

4. 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号

--实现代码:

Select S.SNO,S.SNAME FROM S,( Select SC1.SNO FROM SC SC1,C C1,SC SC2,C C2

Where SC1.CNO=C1.CNO AND C1.NAME='1' AND SC2.CNO=C2.CNO AND C2.NAME='2' AND

SC1.SCGRADE>SC2.SCGRADE )SC Where S.SNO=SC.SNO

5. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课

的成绩

--实现代码:

Select S.SNO,S.SNAME,SC.[1号课成绩],SC.[2号课成绩] FROM S,( Select SC1.SNO,[1

号课成绩]=SC1.SCGRADE,[2号课成绩]=SC2.SCGRADE FROM SC SC1,C C1,SC SC2,C C2

Where SC1.CNO=C1.CNO AND C1.NAME='1' AND SC2.CNO=C2.CNO AND C2.NAME='2' AND

SC1.SCGRADE>SC2.SCGRADE )SC Where S.SNO=SC.SNO

 

 

求其中同一个号码的两次通话之间间隔大于10秒的通话记录ID

例如:6,7,8,9,10条记录均符合

ID 主叫号码 被叫号码      通话起始时间            通话结束时间            通

话时长

1  98290000 0215466546656 2007-02-01 09:49:53.000 2007-02-01 09:50:16.000 23

2  98290000 021546654666  2007-02-01 09:50:29.000 2007-02-01 09:50:41.000 12

3  98290000 021546654666  2007-02-01 09:50:58.000 2007-02-01 09:51:12.000 14

4  68290900 0755133329866 2007-02-01 10:04:31.000 2007-02-01 10:07:13.000 162

5  78290000 0755255708638 2007-02-01 10:48:26.000 2007-02-01 10:49:23.000 57

6  78290000 0755821119109 2007-02-01 10:49:39.000 2007-02-01 10:52:55.000 196

7  78290000 035730928370  2007-02-01 11:30:45.000 2007-02-01 11:31:58.000 73

8  78290000 0871138889904 2007-02-01 11:33:47.000 2007-02-01 11:35:00.000 73

9  68290000 035730928379  2007-02-01 11:52:20.000 2007-02-01 11:54:56.000 156

10 68290000 0298521811199 2007-02-01 12:44:45.000 2007-02-01 12:45:04.000 19

 答案:

SELECT DISTINCT a.* FROM dbo.hc a left join dbo.hc b ON a.主叫号码=b.主叫号码

WHERE a.id<>b.id AND (DATEDIFF(second,a.通话起始时间,b.通话结束时间)>10 AND DATEDIFF(second,b.通话起始时间,a.通话结束时间)>10)

 

 

Sql Server关于按周统计的问题

统计Sql Server里一个销售明细表里某个时间段的销售额,而且要按周进行比较,以下是该语句的写法:

 select sum(销售金额), datename(week, 销售日期-1) from sales where 销售日期 betwee begindate and enddate group by datename(week, 销售日期-1)

修改数据库

alter table [dbo].[TrackInfoes] add TrackLevel int not null default(1)
alter table  [dbo].[ShareLogs] add ShareLevel int not null default(1)

 

posted @ 2016-05-09 23:34  李寒星  阅读(1083)  评论(0编辑  收藏  举报