1. 程式人生 > >Orcal數據庫相關查詢

Orcal數據庫相關查詢

說明 date 計算 index ray 數據庫 pid art spa

ICPNO NOT NULL VARCHAR2(6)

SERVICE_ID NOT NULL VARCHAR2(10)

STAT_MIN NOT NULL DATETIME

MT_RECV_OK NOT NULL NUMBER(10)

請寫出一條SQL語句同時滿足以下3個條件: 計算MT_RECV_OK的總和(顯示為total),並以此對結果集進行升序排序 以ICPNO和SERVICE_ID兩個字段進行分組 所得出數據的STAT_MIN必須為大於2003年1月1號0時0分並且小於2003 年2月1號0時0分 期望的輸出結果如下: ICPNO SERVICE_ID TOTAL ———— —————— ———————————— 901001 7700 271965 901001 7800 3857795 答:

select ICPNO,SERVICE_ID,sum(MT_RECV_OK) TOTAL from ismg_icp_flow where STAT_MIN between to_date(‘2003-1-1‘,‘yyyy-mm-dd‘) and to_date(‘2003-2-1‘,‘yyyy-mm-dd‘) group by ICPNO,SERVICE_ID order by TOTAL;

Student(S#,SN,SD)學生表 Course(C#,CN,T#)課程表 SC(S#,C#,score)成績表 1.查詢選了課程‘稅收’的學生的學號和名字 答:

select SN,SD from Student where S# in( select S# from Course C , SC where C.C#=SC.C# and CN= ’稅收基礎’);

2.查詢選課數量大於5的學生的學號和名字 答:

select SN,SD from Student where S# in ( select S# from SC group by S# having count(distinct C#) > 5); )

3.建立一個學生表students,包括name,age,head_teacher,id,score(姓名,年齡,班主任,學號,成績) Create table students ( Id number(9) not null primary key, Name varchar2(40) not null, Age int check(age between 0 and 100), Head_teacher vachar2(40), Score float );

4.對上表插入一條記錄,姓名:張三,年齡:18,班主任:李四,學號:22 Insert into student(id,name,age,head_teacher) values(‘22’,’張三’,’18’,’李四’);

5.對上表中的age+name創建一個索引,並說明它的作用和使用方法 Create index student_index on students(age,name);

A(id ,name,regdate) B(id,groupid) C(id,name2) 寫出下面的SQL語句

A)統計A表中每個月註冊用戶數 select count(*),to_char(regdate,‘yyyymm‘) from A group by to_char(regdate,‘yyyymm‘);

B)統計A表中有姓名相同的用戶數 select count(*) from (select name from A group by name having count(*) >1);

C)如果表A中有姓名相同的用戶,把相同的查出,寫入表C中 nsert into C(name2) select name from A group by name having count(*) >1;

D)A中ID有多個相同的數據,A中姓名相同的ID只保留註冊時間最大的數據 delete from E where e.regdate < (select max(regdate) from a X where E.id = X.id)

獲取系統當前時間

select to_char(sysdate, ‘yyyy-MM-dd HH24:mi:ss‘) from dual;

分頁查詢

select *
from (
select t.*, rownum rn
from (
select p.xm_id,p.is_std,p.param_id,p.param_name,p.beizhu,p.std_jg,p.condition_id,
p.condition,p.num_type,p.department,p.customer_id,x.custom_xm_name,p.unit,p.measure_unit
from
jc_cw_price_fssd
p,jc_xtcs_xm x
<where>
p.customer_id = #{customer_id} and p.customer_id = x.customer_id
and p.plan_id = #{plan_id}
<if test="custom_xm_name != null and custom_xm_name != ‘‘ ">
and x.custom_xm_name like ‘%‘||#{custom_xm_name}||‘%‘
</if>
<if test="param_name != null and param_name != ‘‘">
and p.param_name like ‘%‘||#{param_name}||‘%‘
</if>
<if test="array != null">
and p.xm_id in
<foreach collection="array" item="xm_id" open="(" separator="," close=")">
#{xm_id}
</foreach>
</if>
and p.xm_id = x.xm_id
</where>
<if test="param_name_order == null and param_name_order == ‘‘">
order by x.custom_xm_name,p.param_name,p.condition_id asc
</if>
<if test="param_name_order != null and param_name_order != ‘‘">
order by x.custom_xm_name,${param_name_order},p.condition_id asc
</if>
) t where rownum &lt;=#{end}
)where rn &gt;=#{start}

Orcal數據庫相關查詢