--IF ELSE
declare @x int @y int @z int
select @x = 1 @y = 2 @z=3
if @x > @y
print 'x > y' --打印字符串'x > y'
else if @y > @z
print 'y > z'
else print 'z > y'
--CASE
use pangu
update employee
set e_wage =
case
when job_level = ’1’ then e_wage*1.08
when job_level = ’2’ then e_wage*1.07
when job_level = ’3’ then e_wage*1.06
else e_wage*1.05
end
--WHILE CONTINUE BREAK
declare @x int @y int @c int
select @x = 1 @y=1
while @x < 3
begin
print @x --打印变量x 的值
while @y < 3
begin
select @c = 100*@x + @y
print @c --打印变量c 的值
select @y = @y + 1
end
select @x = @x + 1
select @y = 1
end
--WAITFOR
--例 等待1 小时2 分零3 秒后才执行SELECT 语句
waitfor delay ’01:02:03’
select * from employee
--例 等到晚上11 点零8 分后才执行SELECT 语句
waitfor time ’23:08:00’
select * from employee
***SELECT***
select *(列名) from table_name(表名) where column_name operator value
ex:(宿主)
select * from stock_information where stockid = str(nid)
stockname = 'str_name'
stockname like '% find this %'
stockname like '[a-zA-Z]%' --------- ([]指定值的范围)
stockname like '[^F-M]%' --------- (^排除指定范围)
--------- 只能在使用like关键字的where子句中使用通配符)
or stockpath = 'stock_path'
or stocknumber < 1000
and stockindex = 24
not stock*** = 'man'
stocknumber between 20 and 100
stocknumber in(10,20,30)
order by stockid desc(asc) --------- 排序,desc-降序,asc-升序
order by 1,2 --------- by列号
stockname = (select stockname from stock_information where stockid = 4)
--------- 子查询
--------- 除非能确保内层select只返回一个行的值,
--------- 否则应在外层where子句中用一个in限定符
select distinct column_name form table_name --------- distinct指定检索独有的列值,不重复
select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name
select stockname , "stocknumber" = count(*) from table_name group by stockname
--------- group by 将表按行分组,指定列中有相同的值
having count(*) = 2 --------- having选定指定的组
select *
from table1, table2
where table1.id *= table2.id -------- 左外部连接,table1中有的而table2中没有得以null表示
table1.id =* table2.id -------- 右外部连接
select stockname from table1
union [all] ----- union合并查询结果集,all-保留重复行
select stockname from table2
***insert***
insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx")
value (select Stockname , Stocknumber from Stock_table2)---value为select语句
----系统函数----
APP_NAME() --函数返回当前执行的应用程序的名称
COALESCE() --函数返回众多表达式中第一个非NULL 表达式的值
COL_LENGTH(<'table_name'>, <'column_name'>) --函数返回表中指定字段的长度值
COL_NAME(, ) --函数返回表中指定字段的名称即列名
DATALENGTH() --函数返回数据表达式的数据的实际长度
DB_ID(['database_name']) --函数返回数据库的编号
DB_NAME(database_id) --函数返回数据库的名称
HOST_ID() --函数返回服务器端计算机的名称
HOST_NAME() --函数返回服务器端计算机的名称
IDENTITY([, seed increment]) [AS column_name])
--IDENTITY() 函数只在SELECT INTO 语句中使用用于插入一个identity column列到新表中
/*select identity(int, 1, 1) as column_name
into newtable
from oldtable*/
ISDATE() --函数判断所给定的表达式是否为合理日期
ISNULL(, ) --函数将表达式中的NULL 值用指定值替换
ISNUMERIC() --函数判断所给定的表达式是否为合理的数值
NEWID() --函数返回一个UNIQUEIDENTIFIER 类型的数值
NULLIF(, )
--NULLIF 函数在expression1 与expression2 相等时返回NULL 值若不相等时则返回expression1 的值
精妙SQL语句
说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 1<>1
说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
SQL: insert into b(a, b, c) select d,e,f from b;
说明:显示文章、提交人和最后回复时间
SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
说明:外连接查询(表名1:a 表名2:b)
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
说明:日程安排提前五分钟提醒
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
说明:两张关联表,删除主表中已经在副表中没有的信息
SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid )
说明:--
SQL:
SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE
FROM TABLE1,
(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE
FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND
FROM TABLE2
WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X,
(SELECT NUM, UPD_DATE, STOCK_ONHAND
FROM TABLE2
WHERE TO_CHAR(UPD_DATE,'YYYY/MM') =
TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM') || '/01','YYYY/MM/DD') - 1, 'YYYY/MM') ) Y,
WHERE X.NUM = Y.NUM (+)
AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND ) B
WHERE A.NUM = B.NUM
说明:--
SQL:
select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名称='"&strdepartmentname&"' and 专业名称='"&strprofessionname&"' order by 性别,生源地,高考总成绩
说明:
从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)
SQL:
SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DEC
FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration
FROM TELFEESTAND a, TELFEE b
WHERE a.tel = b.telfax) a
GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy')
说明:四表联查问题:
SQL: select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....
说明:得到表中最小的未使用的ID号
SQL:
SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID
FROM Handle
WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)
主 题:专贴揭示SQL语句,贴出你的精妙SQL,欢迎来抢分!
作 者:csdntoll (低调惯了)
等 级:
信 誉 值:147
所属论坛:Web 开发 ASP
问题点数:200
回复次数:260
发表时间:2003-3-18 18:02:24
格式
说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 1<>1
回复人: LuoGD(沃适) ( ) 信誉:100 2003-3-18 18:07:37 得分:5
说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
insert into b(a, b, c) select d,e,f from b;
Top
回复人: LuoGD(沃适) ( ) 信誉:100 2003-3-18 18:09:37 得分:5
说明:合并数据(表名1:a 表名2:b)
select a,b,c from a union select d,e,f from b;
Top
回复人: LuoGD(沃适) ( ) 信誉:100 2003-3-18 18:11:36 得分:5
说明:子查询(表名1:a 表名2:b)
select a,b,c from a where a IN (select d from b )
或者:
select a,b,c from a where a IN (1,2,3)
Top
回复人: _TMG_(Alan) ( ) 信誉:120 2003-3-18 18:11:55 得分:0
这都是精妙?
Top
回复人: junsisi(君三思) ( ) 信誉:100 2003-3-18 18:12:36 得分:5
俺来贴个让俺对sql茅塞顿开的例子
说明:显示文章、提交人和最后回复时间
sql:select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
Top
回复人: LuoGD(沃适) ( ) 信誉:100 2003-3-18 18:13:48 得分:0
说明:外连接查询(表名1:a 表名2:b)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
Top
回复人: LuoGD(沃适) ( ) 信誉:100 2003-3-18 18:15:56 得分:0
说明:在线视图查询(表名1:a )
select * from (SELECT a,b,c FROM a) T where t.a > 1;
Top
回复人: net_lover(孟子E章) ( ) 信誉:727 2003-3-18 18:16:14 得分:0
SQL:SELECT NEWID()
Top
回复人: snakegod(蛇姬) ( ) 信誉:100 2003-3-18 21:49:41 得分:0
????
没有深度 没兴趣
Top
回复人: _TMG_(Alan) ( ) 信誉:120 2003-3-18 21:54:21 得分:0
我宁愿用数据库冗余也不会使用子查询,除非万不得已
Top
回复人: nittystone(没有想法) ( ) 信誉:115 2003-3-18 22:20:49 得分:5
自连接取出荣于数据
把所有姓名相同的只取出一个
select a.name from table_name a where a.id in
(select b.id from table_name b where a.id<>b.id)
同理删除荣誉数据
delete from table_name where table_name.id in
(select b.id from table_name b where table_name.id<>b.id)
Top
回复人: allforly(白衣胜血) ( ) 信誉:126 2003-3-19 1:18:29 得分:0
今天开眼界了啊,我就会select * from ** where **=**这一句
Top
回复人: gddd(gddd) ( ) 信誉:100 2003-3-19 2:07:30 得分:0
利害...我会多一句
select top n * form ** where XXX
Top
回复人: csdntoll(低调惯了) ( ) 信誉:147 2003-3-19 9:22:40 得分:0
不错,不错,
蛮喜欢junsisi(君三思)的这句:
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
对net_lover(孟子E章)前辈,这句不理解,我菜,呵呵
SQL:SELECT NEWID()
Top
回复人: cpp2017(幕白兄) ( ) 信誉:145 2003-3-19 9:27:39 得分:0
SELECT DISTINCT TSD.Time_Sheet_Dtl_Record_No,TSD.User_Record_No,TP.Period_Start_Date ,TP.Period_End_Date INTO #temp FROM Time_Sheet_Details TSD, Time_Sheet_Period TP ,User_Group_User_Relationship UGUR,User_Group_Master UGM ,User_Data_Access_Right UDAR WHERE TSD.status ='TS_WFMGRA' AND DATEDIFF(dd,TP.period_start_date,TSD.work_Date)>=0 AND DATEDIFF(dd,TP.period_end_date,TSD.work_Date)<=0 AND UGUR.User_Record_No = TSD.User_Record_No AND UGM.User_Group_Record_No=UGUR.User_Group_Record_No AND UGM.User_Group_Name IN('Technician','Engineer') AND UDAR.User_Record_No = TSD.User_Record_No AND UDAR.Division_Record_No IN(1) SELECT DISTINCT A.User_Record_No, B.Staff_No, B.Full_Name,B.Job_Title, SUM(working_hour) AS Working_Hours,SUM(ot) AS OT_HOURS, C.Period_Start_Date,C.Period_End_Date INTO #temp2 FROM Time_Sheet_Details A INNER JOIN User_Master B ON B.User_Record_No= A.User_Record_No INNER JOIN #temp C ON C.Time_Sheet_Dtl_Record_No = A.Time_Sheet_Dtl_Record_No GROUP BY A.User_Record_No, B.Staff_No,B.Full_Name,B.Job_Title, C.Period_Start_Date,C.Period_End_Date HAVING COUNT(*) = DATEDIFF(d,C.Period_Start_Date,C.Period_End_Date)+1 Select A.User_Record_No,A.Staff_No,A.Full_Name,A.Job_Title ,A.Working_Hours,A.OT_HOURS,A.Period_Start_Date,A.Period_End_Date ,DM.Division_Code INTO #temp3 FROM #temp2 AS A INNER JOIN User_Data_Access_Right UDAR ON UDAR.User_Record_No = A.User_Record_No INNER JOIN Division_Master DM ON DM.Division_Record_No = UDAR.Division_Record_No SELECT * From #temp3 order by 1,7,8; Select Count(Distinct User_Record_No+Period_Start_Date+Period_End_Date) From #temp3 DROP TABLE #temp ,#temp2,#temp3
Top
回复人: cloud1002(好得掉渣~) ( ) 信誉:100 2003-3-19 9:35:06 得分:0
mark
Top
回复人: smallmuda(飞翔的猪) ( ) 信誉:101 2003-3-19 9:37:27 得分:0
select * from a where time between time1 and time2
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 9:40:15 得分:0
Top
回复人: cpp2017(幕白兄) ( ) 信誉:145 2003-3-19 9:41:08 得分:5
select isNull(A.name,B.name),isnull(a.code,B.code) from table1 A
full out join table2 B on
A.id = B.id
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 9:43:29 得分:0
to cpp2017(长安不见使人愁)
jeff?
Top
回复人: zhw_yihui(卜卢特) ( ) 信誉:94 2003-3-19 9:53:25 得分:0
SQL:SELECT NEWID()
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 9:58:59 得分:0
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
选择在每一组b值相同的数据中对应的a最大的(换成average或别的函数或子查询,你会有意想不到的发现)记录的所有信息.
类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.
上面的许多同志对子查询存在有偏见与误解,其实在一个好的数据分析程序中,子查询可以简化很多程序逻辑.
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 10:01:40 得分:0
上面select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
这种相关子查询和delete(insert)等结合起来,可以用于消除数据库中某个字段(或某些)的重复值
Top
回复人: hubinasm(火星撞地球) ( ) 信誉:115 2003-3-19 10:10:41 得分:0
(select a from tableA ) except (select a from tableB) except (select a from tableC)
Top
回复人: aocool(知秋一叶) ( ) 信誉:100 2003-3-19 10:14:11 得分:0
获益不浅啊.
SELECT DISTINCT TSD.Time_Sheet_Dtl_Record_No,TSD.User_Record_No,TP.Period_Start_Date ,TP.Period_End_Date INTO #temp FROM Time_Sheet_Details TSD, Time_Sheet_Period TP ,User_Group_User_Relationship UGUR,User_Group_Master UGM ,User_Data_Access_Right UDAR WHERE TSD.status ='TS_WFMGRA' AND DATEDIFF(dd,TP.period_start_date,TSD.work_Date)>=0 AND DATEDIFF(dd,TP.period_end_date,TSD.work_Date)<=0 AND UGUR.User_Record_No = TSD.User_Record_No AND UGM.User_Group_Record_No=UGUR.User_Group_Record_No AND UGM.User_Group_Name IN('Technician','Engineer') AND UDAR.User_Record_No = TSD.User_Record_No AND UDAR.Division_Record_No IN(1) SELECT DISTINCT A.User_Record_No, B.Staff_No, B.Full_Name,B.Job_Title, SUM(working_hour) AS Working_Hours,SUM(ot) AS OT_HOURS, C.Period_Start_Date,C.Period_End_Date INTO #temp2 FROM Time_Sheet_Details A INNER JOIN User_Master B ON B.User_Record_No= A.User_Record_No INNER JOIN #temp C ON C.Time_Sheet_Dtl_Record_No = A.Time_Sheet_Dtl_Record_No GROUP BY A.User_Record_No, B.Staff_No,B.Full_Name,B.Job_Title, C.Period_Start_Date,C.Period_End_Date HAVING COUNT(*) = DATEDIFF(d,C.Period_Start_Date,C.Period_End_Date)+1 Select A.User_Record_No,A.Staff_No,A.Full_Name,A.Job_Title ,A.Working_Hours,A.OT_HOURS,A.Period_Start_Date,A.Period_End_Date ,DM.Division_Code INTO #temp3 FROM #temp2 AS A INNER JOIN User_Data_Access_Right UDAR ON UDAR.User_Record_No = A.User_Record_No INNER JOIN Division_Master DM ON DM.Division_Record_No = UDAR.Division_Record_No SELECT * From #temp3 order by 1,7,8; Select Count(Distinct User_Record_No+Period_Start_Date+Period_End_Date) From #temp3 DROP TABLE #temp ,#temp2,#temp3
:)
cpp2017(长安不见使人愁)这么长一句,少见,能否介绍介绍它的功力?^_^
SELECT DISTINCT TSD.Time_Sheet_Dtl_Record_No,TSD.User_Record_No,TP.Period_Start_Date ,TP.Period_End_Date INTO #temp FROM Time_Sheet_Details TSD, Time_Sheet_Period TP ,User_Group_User_Relationship UGUR,User_Group_Master UGM ,User_Data_Access_Right UDAR WHERE TSD.status ='TS_WFMGRA' AND DATEDIFF(dd,TP.period_start_date,TSD.work_Date)>=0 AND DATEDIFF(dd,TP.period_end_date,TSD.work_Date)<=0 AND UGUR.User_Record_No = TSD.User_Record_No AND UGM.User_Group_Record_No=UGUR.User_Group_Record_No AND UGM.User_Group_Name IN('Technician','Engineer') AND UDAR.User_Record_No = TSD.User_Record_No AND UDAR.Division_Record_No IN(1) SELECT DISTINCT A.User_Record_No, B.Staff_No, B.Full_Name,B.Job_Title, SUM(working_hour) AS Working_Hours,SUM(ot) AS OT_HOURS, C.Period_Start_Date,C.Period_End_Date INTO #temp2 FROM Time_Sheet_Details A INNER JOIN User_Master B ON B.User_Record_No= A.User_Record_No INNER JOIN #temp C ON C.Time_Sheet_Dtl_Record_No = A.Time_Sheet_Dtl_Record_No GROUP BY A.User_Record_No, B.Staff_No,B.Full_Name,B.Job_Title, C.Period_Start_Date,C.Period_End_Date HAVING COUNT(*) = DATEDIFF(d,C.Period_Start_Date,C.Period_End_Date)+1 Select A.User_Record_No,A.Staff_No,A.Full_Name,A.Job_Title ,A.Working_Hours,A.OT_HOURS,A.Period_Start_Date,A.Period_End_Date ,DM.Division_Code INTO #temp3 FROM #temp2 AS A INNER JOIN User_Data_Access_Right UDAR ON UDAR.User_Record_No = A.User_Record_No INNER JOIN Division_Master DM ON DM.Division_Record_No = UDAR.Division_Record_No SELECT * From #temp3 order by 1,7,8; Select Count(Distinct User_Record_No+Period_Start_Date+Period_End_Date) From #temp3 DROP TABLE #temp ,#temp2,#temp3
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 11:14:05 得分:0
我认为上面的语句在实现需求时并不是最简,效率最高的语句.
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-19 11:45:29 得分:0
各种查询技巧结合才能写出最高效的查询
在SQL中使用DISTINCT是代价比较高的查询方式
Top
回复人: oldsky(九指神丐) ( ) 信誉:54 2003-3-19 13:13:09 得分:0
Top
回复人: funboy88(司令) ( ) 信誉:121 2003-3-19 13:20:20 得分:0
pwdencrypt
密码加密?
有些什么作用,不明白
随机取出10条数据
select top 10 * from tablename order by newid()
Top
回复人: mzcih(小马过河) ( ) 信誉:110 2003-3-19 13:25:31 得分:0
看君一张贴,胜看一天书。
Top
回复人: funboy88(司令) ( ) 信誉:121 2003-3-19 13:26:48 得分:5
功能:
type vender pcs
电脑 A 1
电脑 A 1
光盘 B 2
光盘 A 2
手机 B 3
手机 C 3
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
Top
回复人: funboy88(司令) ( ) 信誉:121 2003-3-19 13:30:27 得分:0
FUNCTION:DELELTE REPEAT RECORDS
DELETE from tablename where id not in (select max(id) from tablename group by col1,col2,...)
Top
回复人: chonboy(一只来自南方的羊) ( ) 信誉:100 2003-3-19 14:32:56 得分:0
Top
回复人: flyinto(孤独求败) ( ) 信誉:100 2003-3-20 14:14:42 得分:0
MARK
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-20 14:15:00 得分:0
source data
kzx4dm xbdm jylsfsdm ...
11 2 10
11 2 10
12 2 10
12 1 20
12 1 20
destination:
kzx4dm bys_count yjs_count jy_ratio
11 2 2 1.00
12 3 1 1/3
SELECT DISTINCT kzx4dm,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm) AS bys_count,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm WHERE jylsfsdm=10) AS yjs_count,yjs_count/bys_count AS jy_ratio
FROM tablename AS TA
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-20 14:18:35 得分:10
to annkie(活着便精彩)
看看上面的语句是否解决了问题?
纠正一下,应该是
SELECT DISTINCT kzx4dm,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm) AS bys_count,(SELECT COUNT(jylsfsdm) FROM tablename WHERE kzx4dm=TA.kzx4dm AND jylsfsdm=10) AS yjs_count,yjs_count/bys_count AS jy_ratio
FROM tablename AS TA
Top
回复人: GageCSDN(稻草人) ( ) 信誉:100 2003-3-20 17:30:45 得分:0
seeing........
Top
回复人: allserver(server) ( ) 信誉:100 2003-3-20 19:13:25 得分:0
8错
Top
回复人: kirc(无聊中...) ( ) 信誉:100 2003-3-20 19:16:32 得分:0
关注....
Top
回复人: annkie(无声的雨) ( ) 信誉:113 2003-3-20 19:28:23 得分:0
To yonghengdizhen:
thank you.
正在研究此问题中.
yjs_count/bys_count AS jy_ratio 这句无法通过检查,不过可以把前面的东西拿倒这里来搞定,不使用字段名.
你的sql语句是对的.
Top
回复人: flyycyu(fly) ( ) 信誉:100 2003-3-21 9:36:40 得分:0
gz
Top
回复人: csdntoll(低调惯了) ( ) 信誉:147 2003-3-21 10:02:00 得分:0
我决定:把200分中的180分,奖给贴出最精妙的SQL的高手!
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-21 11:00:52 得分:0
二维表 T(F1,F2,F3,F4,F5,F6,F7) 表示如下关系:
学生ID 学生姓名 课程ID 课程名称 成绩 教师ID 教师姓名
S3 王五 K4 政治 53 T4 赵老师
S1 张三 K1 数学 61 T1 张老师
S2 李四 K3 英语 88 T3 李老师
S1 张三 K4 政治 77 T4 赵老师
S2 李四 K4 政治 67 T5 周老师
S3 王五 K2 语文 90 T2 王老师
S3 王五 K1 数学 55 T1 张老师
S1 张三 K2 语文 81 T2 王老师
S4 赵六 K2 语文 59 T1 王老师
S1 张三 K3 英语 37 T3 李老师
S2 李四 K1 数学 81 T1 张老师
请以一句 T-SQL (Ms SQL Server) 或 Jet SQL (Ms Access) 在 原表 T 基础上作答
1.如果 T 表还有一字段 F0 数据类型为自动增量整型(唯一,不会重复),
而且 T 表中含有除 F0 字段外,请删除其它字段完全相同的重复多余的脏记录数据:
delete from t where f0 in(select max(f0) from t group by f1,f2,f3,f4,f5,f6,f7 having count(f0)>1)
2.列印各科成绩最高和最低的记录: (就是各门课程的最高、最低分的学生和老师)
课程ID,课程名称,最高分,学生ID,学生姓名,教师ID,教师姓名,最低分,学生ID,学生姓名,教师ID,教师姓名
select tb.f4,tb.f3,tb1.f5,tb.f1,tb.f2,tb.f6,tb.f7,tb2.f5,tb2.f1,tb2.f2,tb2.f6,tb2.f7 from t tb where
f5=(select max(f5) from t where t.f4=tb.f4)
join select f2,f7 from t tb2 where
f5=(select min(f5) from t where t.f4=tb2.f4)
on tb.f4=tb2.f4
先完成一个,想想在做下一个.
3.按成绩从高到低顺序,列印所有学生四门(数学,语文,英语,政治)课程成绩: (就是每个学生的四门课程的成绩单)
学生ID,学生姓名,数学,语文,英语,政治,有效课程数,有效平均分
(注: 有效课程即在 T 表中有该学生的成绩记录,如不明白可不列印"有效课程数"和"有效平均分")
select tb1.f1,tb1.f2,count(tb1.f5) as scores,sum(tb1.f5) as scoresum, avg(tb1.f5) AS average,
tb2.f5,tb3.f5,tb4.f5,tb5.f5
from t as tb1
left join t as tb2
on tb1.f0=tb2.f0 and tb2.f3=k4
left join t as tb3
on tb1.f0=tb3.f0 and tb3.f3=k3
left join t as tb4
on tb1.f0=tb4.f0 and tb4.f3=k2
left join t as tb5
on tb1.f0=tb5.f0 and tb5.f3=k1
grout by tb1.f2 order by tb1.scoresum desc
4.按各科不及格率的百分数从低到高和平均成绩从高到低顺序,统计并列印各科平均成绩和不及格率的百分数(用"N行"表示): (就是分析哪门课程难)
课程ID,课程名称,平均成绩,及格百分数
select f3,f4, (select count(f1) from t where t.f4=tb.f4 and f5<60)/(select count(f1) from t where t.f4=tb.f4) as failper,((select sum(f5) from t where t.f4=tb.f4)/ (select count(f5) from t where t.f4=tb.f4)) as averagescore
from t tb order by failper asc, as averagescore desc
Top
回复人: wenhao676(zzc) ( ) 信誉:93 2003-3-21 11:01:50 得分:0
/****** Object: Stored Procedure dbo.dt_checkoutobject Script Date: 2003-3-12 9:25:26 ******/
create proc dbo.dt_checkoutobject
@chObjectType char(4),
@vchObjectName varchar(255),
@vchComment varchar(255),
@vchLoginName varchar(255),
@vchPassword varchar(255),
@iVCSFlags int = 0,
@iActionFlag int = 0/* 0 => Checkout, 1 => GetLatest, 2 => UndoCheckOut */
as
set nocount on
declare @iReturn int
declare @iObjectId int
select @iObjectId =0
declare @VSSGUID varchar(100)
select @VSSGUID = 'SQLVersionControl.VCS_SQL'
declare @iReturnValue int
select @iReturnValue = 0
declare @vchTempText varchar(255)
/* this is for our strings */
declare @iStreamObjectId int
select @iStreamObjectId = 0
declare @iPropertyObjectId int
select @iPropertyObjectId = (select objectid from dbo.dtproperties where property = 'VCSProjectID')
declare @vchProjectName varchar(255)
declare @vchSourceSafeINI varchar(255)
declare @vchServerName varchar(255)
declare @vchDatabaseName varchar(255)
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSProject', @vchProjectName OUT
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSourceSafeINI', @vchSourceSafeINI OUT
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLServer', @vchServerName OUT
exec dbo.dt_getpropertiesbyid_vcs @iPropertyObjectId, 'VCSSQLDatabase', @vchDatabaseName OUT
if @chObjectType = 'PROC'
begin
/* Procedure Can have up to three streams
Drop Stream, Create Stream, GRANT stream */
exec @iReturn = sp_OACreate @VSSGUID, @iObjectId OUT
if @iReturn <> 0 GOTO E_OAError
exec @iReturn = sp_OAMethod @iObjectId,
'CheckOut_StoredProcedure',
NULL,
@sProjectName = @vchProjectName,
@sSourceSafeINI = @vchSourceSafeINI,
@sObjectName = @vchObjectName,
@sServerName = @vchServerName,
@sDatabaseName = @vchDatabaseName,
@sComment = @vchComment,
@sLoginName = @vchLoginName,
@sPassword = @vchPassword,
@iVCSFlags = @iVCSFlags,
@iActionFlag = @iActionFlag
if @iReturn <> 0 GOTO E_OAError
exec @iReturn = sp_OAGetProperty @iObjectId, 'GetStreamObject', @iStreamObjectId OUT
if @iReturn <> 0 GOTO E_OAError
create table #commenttext (id int identity, sourcecode varchar(255))
select @vchTempText = 'STUB'
while @vchTempText IS NOT NULL
begin
exec @iReturn = sp_OAMethod @iStreamObjectId, 'GetStream', @iReturnValue OUT, @vchTempText OUT
if @iReturn <> 0 GOTO E_OAError
if (@vchTempText IS NOT NULL) insert into #commenttext (sourcecode) select @vchTempText
end
select 'VCS'=sourcecode from #commenttext order by id
select 'SQL'=text from syscomments where id = object_id(@vchObjectName) order by colid
end
CleanUp:
return
E_OAError:
exec dbo.dt_displayoaerror @iObjectId, @iReturn
GOTO CleanUp
GO
看的我直晕~!
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-21 11:17:12 得分:0
1.如果 T 表还有一字段 F0 数据类型为自动增量整型(唯一,不会重复),
而且 T 表中含有除 F0 字段外,请删除其它字段完全相同的重复多余的脏记录数据:
delete from t where f0 in(select max(f0) from t group by f1,f2,f3,f4,f5,f6,f7 having count(f0)>1)
上面这个sql有问题
正确的如下
DELETE Legal_Dispute_Lawyer WHERE Lawyer_Record_No IN(SELECT Lawyer_Record_No FROM Legal_Dispute_Lawyer LDL WHERE Lawyer_Record_No(SELECT TOP 1 Lawyer_Record_No FROM Legal_Dispute_Lawyer WHERE LD=LDL.LD AND Name=LDL.Name AND Email=LDL.Email AND Phone_No=LDL.Phone_No AND Fax_No=LDL.Fax_No))
消除Legal_Dispute_Lawyer 表中除Lawyer_Record_No(自增字段)外其余数据完全相同的记录.
表结构如下
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Legal_Dispute_Lawyer]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Legal_Dispute_Lawyer]
GO
CREATE TABLE [dbo].[Legal_Dispute_Lawyer] (
[Lawyer_Record_No] [int] IDENTITY (1, 1) NOT NULL ,
[LD] [int] NOT NULL ,
[Name] [int] NOT NULL ,
[Phone_No] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[Fax_No] [varchar] (255) COLLATE Chinese_PRC_CI_AS NULL ,
[varchar] (255) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
以这种方式还可以实现组内消除重复值..
Top
回复人: yonghengdizhen(刹那←→永恒) ( ) 信誉:112 2003-3-21 11:19:48 得分:0
DELETE Legal_Dispute_Lawyer WHERE Lawyer_Record_No IN(SELECT Lawyer_Record_No FROM Legal_Dispute_Lawyer LDL WHERE Lawyer_Record_No<>(SELECT TOP 1 Lawyer_Record_No FROM Legal_Dispute_Lawyer WHERE LD=LDL.LD AND Name=LDL.Name AND Email=LDL.Email AND Phone_No=LDL.Phone_No AND Fax_No=LDL.Fax_No))
Top
回复人: ylecho(~猫猫) ( ) 信誉:100 2003-3-21 13:58:37 得分:0
select * from table;
xixi
Top
回复人: SeaSee(纵横四海) ( ) 信誉:100 2003-3-21 14:10:19 得分:0
androi中提到了布尔数组;
布尔数组默认的是false, 并且只会打印false或者是true
布尔数组的例子; 根据字符数组创建布尔数组
char[] c = {'p','u','b','l','i','c'};
//根据字符数组的长度创建布尔数组的个数
boolean[] b = new bool
文章摘自:http://blog.csdn.net/yangwawa19870921/article/details/7553181
在编写HQL时,可能会出现这种代码:
select a.name,b.age from TableA a left join TableB b on a.id=b.id
如果这是HQL,那么这段代码就是错误的,因为HQL不支持
1. 简单的for循环
public static void main(String[] args) {
for (int i = 1, y = i + 10; i < 5 && y < 12; i++, y = i * 2) {
System.err.println("i=" + i + " y="
异常信息本地化
Spring Security支持将展现给终端用户看的异常信息本地化,这些信息包括认证失败、访问被拒绝等。而对于展现给开发者看的异常信息和日志信息(如配置错误)则是不能够进行本地化的,它们是以英文硬编码在Spring Security的代码中的。在Spring-Security-core-x
近来工作中遇到这样的两个需求
1. 给个Date对象,找出该时间所在月的第一天和最后一天
2. 给个Date对象,找出该时间所在周的第一天和最后一天
需求1中的找月第一天很简单,我记得api中有setDate方法可以使用
使用setDate方法前,先看看getDate
var date = new Date();
console.log(date);
// Sat J
MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了。本篇仅记录批量update操作的
sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了。 注意:下列批量更新语句都是作为一个事务整体执行,要不全部成功,要不全部回滚。
MSSQL的SQL语句
WITH R AS(
SELECT 'John' as name, 18 as