您的位置:首页 > 大数据

五大数据库常用SQL比较(三)

2012-06-05 16:10 363 查看
四十九、生成累计和

db2/oracle

select ename,sal,sum(sal) over (order by sal,empno) as running_total from emp order by 2

mysql/postgresql/sqlserver

select e.ename,e.sal,(select sum(d.sal) from emp d where d.empno< =e.empno) as running_total from emp e order by 3

五十、生成累乘积

db2/oracle

select empno,ename,sal,exp(sum(ln(sal)) over (order by sal,empno)) as running_prod from emp where deptno=10

select empno,ename,sal,tmp as running_prod from (select empno,ename,-sal as sal from emp where deptno=10) model dimension by(row_number() over(order by sal desc) rn) measures(sal,0 tmp,empno,ename) rules(tmp[any]=case when sal[cv()-1] is null then sal[cv()]
else tmp[cv()-1]*sal[cv()] end )

mysql/postgresql/sqlserver

select e.empno,e.ename,e.sal,(select exp(sum(ln(d.sal))) from emp d where d.empno<=e.empno and e.deptno=d.deptno) as running_prod from emp e where e.deptno=10

五十一、生成累计差

db2/oracle

select ename,sal,sum(case when rn=1 then sal else -sal end) over(order by sal,empno) as

running_diff from (select empno,ename,sal,row_number() over(order by sal,empno) as rn from

emp where deptno=10) x

mysql/postgresql/sqlserver

select a.empno,a.ename,a.sal,(select case when a.empno=min(b.empno) then sum(b.sal) else

sum(-b.sal) end from emp b where b.empno<=a.empno and b.deptno=a.deptno ) as rnk from emp a

where a.deptno=10

五十二、计算模式

db2/sqlserver

select sal from (select sal,dense_rank() over(order by cnt desc) as rnk from (select

sal,count(*) as cnt from emp where deptno=20 group by sal) x) y where rnk=1

oracle

select max(sal) keep(dense_rank first order by cnt desc) sal from (select sal,count(*) cnt

from emp where deptno=20 group by sal)

mysql/postgresql

select sal from emp where deptno=20 group by sal having count(*) >=all(select count(*) from

emp where deptno=20 group by sal)

五十三、计算中间值

db2

select avg(sal) from (select sal,count(*) over() total cast(count(*) over() as desimal)/2

mid,ceil(cast(count(*) over as decimal)/2) next,row_number() over (order by sal) rn from emp

where deptno=20) x where (mod(total,2)=0 and rn in(mid,mid+1)) or (mod(total,2)=1 and

rn=next)

mysql/postgresql

select avg(sal) from (select e.sal from emp e,emp d where e.deptno=d.deptno and e.deptno=20

group by e.sal having sum(case when e.sal=d.sal then 1 else 0 end)>=abs(sum(sign(e.sal-

d.sal))))

oracle

select median(sal) from emp where deptno=20

select percentile_cont(0.5) within group(order by sal) from emp where deptno=20

sqlserver

select avg(sal) from (select sal,count(*) over() total,cast(count(*) over() as decimel)/2

mid,ceiling(cast(count(*) over() as decimal)/2) next,row_number() over(order by sal) rn from

emp where deptno=20) x where (total%2=0 and rn in(mid,mid+1)) or(total%2=1 and rn=next)

五十四、求总和的百分比

mysql/postgresql

select (sum(case when deptno=10 then sal end)/sum(sal))*100 as pct from emp

db2/oracle/sqlserver

select distinct (d10/total)*100 as pct from (select deptno,sum(sal) over() total,sum(sal)

over(partition by deptno) d10 from emp) x where deptno=10

五十五、对可空列作聚集

select avg(coalesce(comm,0)) as avg_comm from emp where deptno=30

五十六、计算不包含最大值和最小值的均值

mysql/postgresql

select avg(sal) from emp where sal not in((select min(sal) from emp),(select max(sal) from

emp))

db2/oracle/sqlserver

select avg(sal) from (select sal,min(sal) over() min_sal,max(sal) over() max_sal from emp) x

where sal not in(min_sal,max_sal)

五十七、把字母数字串转换为数值

db2

select cast(replace(translate(’paul123f321′,repeat

(’#',26),’abcdefghijklmnopqrstuvwxyz’),’#',”) as integer) as num from t1

oracle/postgresql

select cast(replace(translate(’paul123f321′,’abcdefghijklmnopqrstuvwxyz’,rpad

(’#',26′#’)),’#',”) as integer ) as num from t1

五十八、更改累计和中的值

db2/oracle

select case when trx=’PY’ then ‘PAYMENT’ else ‘PURCHASE’ end trx_type,amt,sum(case when

trx=’PY’ then -amt else amt end) over (order by id,amt) as balance from V

mysql/postgresql/sqlserver

select case when v1.trx=’PY’ then ‘PAYMENT’ else ‘PURCHASE’ end as trx_type,v1.amt,(select sum(case when v2.trx=’PY’ then -v2.amt else v2.amt end) from V v2 where v2.id< =v1.id) as balance from V v1

五十九、增加或减少日,月,年

db2

select hiredate -5 day as hd_minus_5D,hiredate +5 day as hd_plus_5D,hiredate -5 month as

hd_minus_5M,hiredate +5 month as hd_plus_5M,hiredate -5 year as hd_minus_5Y,hiredate +5 year

as hd_plus_5Y from emp where deptno=10

oracle

select hiredate-5 as hd_minus_5D,hiredate+5 as hd_plus_5D,add_months(hiredate,-5) as

hd_minus_5M,add_months(hiredate,5) as hd_plus_5M,add_months(hiredate,-5*12) as

hd_minus_5Y,add_months(hiredate,5*120 as hd_plus_5Y from emp where deptno=10

postgresql

select hiredate-interval '5 day' as hd_minus_5D,hiredate+interval '5 day' as

hd_plus_5D,hiredate-interval '5 month' as hd_minus_5M,hiredate+interval '5 month' as

hd_plus_5M,hiredate-interval '5 year' as hd_minus_5Y,hiredate+interval '5 year' as

hd_plus_5Y from emp where deptno=10

mysql

select hiredate-interval 5 day as hd_minus_5D,hiredate+interval 5 day as

hd_plus_5D,hiredate-interval 5 month as hd_minus_5M,hiredate+interval 5 month as

hd_plus_5M,hiredate-interval 5 year as hd_minus_5Y,hiredate+interval 5 year as hd_plus_5Y

from emp where deptno=10

select date_add(hiredate,interval -5 day) as hd_minus_5D,date_add(hiredate,interval 5 day)

as hd_plus_5D,date_add(hiredate,interval -5 month) as hd_minus_5M,date_add(hiredate,interval

5 month) as hd_plus_5M,date_add(hiredate,interval -5 year) as hd_minus_5Y,date_add

(hiredate,interval 5 year) as hd_plus_5Y from emp where deptno=10

sqlserver

select dateadd(day,-5,hiredate) as hd_minus_5D,dateadd(day,5,hiredate0 as

hd_plus_5D,dateadd(month,-5,hiredate) as hd_minus_5M,dateadd(month,5,hiredate) as

hd_plus_5M,dateadd(year,-5,hiredate) as hd_minus_5Y,dateadd(year,5,hiredate) as hd_plus_5Y

from emp where deptno=10

六十、计算2个日期之间的天数

db2

select days(ward_hd) - days(allen_hd) from (select hiredate as ward_hd from emp where

ename='WARD') x,(select hiredate as allen_hd from emp where ename='ALLEN') y

oracle/postgresql

select ward_hd - allen_hd from (select hiredate as ward_hd from emp where ename='WARD') x,

(select hiredate as allen_hd from emp where ename='ALLEN') y

mysql/sqlserver

select datediff(day,allen_hd,ward_hd) from select hiredate as ward_hd from emp where ename =

'WARD') x,(select hiredate as allen_hd from emp where ename='ALLEN') y

六十一、计算2个日期之间的工作日天数

db2

select sum(case when dayname(jones_hd+t500.id day -1 day) in ('Saturday','Sunday') then 0

else 1 end) as days from (select max(case when ename='BLAKE' then hiredate end) as

blake_hd,max(case when ename='JONES' then hiredate end) as jones_hd from emp where ename in

('BLAKE','JONES') ) x,t500 where t500.id <=blake_hd-jones_hd+1

mysql

select sum(case when date_format(date_add(jones_hd,interval t500.id-1 DAY),'%a') in

('Sat','Sun') then 0 else 1 end) as days from (select max(case when ename='BLAKE' then

hiredate end) as blake_hd,max(case when ename='JONES' then hiredate end) as jones_hd from

emp where ename in ('BLAKE','JONES') ) x,t500 where t500.id<=datediff(blake_hd,jones_hd)+1

oracle

select sum(case when to_char(jones_hd+t500.id-1,'DY') in ('SAT','SUN') then 0 else 1 end) as

days from ( select max(case when ename='BLAKE' then hiredate end ) as blake_hd,max(case when

ename='JONES' then hiredate end) as jones_hd from emp where ename in ('BLAKE','JONES') )

x,t500 where t500.id<=blake_hd-jones_hd+1

postgresql

select sum(case when trim(to_char(jones_hd+t500.id-1,'DAY')) in ('SATURDAY','SUNDAY') then 0

else 1 end) as days from (select max(case when ename='BLAKE' then hiredate end) as

blake_hd,max(case when ename='JONES' then hiredate end) as jones_hd from emp where ename in

('BLAKE','JONES') ) x,t500 where t500.id<=blake_hd-jones_hd+1

sqlserver

select sum(case when datename(dw,jones_hd+t500.id-1) in ('SATURDAY','SUNDAY') then 0 else 1

end) as days from ( select max(case when ename='BLAKE' then hiredate end) as blake_hd,max

(case when ename='JONES' then hiredate end) as jones_hd from emp where ename in

('BLAKE','JONES') ) x,t500 where t500.id<=datediff(day,jones_hd-blake_hd)+1

六十二、计算2个日期之间的月和年

db2/mysql

select mnth,mnth/12 from (select (year(max_hd)-year(min_hd))*12+(month(max_hd)-month

(min_hd)) as mnth from (select min(hiredate) as min_hd,max(hiredate) as max_hd from emp ) x

) y

oracle

select months_between(max_hd,min_hd),months_between(max_hd,min_hd)/12 from (select min

(hiredate) min_hd,max(hiredate) max_hd from emp ) x

postgresql

select mnth,mnth/12 from ( select extract(year from max_hd) extract(year from min_hd))*12 +

(extract(month from max_hd) extract(month from min_hd)) as mnth from (select min(hiredate)

as min_hd,max(hiredate) as max_hd from emp ) x ) y

sqlserver

select datediff(month,min_hd,max_hd),datediff(month,min_hd,max_hd)/12 from (select min

(hiredate) min_hd,max(hiredate) max_hd from emp ) x

六十三、计算2个日期之间的秒、分、小时

db2

select dy*24 hr,dy*24*60 min,day*24*60*60 sec from (select (days(max(case when ename='WARD'

then hiredate end))-days(max(case when ename='ALLEN' then hiredate end)) ) as dy from emp )

x

mysql/sqlserver

select datediff(day,allen_hd,ward_hd)*24 hr,datediff(day,allen_hd,ward_hd)*24*60

min,datediff(day,allen_hd,ward_hd)*24*60*60 sec from (select max(case when ename='WARD' then

hiredate end) as ward_hd,max(case when ename='ALLEN' then hiredate end) as allen_hd from emp

) x

oracle/postgresql

select dy*24 as hr,dy*24*60 as min,dy*24*60*60 as sec from (select (max(case when

ename='WARD' then hiredate end)-max(case when ename='ALLEN' then hiredate end)) as dy from

emp ) x

六十四、统计一年中的周日的天数

db2

with x (start_date,end_date) as ( select start_date,start_date+1 year end_date from (select

(current_date dayofyear(current_date) day)+1 day as start_date from t1) tmp union all select

start_date+1 day,end_date from x where start_date+1 day (start_date),count(*) from x group by dayname(start_date)

mysql

select date_format(date_add(cast(concat(year(current_date),'-01-01') as date),interval

t500.id-1 day),'%W') day,count(*) from t500 where t500.id<=datediff(cast(concat(year

(current_date)+1,'-01-01') as date),cast(concat(year(current_date),'-01-01') as date)) group

by date(format(date_add(cast(concat(year(current_date),'-01-01') as date),interval t500.id-1

day),'%W')

oracle

with x as (select level lvl from dual connect by level<=(add_months(trunc(sysdate,'y'),12)-

trunc(sysdate,'y'))) select to_char(trunc(sysdate,'y')+lvl-1,'DAY'),count(*) from x group by

to_char(trunc(sysdate,'y')+lvl-1,'DAY')

select to_char(trunc(sysdate,'y')+rownum-1,'DAY'),count(*) from t500 where rownum<=

(add_months(trunc(sysdate,'y'),12)-trunc(sysdate,'y')) group by to_char(trunc(sysdate,'y')

+rownum-1,'DAY')

postgresql

select to_char(cast(date_trunc('year',current_date) as date+gs.id-1,'DAY'),count(*) from

generate_series(1,366) gs(id) where gs.id<=(cast(date_trunc('year',current_date)+interval

'12 month' as date)-cast(date_trunc('year',current_date) as date)) group by to_char(cast

(date_trunc('year',current_date) as date) + gs.id-1,'DAY')

sqlserver

with x (start_date,end_date) as (select start_date,dateadd(year,1,start_date) end_date from

(select cast(cast(year(getdate()) as varchar) +'-01-01' as datetime) start_date from t1 )

tmp union all select dateadd(day,1,start_date),end_date from x where dateadd

(day,1,start_date) datename(dw,start_date) OPTION (MAXRECURSION 366)

六十五、查看2个记录之间的日期不同

db2

select x.*,days(x.next_hd)-days(x.hiredate) diff from (select e.deptno,e.name,e.hiredate,

(select min(d.hiredate) from emp d where d.hiredate>e.hiredate) next_hd from emp e where

e.deptno=10) x

mysql/sqlserver

select x.*,datediff(day,x.hiredate,x.next_hd) diff from (select

e.deptno,e.ename,e.hiredate,(select min(d.hiredate) from emp d where d.hiredate>e.hiredate)

next_hd from emp e where e.deptno=10 ) x

oracle

select ename,hiredate,next_hd,next_hd-hiredate diff from (select deptno,ename,hiredate,lead

(hiredate) over(order by hiredate) next_hd from emp ) where deptno=10

postgresql

select x.*,x.next_hd-x.hiredate as diff from (select e.deptno,e.ename,e.hiredate,(select

min(d.hiredate) from emp d where d.hiredate>e.hiredate) as next_hd from emp e where

e.deptno=10 ) x

六十六、判断这一年是不是闰年

db2

with x (dy,mth) as ( select dy,month(dy) from (select (current_date-dayofyear(current_date)

days+1 days)+1 months as dy from t1) tmp1 union all select dy+1 days,mth from x where month

(dy+1 day)=mth) select max(day(dy) from x

oracle

select to_char(last_day(add_months(trunc(sysdate,’y'),1)),’DD’) from t1

postgresql

select max(to_char(tmp2.dy+x.id,’DD’)) as dy from ( select dy,to_char(dy,’MM’) as mth from

(select cast(cast(date_trunc(’year’,current_date) as date)+interval ‘1 month’ as date) as dy

from t1) tmp1 ) tmp2,generate_series (0,29) x(id) where to_char(tmp2.dy+x.id,’MM’)=tmp2.mth

mysql

select day(last_day(date_add(date_add(date_add(current_date,interval -dayofyear

(current_date) day),interval 1 day),interval 1 month))) dy from t1

sqlserver

with x (dy,mth) as (select dy,month(dy) from (select dateadd(mm,1,(getdate()-datepart

(dy,getdate()))+1) dy from t1) tmp1 union all select dateadd(dd,1,dy),mth from x where

month(dateadd(dd,1,dy))=mth) select max(day(dy)) from x

六十七、计算这一年中有多少天

db2

select days((curr_year+1 year))-days(curr_year) from (select (current_date-dayofyear

(current_date) day+1 day) curr_year from t1 ) x

oracle

select add_months(trunc(sysdate,’y'),12)-trunc(sysdate,’y') from dual

postgresql

select cast((curr_year+interval ‘1 year’) as date) - curr_year from ( select cast

(date_trunc(’year’,current_date) as date) as curr_year from t1 ) x

mysql

select datediff((curr_year+interval 1 year),curr_year) from ( select adddate(current_date,-

dayofyear(current_date)+1) curr_year from t1 ) x

sqlserver

select datediff(d,curr_year,dateadd(yy,1,curr_year)) from ( select dateadd(d,-datepart

(dy,getdate())+1,getdate()) curr_year from t1 ) x

六十八、分解日期—-小时、分、秒、日、月、年

db2

select hour(current_timestamp) hr,minute(current_timestamp) min,second(current_timestamp)

sec,day(current_timestamp) dy,month(current_timestamp) mth,year(current_timestamp) yr from

t1

oracle

select to_number(to_char(sysdate,’hh24′)) hour,to_number(to_char(sysdate,’mi’))

min,to_number(to_char(sysdate,’ss’)) sec,to_number(to_char(sysdate,’dd’)) day,to_number

(to_char(sysdate,’mm’)) mth,to_number(to_char(sysdate,’yyyy’)) year from dual

postgresql

select to_number(to_char(current_timestamp,’hh24′),’99′) as hr,to_number(to_char

(current_timestamp,’mi’),’99′) as min,to_number(to_char(current_timestamp,’ss’),’99′) as

sec,to_number(to_char(current_timestamp,’dd’),’99′) as day,to_number(to_char

(current_timestamp,’mm’),’99′) as mth,to_number(to_char(current_timestamp,’yyyy’),’9999′) as

yr from t1

mysql

select date_format(current_timestamp,’%k’) hr,date_format(current_timestamp,’%i’)

min,date_format(current_timestamp,’%s’) sec,date_format(current_timestamp,’%d’)

dy,date_format(current_timestamp,’%m’) mon,date_format(current_timestamp,’%Y’) yr from t1

sqlserver

select datepart(hour,getdate()) hr,datepart(minute,getdate()) min,datepart(second,getdate())

sec,datepart(day,getdate()) dy,datepart(month,getdate()) mon,datepart(year,getdate()) yr

from t1

六十九、计算一个月的第一天和最后一天

db2

select (current_date-day(current_date) day+1 day) firstday,(current_date+1 month -day

(current_date) day) lastday from t1

oracle

select trunc(sysdate,’mm’) firstday,last_day(sysdate) lastday from dual

postgresql

select firstday,cast(firstday+interval ‘1 month’-interval ‘1 day’ as date) as lastday from (

select cast(date_trunc(’month’,current_date) as date) as firstday from t1) x

mysql

select date_add(current_date,interval -day(current_date)+1 day) firstday,last_day

(current_date) lastday from t1

sqlserver

select dateadd(day,-day(getdate())+1,getdate()) firstday,dateadd(day,-day(getdate(),dateadd

(month,1,getdate())) lastdate from t1

七十、计算出一年中的一周中某一天的日期

db2

with x (dy,yr) as ( select dy,year(dy) yr from (select (current_date-dayofyear(current_date)

days +1 days) as dy from t1) tmp1 union all select dy+1 days,yr from x where year(dy+1 day)

=yr) select dy from x where dayname(dy)=’Friday’

oracle

with x as (select trunc(sysdate,’y')+level-1 dy from dual connect by level< =add_months(trunc(sysdate,'y'),12)-trunc(sysdate,'y')) select * from x where to_char(dy-1,'d') ='5'

postgresql

select cast(date_trunc('year',current_date) as date)+x.id as dy from generate_series( 0,

(select cast(cast(date_trunc('year',current_date) as date) + interval '1 years' as date)-

cast(date_trunc('year',current_date) as date))-1 ) x(id) where to_char(cast(date_trunc

('year',current_date) as date)+x.id,'dy')='fri'

mysql

select dy from (select adddate(x.dy,interval t500.id-1 day) dy from (select dy,year(dy) yr

from (select adddate(adddate(current_date,interval -dayofyear(current_date) day),interval 1

day ) dy from t1) tmp1 ) x,t500 where year(adddate(x.dy,interval t500.id-1 day))=x.yr) tmp2

where dayname(dy)='Friday'

sqlserver

with x (dy,yr) as ( select dy,year(dy) yr from ( select getdate()-datepart(dy,getdate())+1

dy from t1) tmp1 union all select dateadd(dd,1,dy),yr from x where year(dateadd(dd,1,dy))

=yr) select x.dy from x where datename(dw,x.dy)='Friday' option (maxrecursion 400)

七十一、确定这个月中的第一次出现和最后一次出现的特定的周末

db2

with x (dy,mth,is_monday) as ( select dy,month(dy),case when dayname(dy)='Monday' then 1

else 0 end from (select (current_date-day(current_date) day +1 day) dy from t1) tmp1 union

all select (dy +1 day),mth,case when dayname(dy +1 day)='Monday' then 1 else 0 end from x

where month(dy +1 day)=mth) select min(dy) first_month,max(dy) last_monday from x where

is_monday=1

oracle

select next_day(trunc(sysdate,'mm')-1,'星期一') first_monday,next_day(last_day(trunc

(sysdate,'mm'))-7,'星期一') last_monday from dual

postgresql

select first_monday,case to_char(first_monday+28,'mm') when mth then first_monday+28 else

first_monday+21 end as last_monday from (select case sign(cast(to_char(dy,'d') as integer)

-2) when 0 then dy when -1 then dy+abs(cast(to_char(dy,'d') as integer)-2) when 1 then (7-

(cast(to_char(dy,'d') as integer)-2))+dy end as first_monday,mth from (select cast

(date_trunc('month',current_date) as date) as dy,to_char(current_date,'mm') as mth from t1 )x ) y

mysql

select first_monday,case month(adddate(first_monday,28)) when mth then adddate

(first_monday,28) else adddate(first_monday,21) end last_monday from ( select case sign

(dayofweek(dy)-2) when 0 then dy when -1 then adddate(dy,abs(dayofweek(dy)-2)) when 1 then adddate(dy,(7-(dayofweek(dy)-2))) end first_monday,mth from (select adddate(adddate

(current_date,-day(current_date)),1) dy,month(current_date0 mth from t1) x ) y

sqlserver

with x (dy,mth,is_monday) as ( select dy,mth,case when datepart(dw,dy)=2 then 1 else 0 end

from (select dateadd(day,1,dateadd(day,-day(getdate()),getdate())) dy,month(getdate()) mth

from t1 ) tmp1 union all select dateadd(day,1,dy),mth,case when datepart(dw,dateadd

(day,1,dy))=2 then 1 else 0 end from x where month(dateadd(day,1,dy))=mth ) select min(dy)

first_monday,max(dy) last_monday from x where is_monday=1

七十二、创建一个日历

db2

with x (dy,dm,mth,dw,wk) as ( select (current_date -day(current_date) day +1 day) dy,day

((current_date -day(current_date) day +1 day)) dm,month(current_date) mth,dayofweek

(current_date -day(current_date) day +1 day) dw,week(iso(current_date -day(current_date) day+1 day) wk from t1 union all select dy+1 day,day(dy+1 day),mth,dayofweek(dy+1 day),week_iso(dy+1 day) from x where month(dy+1 day)=mth ) select max(case dw when 2 then
dm end) as Mo,max(case dw when 3 then dm end) as Tu,max(case dw when 4 then dm end) as We,max(case dw when 5 then dm end) as Th,max(case dw when 6 then dm end) as Fr,max(case dw when 7 then dm end) as Sa,max(case dw when 1 then dm end) as Su from x group by
wk order by wk

oracle

with x as ( select * from (select to_char(trunc(sysdate,'mm')+level-1,'iw') wk,to_char

(trunc(sysdate,'mm')+level-1,'dd') dm,to_number(to_char(trunc(sysdate,'mm')+level-1,'d'))

dw,to_char(trunc(sysdate,'mm')+level-1,'mm') curr_mth,to_char(sysdate,'mm') mth from dual

connect by level<=31) where curr_mth<=31) select max(case dw when 2 then dm end) as Mo,max(case dw when 3 then dm end) as Tu,max(case dw when 4 then dm end) as We,max(case dw when 5 then dm end) as Th,max(case dw when 6 then dm end) as Fr,max(case dw when 7
then dm end) as Sa,max(case dw when 1 then dm end) as Su from x group by wk order by wk

postgresql

select max(case dw when 2 then dm end) as Mo,max(case dw when 3 then dm end) as Tu,max(case dw when 4 then dm end) as We,max(case dw when 5 then dm end) as Th,max(case dw when 6 then dm end) as Fr,max(case dw when 7 then dm end) as Sa,max(case dw when 1 then
dm end) as Su from (select * from ( select cast(date_trunc('month',current_date) as date) +x.id,to_char(cast(date_trunc('month',current_date0 as date)+x.id,'iw') as wk,to_char(cast(date_trunc('month',current_date) as date) +x.id,'dd') as dm,cast(to-char(cast(date_trunc('month',current_date)
as date) +x.id,'d') as integer) as dw,to_char(cast(date_trunc('month',current_date) as date) +x.id,'mm') as curr_mth,to_char(current_date,'mm') as mth

from generate_series (0,31) x(id) ) x where mth=curr_mth ) y group by wk order by wk

mysql

select max(case dw when 2 then dm end) as Mo,max(case dw when 3 then dm end) as Tu,max(case dw when 4 then dm end) as We,max(case dw when 5 then dm end) as Th,max(case dw when 6 then dm end) as Fr,max(case dw when 7 then dm end) as Sa,max(case dw when 1 then
dm end) as Su from (select date_format(dy,'%u') wk,date_format(dy,'%d') dm,date_format(dy,'%w')+1 dw from (select adddate(x.dy,t500.id-1) dy,x.mth from (select adddate(current_date,-dayofmonth(current_date)+1) dy,date_format(adddate(current_date,-dayomonth(current_date)+1),'%m')
mth from t1) x,t500 where t500.id<=31 and date_format(adddate(x.dy,t500.id-1),'%m')=x.mth) y ) z group by wk order by wk

sqlserver

with x (dy,dm,mth,dw,wk) as ( select dy,day(dy) dm,datepart(m,dy) mth,datepart(dw,dy)

dw,case when datepart(dw,dy)+1 then datepart(ww,dy)-1 else datepart(ww,dy) end wk from

(select dateadd(day,-day(getdate())+1,getdate()) dy from t1 ) x union all select dateadd

(d,1,dy),day(dateadd(d,1,dy)),mth,datepart(dw,dateadd(d,1,dy)),case when datepart

(dw,dateadd(d,1,dy))=1 then datepart(wk,dateadd(d,1,dy))-1 else datepart(wk,dateadd(d,1,dy)) end from x where datepart(m,dateadd(d,1,dy))=mth) select max(case dw when 2 then dm end) as Mo,max(case dw when 3 then dm end) as Tu,max(case dw when 4 then dm end)
as We,max(case dw when 5 then dm end) as Th,max(case dw when 6 then dm end) as Fr,max(case dw when 7 then dm end) as Sa,max(case dw when 1 then dm end) as Su from x group by wk order by wk
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: