请用SQL语句实现:
从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。
请注意:
TestDB中有很多科目,都有1-12月份的发生额。
AccID:
科目代码,Occmonth:
发生额月份,DebitOccur:
发生额。
数据库名:
JcyAudit,数据集:
Select*fromTestDB
答:
selecta.*
fromTestDBa
(selectOccmonth,max(DebitOccur)Debit101ccurfromTestDBwhereAccID='101'groupbyOccmonth)b
wherea.Occmonth=b.Occmonthanda.DebitOccur>b.Debit101ccur
************************************************************************************
面试题:
怎么把这样一个表儿
year monthamount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
yearm1 m2 m3 m4
19911.11.21.31.4
19922.12.22.32.4
答案一、
selectyear,
(selectamountfrom aaamwheremonth=1 andm.year=aaa.year)asm1,
(selectamountfrom aaamwheremonth=2 andm.year=aaa.year)asm2,
(selectamountfrom aaamwheremonth=3 andm.year=aaa.year)asm3,
(selectamountfrom aaamwheremonth=4 andm.year=aaa.year)asm4
fromaaa groupbyyear
这个是ORACLE 中做的:
select*from(selectname,yearb1,lead(year)over
(partitionbynameorderbyyear)b2,lead(m,2)over(partitionbynameorderbyyear)b3,rank()over(
partitionbynameorderbyyear)rkfromt)whererk=1;
************************************************************************************
精妙的SQL语句!
精妙SQL语句
作者:
不详发文时间:
2003.05.2910:
55:
05
说明:
复制表(只复制结构,源表名:
a新表名:
b)
SQL:
select*intobfromawhere1<>1
说明:
拷贝表(拷贝数据,源表名:
a目标表名:
b)
SQL:
insertintob(a,b,c)selectd,e,ffromb;
说明:
显示文章、提交人和最后回复时间
SQL:
selecta.title,a.username,b.adddatefromtablea,(selectmax(adddate)adddatefromtablewheretable.title=a.title)b
说明:
外连接查询(表名1:
a表名2:
b)
SQL:
selecta.a,a.b,a.c,b.c,b.d,b.ffromaLEFTOUTJOINbONa.a=b.c
说明:
日程安排提前五分钟提醒
SQL:
select*from日程安排wheredatediff('minute',f开始时间,getdate())>5
说明:
两张关联表,删除主表中已经在副表中没有的信息
SQL:
deletefrominfowherenotexists(select*frominfobzwhereinfo.infid=infobz.infid)
说明:
--
SQL:
SELECTA.NUM,A.NAME,B.UPD_DATE,B.PREV_UPD_DATE
FROMTABLE1,
(SELECTX.NUM,X.UPD_DATE,Y.UPD_DATEPREV_UPD_DATE
FROM(SELECTNUM,UPD_DATE,INBOUND_QTY,STOCK_ONHAND
FROMTABLE2
WHERETO_CHAR(UPD_DATE,'YYYY/MM')=TO_CHAR(SYSDATE,'YYYY/MM'))X,
(SELECTNUM,UPD_DATE,STOCK_ONHAND
FROMTABLE2
WHERETO_CHAR(UPD_DATE,'YYYY/MM')=
TO_CHAR(TO_DATE(TO_CHAR(SYSDATE,'YYYY/MM')¦¦'/01','YYYY/MM/DD')-1,'YYYY/MM'))Y,
WHEREX.NUM=Y.NUM(+)
ANDX.INBOUND_QTY+NVL(Y.STOCK_ONHAND,0)<>X.STOCK_ONHAND)B
WHEREA.NUM=B.NUM
说明:
--
SQL:
select*fromstudentinfowherenotexists(select*fromstudentwherestudentinfo.id=student.id)and系名称='"&strdepartmentname&"'and专业名称='"&strprofessionname&"'orderby性别,生源地,高考总成绩
说明:
从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)
SQL:
SELECTa.userper,a.tel,a.standfee,TO_CHAR(a.telfeedate,'yyyy')AStelyear,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'01',a.factration))ASJAN,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'02',a.factration))ASFRI,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'03',a.factration))ASMAR,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'04',a.factration))ASAPR,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'05',a.factration))ASMAY,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'06',a.factration))ASJUE,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'07',a.factration))ASJUL,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'08',a.factration))ASAGU,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'09',a.factration))ASSEP,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'10',a.factration))ASOCT,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'11',a.factration))ASNOV,
SUM(decode(TO_CHAR(a.telfeedate,'mm'),'12',a.factration))ASDEC
FROM(SELECTa.userper,a.tel,a.standfee,b.telfeedate,b.factration
FROMTELFEESTANDa,TELFEEb
WHEREa.tel=b.telfax)a
GROUPBYa.userper,a.tel,a.standfee,TO_CHAR(a.telfeedate,'yyyy')
说明:
四表联查问题:
SQL:
select*fromaleftinnerjoinbona.a=b.brightinnerjoincona.a=c.cinnerjoindona.a=d.dwhere.....
说明:
得到表中最小的未使用的ID号
SQL:
SELECT(CASEWHENEXISTS(SELECT*FROMHandlebWHEREb.HandleID=1)THENMIN(HandleID)+1ELSE1END)asHandleID
FROMHandle
WHERENOTHandleIDIN(SELECTa.HandleID-1FROMHandlea)
*******************************************************************************
有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value换为A中对应的value
这道题的SQL语句怎么写?
update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);
***************************************************************************
高级sql面试题
原表:
courseidcoursenamescore
-------------------------------------
1java70
2oracle90
3xml40
4jsp30
5servlet80
-------------------------------------
为了便于阅读,查询此表后的结果显式如下(及格分数为60):
courseidcoursenamescoremark
---------------------------------------------------
1java70pass
2oracle90pass
3xml40fail
4jsp30fail
5servlet80pass
---------------------------------------------------
写出此查询语句
没有装ORACLE,没试过
selectcourseid,coursename,score,decode(sign(score-60),-1,'fail','pass')asmarkfromcourse
完全正确
SQL>desccourse_v
NameNull?
Type
-----------------------------------------------------------------------------
COURSEIDNUMBER
COURSENAMEVARCHAR2(10)
SCORENUMBER
SQL>select*fromcourse_v;
COURSEIDCOURSENAMESCORE
------------------------------
1java70
2oracle90
3xml40
4jsp30
5servlet80
SQL>selectcourseid,coursename,score,decode(sign(score-60),-1,'fail','pass')asmarkfromcourse_v;
COURSEIDCOURSENAMESCOREMARK
----------------------------------
1java70pass
2oracle90pass
3xml40fail
4jsp30fail
5servlet80pass
*******************************************************************************
原表:
idproidproname
11M
12F
21N
22G
31B
32A
查询后的表:
idpro1pro2
1MF
2NG
3BA
写出查询语句
解决方案
sql求解
表a
列a1a2
记录1a
1b
2x
2y
2z
用select能选成以下结果吗?
1ab
2xyz
使用pl/sql代码实现,但要求你组合后的长度不能超出oraclevarchar2长度的限制。
下面是一个例子
createorreplacetypestrings_tableistableofvarchar2(20);
/
createorreplacefunctionmerge(pvinstrings_table)returnvarchar2
is
lsvarchar2(4000);
begin
foriin1..pv.countloop
ls:
=ls||pv(i);
endloop;
returnls;
end;
/
createtablet(idnumber,namevarchar2(10));
insertintotvalues(1,'Joan');
insertintotvalues(1,'Jack');
insertintotvalues(1,'Tom');
insertintotvalues(2,'Rose');
insertintotvalues(2,'Jenny');
columnnamesformata80;
selectt0.id,merge(cast(multiset(selectnamefromtwheret.id=t0.id)asstrings_table))names
from(selectdistinctidfromt)t0;
droptypestrings_table;
dropfunctionmerge;
droptablet;
用sql:
Wellifyouhaveathoreticalmaximum,whichIwouldassumeyouwouldgiventhelegibilityoflistinghundredsofemployeesinthewayyoudescribethenyes.ButtheSQLneedstousetheLAGfunctionforeachemployee,henceahundredempsahundredLAGs,sokindofbulky.
Thisexampleusesamaxof6,andwouldneedmorecutnpastingtodomorethanthat.
SQL>selectdeptno,dname,emps
2from(
3selectd.deptno,d.dname,rtrim(e.ename||','||
4lead(e.ename,1)over(partitionbyd.deptno
5orderbye.ename)||','||
6lead(e.ename,2)over(partitionbyd.deptno
7orderbye.ename)||','||
8lead(e.ename,3)over(partitionbyd.deptno
9orderbye.ename)||','||
10lead(e.ename,4)over(partitionbyd.deptno
11orderbye.ename)||','||
12lead(e.ename,5)over(partitionbyd.deptno
13orderbye.ename),',')emps,
14row_number()over(partitionbyd.deptno
15orderbye.ename)x
16fromempe,deptd
17whered.deptno=e.deptno
18)
19wherex=1
20/
DEPTNODNAMEEMPS
------------------------------------------------------------
10ACCOUNTINGCLARK,KING,MILLER
20RESEARCHADAMS,FORD,JONES,ROONEY,SCOTT,SMITH
30SALESALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
also
先createfunctionget_a2;
createorreplacefunctionget_a2(tmp_a1number)
returnvarchar2
is
Col_a2varchar2(4000);
begin
Col_a2:
='';
forcurin(selecta2fromunite_awherea1=tmp_a1)
loop
Col_a2=Col_a2||cur.a2;
endloop;
returnCol_a2;
endget_a2;
selectdistincta1,get_a2(a1)fromunite_a
1ABC
2EFG
3KMN
*******************************************************************************
一个SQL面试题
去年应聘一个职位未果,其间被考了一个看似简单的题,但我没有找到好的大案.
不知各位大虾有无好的解法?
题为:
有两个表,t1,t2,
Tablet1:
SELLER|NON_SELLER
----------
AB
AC
AD
BA
BC
BD
CA
CB
CD
DA
DB
DC
Tablet2:
SELLER|COUPON|BAL
-----------------------
A9100
B9200
C9300
D9400
A9.5100
B9.520
A1080
要求用SELECT语句列出如下结果:
------如A的SUM(BAL)为B,C,D的和,B的