标签:面试题 aaa SQL 练习 year where id select
有表A,结构如下: A: p_ID p_Num s_id 1 10 01 1 12 02 2 8 01 3 11 01 3 8 03 其中:p_ID为产品ID,p_Num为产品库存量,s_id为仓库ID。请用SQL语句实现将上表中的数据合并,合并后的数据为: p_ID s1_id s2_id s3_id 1 10 12 0 2 8 0 0 3 11 0 8 其中:s1_id为仓库1的库存量,s2_id为仓库2的库存量,s3_id为仓库3的库存量。如果该产品在某仓库中无库存量,那么就是0代替。 结果: select p_id , sum(case when s_id=1 then p_num else 0 end) as s1_id ,sum(case when s_id=2 then p_num else 0 end) as s2_id ,sum(case when s_id=3 then p_num else 0 end) as s3_id from myPro group by p_id
题目2:
COURSEID NUMBER COURSENAME VARCHAR2(10) SCORE NUMBER SQL> select * from course_v; COURSEID COURSENAME SCORE ---------- ---------- ---------- 1 java 70 2 oracle 90 3 xml 40 4 jsp 30 5 servlet 80 SQL> select courseid, coursename ,score ,decode(sign(score-60),-1,'fail','pass') as mark from course_v; COURSEID COURSENAME SCORE MARK ---------- ---------- ---------- ---- 1 java 70 pass 2 oracle 90 pass 3 xml 40 fail 4 jsp 30 fail 5 servlet 80 pass
==========
含义解释:(oracle可能)
decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)的理解如下:
if (条件==值1)
then
return(翻译值1)
elsif (条件==值2)
then
return(翻译值2)
---
举例说明:
现定义一table名为output,其中定义两个column分别为monthid(var型)和sale(number型),
若sale值=1000时翻译为D,=2000时翻译为C,=3000时翻译为B,=4000时翻译为A,如是其他值则翻译为Other;
SQL如下:
Select monthid , decode (sale,1000,'D',2000,'C',3000,'B',4000,'A',’Other’) sale from output
特殊情况:
若只与一个值进行比较
Select monthid ,decode(sale, NULL,‘---’,sale) sale from output
另:decode中可使用其他函数,如nvl函数或sign()函数等;
题目3:
11. 说明:两张关联表,删除主表中已经在副表中没有的信息 SQL: Delete from info where not exists (select * from infobz where info.infid=infobz.infid )
题目4:
******************************************************************************* 12.有两个表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); ***************************************************************************
是不是写错了?更新b应该set a.value
其他:
5.面试题:怎么把这样一个表儿 year month amount 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 查成这样一个结果 year m1 m2 m3 m4 1991 1.1 1.2 1.3 1.4 1992 2.1 2.2 2.3 2.4 答案一、 --我的备注:感觉用pivot更好 select year, (select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year ******************************************************************************* 6. 说明:复制表( 只复制结构, 源表名:a新表名:b) SQL: select * into b from a where 1<>1 (where1=1,拷贝表结构和数据内容)
https://www.cnblogs.com/diffrent/p/8854995.html
标签:面试题,aaa,SQL,练习,year,where,id,select 来源: https://www.cnblogs.com/watermarks/p/13092460.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。