‘壹’ sql 计算a表连续两个月的数据和 1月份直接出不计算
什么数据库啊?
还有ny的类型是什么啊?
--sqlserver写法,表名test,自己换一下名,我是把ny当做字符来处理的
select * from test where ny=(select min(ny) from test)
union all
select b.ny,case when substring(b.ny,5,2)<>'01' then a.sal+b.sal else b.sal end
from test a,test b where
datediff(month,cast(cast(a.ny+'01' as varchar(8)) as datetime),cast(cast(b.ny+'01' as varchar(8)) as datetime))=1;
--oracle写法
select * from test where ny=(select min(ny) from test)
union all
select b.ny,case when substr(b.ny,5,2)<>'01' then a.sal+b.sal else b.sal end
from test a,test b where
months_between(to_date(b.ny,'yyyymm'),to_date(a.ny,'yyyymm'))=1;
‘贰’ sql查询连续出勤记录
楼主的笔误很多啊,看看是不是这个效果。
declare @tab table (dt datetime , ur varchar(12))
insert into @tab
select '2007-01-02','2014' union all
select '2007-01-04','2014' union all
select '2007-01-08','2014' union all
select '2007-01-09','2014' union all
select '2007-01-10','2014' union all
select '2007-01-11','2014' union all
select '2007-01-13','2014' union all
select '2007-01-14','2014' union all
select '2007-01-15','2014' union all
select '2007-01-17','2014' union all
select '2007-01-18','2014' union all
select '2007-01-20','2014' union all
select '2007-01-21','2014' union all
select '2007-01-22','2014' union all
select '2007-01-23','2014' union all
select '2007-01-25','2014' union all
select '2007-01-29','2014'
select *
from @tab a
where exists(select * from @tab where datediff(dd,dt,a.dt) in (1,-1))
order by 1
‘叁’ 如何写SQL从一个表里,获取最近两个月的数据记录
只要判断表中的时间字段跟当前日期,对比两者的秒数,绝对值最少的就是离当前最近的那条记录了,语句如下: select top 1 * from 表 order by abs(datediff(ss,时间,getdate()))
‘肆’ oracle 求SQL语句 求连续三个月都有记录的用户 求中间间隔一个月的用户:如有2008-03-01,无2008-04 -01,
不知道你的三个字段叫什么名字,假设第一个叫userid, 第三个叫tstdt
使用以下语句可以得到连续三个月有记录的用户
-------先构建一个表,选userid和月份,然后利用这个表(tab),选择存在连续比当前行月份大于1大于2的数据,即连续三个月的数据
with tab as (select userid, to_number(to_char(tstdt, 'yyyy')) as tyear,to_number(to_char(tstdt, 'mm')) as tmonth from test1 order by userid, tstdt)
select userid
from tab t
where exists (select 1
from tab t1
where ((t1.tyear - t.tyear = 1 and t1.tmonth = 1 and t.tmonth = 12) or (t1.tyear = t.tyear and t1.tmonth - t.tmonth = 1))
and t1.userid = t.userid
and exists (select 1
from tab t2
where ((t2.tyear - t1.tyear = 1 and t2.tmonth = 1 and t1.tmonth = 12) or (t2.tyear = t1.tyear and t2.tmonth - t1.tmonth = 1))
and t2.userid = t1.userid))
第二个,查询间隔一个月的用户
with tab as (select userid, to_number(to_char(tstdt, 'yyyy')) as tyear,to_number(to_char(tstdt, 'mm')) as tmonth from test1 order by userid, tstdt)
select distinct * from tab t
where not exists (select 1
from tab t1
where t.userid = t1.userid
and ((t1.tyear = t.tyear and t1.tmonth - t.tmonth = 1) or (t1.tyear - t.tyear = 1 and t1.tmonth = 1 and t.tmonth = 12)))
and exists (select 1 from tab t2 where ((t2.tyear > t.tyear) or (t2.tyear = t.tyear and t2.tmonth > t.tmonth)) and t2.userid = t.userid)
不存在大一个月的数据同时又存在大超过一个月的数据,最终的结果就是了。
‘伍’ 如何写SQL从一个表里,获取最近两个月的数据记录
如何写SQL从一个表里,获取最近两个月的数据记录
一:如果要插入目标表不存在:
select * into 目标表 from 表 where ...
二:如果要插入目标表已经存在:
insert into 目的表 select * from 表 where 条件
三:如果是跨数据库操作的话: 怎么把A数据库的atable表所查询的东西,全部插入到B 数据库的btable表中
select * into B.btable from A.atable where ...
同样,如果是跨服务器的,也是可以的。
‘陆’ sql 怎样查询两个月的部分数据,得出结果为当月的数据
我没有sql server数据库,无法测试你可以试试下面的写法:select sum(t3.fieldName) from (select top 3 fieldName from TableName) t3应该可以的。
‘柒’ Sql查询连续月份的值
先 Group By 出来每个用户的登陆月份,使用 ROW_NUMBER() 函数给每个用户一个排序的序号。然后用用户的登陆月份与序号相减,如果月份连续,则相减后的数就是一样的。这样就能找到连续的月份了。