當前位置:首頁 » 編程語言 » sql判斷連續兩個月有記錄
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql判斷連續兩個月有記錄

發布時間: 2023-01-04 08:44:05

『壹』 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() 函數給每個用戶一個排序的序號。然後用用戶的登陸月份與序號相減,如果月份連續,則相減後的數就是一樣的。這樣就能找到連續的月份了。