今天减昨天查询语句

IGE4G 2014-08-30 11:17:55
表tab1如下:
a b c d t
100 100 50 40 2014-08-30 00:00:000
90 90 30 20 2014-08-29 00:00:000
80 80 20 15 2014-08-28 00:00:000
70 70 15 14 2014-08-27 00:00:000
.............................

我需要查询 a列每日数据减去前一日数据 b+c+d 减去前一日的b+c+d

结果如下:
a e t
10 50 2014-08-30 00:00:000
10 25 2014-08-29 00:00:000
10 16 2014-08-28 00:00:000

求SQL语句

这是按日计算 还需要一个按月计算的 今天的数据 减去前一个月最后一天的数据 若是上一个月就是上月最后一天的数据减去 上上个月最后一天的数据
...全文
292 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
山城忙碌人 2014-09-29
  • 打赏
  • 举报
回复
引用 4 楼 dotnetstudio 的回复:

if object_id('[tb1]') is not null drop table [tb1]
go
create table tb1 (a int,b  int,c int,d int ,t datetime )
insert into tb1 
select  '100 ','  100 ',' 50   ',' 40   ',' 2014-08-30 00:00:000      ' union all
select  '90  ','    90','   30 ','   20 ','   2014-08-29  00:00:000   'union all
select  '80  ','    80','   20 ','   15 ','   2014-08-28 00:00:000    'union all
select  '70  ','    70','   15 ','    14','   2014-08-27 00:00:000    '

create table #tb 
(
   a int,
   e int,
   t datetime,
   rn int 
)

insert into #tb select a,b+c+d as e,t,ROW_NUMBER() over(order by t desc) from tb1

  select a.a-b.a as a, a.e-b.e as e,a.t from #tb a
  inner join (select * from #tb where rn>1) b
  on a.rn+1=b.rn


drop table #tb
这个语句不错,通俗易懂!
KeepSayingNo 2014-08-30
  • 打赏
  • 举报
回复

这是结果
KeepSayingNo 2014-08-30
  • 打赏
  • 举报
回复

if object_id('[tb1]') is not null drop table [tb1]
go
create table tb1 (a int,b  int,c int,d int ,t datetime )
insert into tb1 
select  '100 ','  100 ',' 50   ',' 40   ',' 2014-08-30 00:00:000      ' union all
select  '90  ','    90','   30 ','   20 ','   2014-08-29  00:00:000   'union all
select  '80  ','    80','   20 ','   15 ','   2014-08-28 00:00:000    'union all
select  '70  ','    70','   15 ','    14','   2014-08-27 00:00:000    '

create table #tb 
(
   a int,
   e int,
   t datetime,
   rn int 
)

insert into #tb select a,b+c+d as e,t,ROW_NUMBER() over(order by t desc) from tb1

  select a.a-b.a as a, a.e-b.e as e,a.t from #tb a
  inner join (select * from #tb where rn>1) b
  on a.rn+1=b.rn


drop table #tb
霜寒月冷 2014-08-30
  • 打赏
  • 举报
回复
引用 2 楼 IGE4G 的回复:
感谢 这是按日计算 还需要一个按月计算的 今天的数据 减去前一个月最后一天的数据 若是上一个月就是上月最后一天的数据减去 上上个月最后一天的数据
你先把日期的数据处理下,和上面大同小异 year() 和 month() datediff (month ,t1,t2)这些函数用一下,我就不写了,吃饭去了
IGE4G 2014-08-30
  • 打赏
  • 举报
回复
感谢 这是按日计算 还需要一个按月计算的 今天的数据 减去前一个月最后一天的数据 若是上一个月就是上月最后一天的数据减去 上上个月最后一天的数据
霜寒月冷 2014-08-30
  • 打赏
  • 举报
回复
if object_id('[tb1]') is not null drop table [tb1]
go
create table tb1 (a int,b  int,c int,d int ,t datetime )
insert into tb1 
select  '100 ','  100 ',' 50   ',' 40   ',' 2014-08-30 00:00:000      ' union all
select  '90  ','    90','   30 ','   20 ','   2014-08-29  00:00:000   'union all
select  '80  ','    80','   20 ','   15 ','   2014-08-28 00:00:000    'union all
select  '70  ','    70','   15 ','    14','   2014-08-27 00:00:000    '
go
with cte as
(
select * ,ROW_NUMBER() over (order by t asc) as rn from tb1
)
select  b.a, b.b+b.c+b .d -a.b-a.c-a.d as e ,b.t from cte  a
left join cte b
on a .rn =b.rn-1  where  b.a is not null  and  DATEDIFF (DAY ,a .t ,b .t )=1 order by  b.t desc 

--a	e	t
--100	50	2014-08-30 00:00:00.000
--90	25	2014-08-29 00:00:00.000
--80	16	2014-08-28 00:00:00.000
是这个么?

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧