乙個基本SQL學習

2021-04-22 10:49:45 字數 3687 閱讀 1428

create table te***

(sname char(1),

sdate varchar2(10),

value number

);insert into te*** (sname, sdate, value)

values ('a', '2008-10-1', 100);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-1', 200);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-10', 900);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-11', 900);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-2', 101);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-3', 100);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-5', 200);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-6', 200);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-8', 900);

insert into te*** (sname, sdate, value)

values ('a', '2008-10-9', 900);

insert into te*** (sname, sdate, value)

values ('b', '2008-10-4', 900);

insert into te*** (sname, sdate, value)

values ('b', '2008-10-5', 900);

insert into te*** (sname, sdate, value)

values ('b', '2008-10-6', 900);

insert into te*** (sname, sdate, value)

values ('c', '2008-11-1', 100);

insert into te*** (sname, sdate, value)

values ('d', '2008-12-1', 300);

commit;

select * from te***

sname        sdate        value

a        2008-10-1        100

a        2008-10-1        200

a        2008-10-2        101

a        2008-10-3        100

a        2008-10-5        200

a        2008-10-6        200

a        2008-10-8        900

a        2008-10-9        900

a        2008-10-10        900

a        2008-10-11        900

b        2008-10-4        900

b        2008-10-5        900

b        2008-10-6        900

c        2008-11-1        100

d        2008-12-1        300

執行下面語句

with temp as

(select t.*,

lead(timec,1,null) over(partition by sname order by sdate) nexttime1,

lead(timec,2,null) over(partition by sname order by sdate) nexttime2,

lead(rn1,1,null) over(partition by sname order by sdate) nextrn1,

lead(rn1,2,null) over(partition by sname order by sdate) nextrn2

from

(select sname,sdate,value,prvdate,rn1,sdate - prvdate timec from

(select sname,sdate,value,

lag(sdate,1,null) over(partition by sname order by sdate) prvdate,

row_number() over(order by rownum) rn1

from (select sname,to_date(sdate,'yyyy-mm-dd') sdate,value from te*** order by sname,sdate)

)) t

)select * from

(select t.*,row_number() over(order by rownum) rn

from (select sname,to_date(sdate,'yyyy-mm-dd') sdate,value from te*** order by sname,sdate) t

) where rn in

(select rn1 from temp where nexttime1 = 1 and nexttime2 = 1

union

select nextrn1 from temp where nexttime1 = 1 and nexttime2 = 1

union

select nextrn2 from temp where nexttime1 = 1 and nexttime2 = 1

)sname        sdate                value        rn

a        2008-10-1        200        2

a        2008-10-2        101        3

a        2008-10-3        100        4

a        2008-10-8        900        7

a        2008-10-9        900        8

a        2008-10-10        900        9

a        2008-10-11        900        10

b        2008-10-4        900        11

b        2008-10-5        900        12

b        2008-10-6        900        13

乙個簡單的乙個sql表遍歷

簡單的乙個sql表遍歷 一般我們寫儲存過程或者其他sql語句的時候都會用到迴圈遍歷資料,最常用的兩種就是 1 游標 2 臨時表 while 下面貼出示例 declare minrelogid int 這裡的 minrelogid 一般都是表中的主鍵 select top 1 minrelogid m...

乙個sql的例子

select dbo.userinfo.username,dbo.userinfo.usertruename,dbo.userinfo.useremail,dbo.userinfo.usermobile,dbo.userinfo.usertelephone,dbo.userinfo.usercar,...

乙個複雜的sql

select f.course node info id as nodeid,c.course node name as nodename,c.course node type as nodelevel,c.course code,case when select course node info ...