SQL 将两条数据并成一条 有一表Table1 StarTime,EndTime,state 如:我 22:00 -- 08:00在睡觉,录数据时就要录成, 22:00 -- 24:00 睡觉 00:00 -- 08:00 睡觉 如果我用select COUNT(*) from Table1 where state ='睡觉',Count()出来的结果是2,我要如果把这两条数据并成一条,意思就是Count()出来后结果要为1 要怎么写呢
[解决办法]
SQL code
--自连接--select count(*) from table1 t1 join table1 t2 on t1.endtime=t2.starttime and state='睡觉'--当然24点和0点自己去处理。因为不知道你是字符,还是time [解决办法] count(distinct state) [解决办法]
[解决办法] 这个好像有点复杂 如果不连续就算是两条了对吧 [解决办法]
SQL code
--我也来猜一个;with t as ( select a.* from test a where exists (select 1 from test b where a.EndTime='24:00' and b.StartTime='00:00') )select count(*) from twhere state=N'睡觉' [解决办法]