id 时间 甲线石灰石 乙线石灰 石脱硫电量
1 k 06:30:30 4545 21 23
2 j 07:30:00 6547 312 1312
3 k 08:21:00 678 7878 78898
4 j 08:55:00 789 77878 345355
这是我的 表结构 和数据 k 开头的 就是 开始时间 j开头的就是结束时间 根据这个表结构 怎么能得到 比如:id为1,2这两条记录 要显示出来 开始时间 结束时间 6547-4545的值 312-21的值 1312-23
一行一行显示
1 k 06:30:30 4545 21 23
2 j 07:30:00 6547 312 1312
3 k 08:21:00 678 7878 78898
4 j 08:55:00 789 77878 345355
这是我的 表结构 和数据 k 开头的 就是 开始时间 j开头的就是结束时间 根据这个表结构 怎么能得到 比如:id为1,2这两条记录 要显示出来 开始时间 结束时间 6547-4545的值 312-21的值 1312-23
一行一行显示
create table tb(id int,类型 varchar(10),时间 varchar(10),甲线石灰石 int,乙线石灰石 int,脱硫电量 int)
insert into tb select 1,'k','06:30:30',4545,21,23
insert into tb select 2,'j','07:30:00',6547,312,1312
insert into tb select 3,'k','08:21:00',678,7878,78898
insert into tb select 4,'j','08:55:00',789,77878,345355
go
select a.时间 开始时间,b.时间 结束时间,
b.甲线石灰石-a.甲线石灰石 as 甲线石灰石,
b.乙线石灰石-a.乙线石灰石 as 乙线石灰石,
b.脱硫电量-a.脱硫电量 as 脱硫电量
from tb a inner join tb b on a.id=b.id-1 and a.类型='k' and b.类型='j'
/*
开始时间 结束时间 甲线石灰石 乙线石灰石 脱硫电量
---------- ---------- ----------- ----------- -----------
06:30:30 07:30:00 2002 291 1289
08:21:00 08:55:00 111 70000 266457(2 行受影响)*/
go
drop table tb
否则要row_number函数先生成一个顺序号再作同样处理.
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,时间 varchar(20),甲线石灰石 int,乙线石灰石 int,脱硫电量 int)
insert into [tb]
select 1,'k 06:30:30',4545,21,23 union all
select 2,'j 07:30:00',6547,312,1312 union all
select 3,'k 08:21:00',678,7878,78898 union all
select 4,'j 08:55:00',789,77878,345355--开始查询
;with cte as(
select rid=ROW_NUMBER() over (order by id),type=LEFT(时间,1),time=RIGHT(时间,8),甲线石灰石,乙线石灰石,脱硫电量 from tb
)
select 开始时间=a.time,
结束时间=b.time,
甲=b.甲线石灰石-a.甲线石灰石,
乙=b.乙线石灰石-a.乙线石灰石,
电量=b.脱硫电量-a.脱硫电量
from cte a join cte b on(a.rid=b.rid-1)
where a.type='k' and b.type='j'--结束查询
drop table [tb]/*
开始时间 结束时间 甲 乙 电量
---------------- ---------------- ----------- ----------- -----------
06:30:30 07:30:00 2002 291 1289
08:21:00 08:55:00 111 70000 266457(2 行受影响)
)
select 开始时间=a.time,
结束时间=b.time,
甲=b.甲线石灰石-a.甲线石灰石,
乙=b.乙线石灰石-a.乙线石灰石,
电量=b.脱硫电量-a.脱硫电量
from cte a join cte b on(a.rid=b.rid-1)
where a.type='k' and b.type='j'
)
select 开始时间=a.time,
结束时间=b.time,
甲=b.甲线石灰石-a.甲线石灰石,
乙=b.乙线石灰石-a.乙线石灰石,
电量=b.脱硫电量-a.脱硫电量
from cte a join cte b on(a.rid=b.rid-1)
where a.type='k' and b.type='j'
k 06:30:30 4545 21 23
j 07:30:00 6547 312 1312
k 08:21:00 678 7878 78898
j 08:55:00 789 77878 345355
表结构是这样的 要用sql 写出上面的结果 谢谢
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id int,时间 varchar(20),甲线石灰石 int,乙线石灰石 int,脱硫电量 int)
insert into [tb]
select 1,'k 06:30:30',4545,21,23 union all
select 2,'j 07:30:00',6547,312,1312 union all
select 3,'k 08:21:00',678,7878,78898 union all
select 4,'j 08:55:00',789,77878,345355--开始查询
select rid=(select COUNT(*) from tb where RIGHT(时间,8)<=RIGHT(a.时间,8)),
type=LEFT(时间,1),time=RIGHT(时间,8),甲线石灰石,乙线石灰石,脱硫电量
into #temptb from tb a --数据插入临时表#temptb,不明白的话select * from #temptb看看结果select 开始时间=a.time,
结束时间=b.time,
甲=b.甲线石灰石-a.甲线石灰石,
乙=b.乙线石灰石-a.乙线石灰石,
电量=b.脱硫电量-a.脱硫电量
from #temptb a join #temptb b on(a.rid=b.rid-1)
where a.type='k' and b.type='j'--结束查询
drop table [tb],#temptb/*
开始时间 结束时间 甲 乙 电量
---------------- ---------------- ----------- ----------- -----------
06:30:30 07:30:00 2002 291 1289
08:21:00 08:55:00 111 70000 266457(2 行受影响)
这不是用到了吗?
create or replace procedure 存储名
as
begin 你表的查询语句:eg:select * from 表名;
end;
这样就建立了一个存储过程只要调用这个存储过程就可以查询出数据了exec 存储过程名称;
就可以了
if object_id('tb','U') is not null
drop table tb
go
create table tb
(
id int identity(1,1) not null,
时间 varchar(20) not null,
甲线石灰石 varchar(10) not null,
乙线石灰石 varchar(10) not null,
脱硫电量 varchar(10) not null
)
go
insert into tb(时间,甲线石灰石,乙线石灰石,脱硫电量)
select 'k 06:30:30','4545','21','23' union all
select 'j 07:30:00','6547','312','1312' union all
select 'k 08:21:00','678','7878','78898' union all
select 'j 08:55:00','789','77878','345355'
go
select
[开始时间]=a.时间,
[结束时间]=b.时间,
[甲]=a.甲线石灰石+'-'+b.甲线石灰石,
[乙]=a.乙线石灰石+'-'+b.乙线石灰石,
[脱硫电量]=a.脱硫电量+'-'+b.脱硫电量
from tb a inner join tb b on a.id=b.id+1 where a.id%2=0
有多个是什么意思?不会你的k和j不是一一对应的,那对于:k,6:00,......
k,6:20,......
k,6:30,......
j,6:35,......
k,7:00,.....
k,8:00,....
j,8:20,....
或许还有一起出来两个j的,你想要怎么处理呢?
insert into tb select 'k','06:25:00',3254,10,5
insert into tb select 'k','06:30:30',4545,21,23
insert into tb select 'j','07:30:00',6547,312,1312
insert into tb select 'k','08:21:00',678,7878,78898
insert into tb select 'j','08:55:00',789,77878,345355
go
select a.时间 开始时间,b.时间 结束时间,
b.甲线石灰石-a.甲线石灰石 as 甲线石灰石,
b.乙线石灰石-a.乙线石灰石 as 乙线石灰石,
b.脱硫电量-a.脱硫电量 as 脱硫电量
from tb a inner join tb b on a.类型='k' and b.类型='j' and a.时间<b.时间 and not exists(
select 1 from tb where 时间>a.时间 and 时间<b.时间)
/*
开始时间 结束时间 甲线石灰石 乙线石灰石 脱硫电量
---------- ---------- ----------- ----------- -----------
06:30:30 07:30:00 2002 291 1289
08:21:00 08:55:00 111 70000 266457(2 行受影响)*/
go
drop table tb
1 k 06:30:30 4545 21 23
2 j 07:30:00 6547 312 1312
3 k 08:21:00 678 7878 78898
4 j 08:55:00 789 77878 345355
select (id+1)/2,开始时间=Max(case when 1%2=1 then 时间 else 0 end)
开始时间=Max(case when 1%2=0 then 时间 else 0 end)
from 表名
gropu by (id+1)/2后面的类似了。不写了。