sql查詢問題,想找出開始時間和結束時間

  • 作者:由 匿名使用者 發表于 農業
  • 2021-09-09

sql查詢問題,想找出開始時間和結束時間 匿名使用者 1級 2014-03-30 回答

—— for M$SQL: 若所有的“未解決”均有“已解決”相一一對應,可以:

declare @bgnTb table (

idx int identity(1,1) primary key,

name varchar(16),

bgntime datetime )

declare @endTb table (

idx int identity(1,1) primary key,

name varchar(16),

endtime datetime )

insert into @bgnTb

select name, stime

from tb

where statue=‘未解決’

order by name, stime

insert into @endTb

select name, stime

from tb

where statue=‘已解決’

order by name, stime

select a。name, bgntime, endtime

, datediff(day, bgntime, endtime) as 持續時間

from @bgnTb a

join @endTb b on a。idx = b。idx for SQLite3 (標準SQL)

$ sqlite3

SQLite version 3。7。13 2012-06-11 02:05:22

Enter “。help” for instructions

Enter SQL statements terminated with a “;”

sqlite> create table tb_log (

。。。>     stime datetime,

。。。>     name  varchar(16),

。。。>     status varchar(8) —— -1: 未解決, 0: 解決中, 1: 已解決

。。。> );

sqlite>

sqlite> insert into tb_log values (‘2014-1-1’, ‘張三’, ‘未解決’);

sqlite> insert into tb_log values (‘2014-1-2’, ‘張三’, ‘解決中’);

sqlite> insert into tb_log values (‘2014-1-3’, ‘張三’, ‘已解決’);

sqlite> insert into tb_log values (‘2014-1-4’, ‘張三’, ‘未解決’);

sqlite> insert into tb_log values (‘2014-1-5’, ‘張三’, ‘解決中’);

sqlite> insert into tb_log values (‘2014-1-6’, ‘張三’, ‘解決中’);

sqlite> insert into tb_log values (‘2014-1-7’, ‘張三’, ‘解決中’);

sqlite> insert into tb_log values (‘2014-1-8’, ‘張三’, ‘已解決’);

sqlite> insert into tb_log values (‘2014-1-6’, ‘李四’, ‘未解決’);

sqlite> insert into tb_log values (‘2014-1-7’, ‘李四’, ‘解決中’);

sqlite> insert into tb_log values (‘2014-1-8’, ‘李四’, ‘解決中’);

sqlite> insert into tb_log values (‘2014-1-9’, ‘李四’, ‘已解決’);

sqlite>

sqlite> create table tb_bgn as

。。。> select name, stime as bgntime

。。。> from tb_log

。。。> where status = ‘未解決’

。。。> order by bgntime, name

。。。> ;

sqlite>

sqlite> create table tb_end as

。。。> select name, stime as endtime

。。。> from tb_log

。。。> where status = ‘已解決’

。。。> order by endtime, name

。。。> ;

sqlite>

sqlite> select a。name, bgntime, endtime

。。。> from tb_bgn a

。。。> join tb_end b on a。name = b。name

。。。>     and a。bgntime  where 1=1

。。。> and not exists (

。。。>     select 1

。。。>     from tb_bgn a0

。。。>     join tb_end b0 on a0。name = b0。name

。。。>         and a0。bgntime      where a。name = a0。name

。。。>     and a。bgntime = a0。bgntime

。。。>     and b。endtime > b0。endtime)

。。。> ;

張三|2014-1-1|2014-1-3

張三|2014-1-4|2014-1-8

李四|2014-1-6|2014-1-9

sqlite>

sql查詢問題,想找出開始時間和結束時間 匿名使用者 1級 2014-03-30 回答

Create Table Tb

stime Datetime,

name Varchar(10),

status Varchar(10)

Insert Into Tb Values(‘2014-1-1’,‘張三’, ‘未解決’)

Insert Into Tb Values(‘2014-1-2’,‘張三’ , ‘解決中’)

Insert Into Tb Values(‘2014-1-3’, ‘張三’ , ‘已解決’)

Insert Into Tb Values(‘2014-1-4’, ‘張三’ , ‘未解決’)

Insert Into Tb Values(‘2014-1-5’, ‘張三’ , ‘解決中’)

Insert Into Tb Values(‘2014-1-6’, ‘張三’ , ‘解決中’)

Insert Into Tb Values(‘2014-1-7’, ‘張三’ , ‘解決中’)

Insert Into Tb Values(‘2014-1-8’, ‘張三’ , ‘已解決’)

Insert Into Tb Values(‘2014-1-6’, ‘李四’ , ‘未解決’)

Insert Into Tb Values(‘2014-1-7’, ‘李四’ , ‘解決中’)

Insert Into Tb Values(‘2014-1-8’, ‘李四’ , ‘解決中’)

Insert Into Tb Values(‘2014-1-9’,‘李四’  ,‘已解決’)

With T

As

Select 開始時間,解決時間,name,datediff(dd,開始時間,解決時間) as 持續時間

From

Select stime as 開始時間,(Select Min(stime) From tb b Where b。name=a。name And b。stime>a。stime And b。status=‘已解決’)

As 解決時間,Name From tb  a Where status=‘未解決’

) C

Select 開始時間,解決時間,name,持續時間,(Select sum(持續時間) From T a Where a。name=b。name And A。開始時間<=B。開始時間) As 累計時間

From T b

——SQL2005或以上版本

搜狗問問

Top