根據兩日期欄位產生兩日期間隔天數欄位

來源:互聯網
上載者:User

表結構資料如下:(sdate預計時間,edate實際時間)
id  sdate        edate
1   2010-01-01   2010-01-03
2   2010-01-02   2010-01-04
3   2010-01-04   2010-01-02
4   2010-01-07   2010-01-03
5   2010-01-08   2010-01-08
6   2010-01-15   2010-01-09
7   2010-01-23   2010-01-20

統計結果如下:(按2天的來進行統計,實際時間和預計時間來進行比較)
准交期       數量
...
提前6天       1  
提前4天       1
提前2天       2
當天          1
延遲2天       2

....
--> 測試資料:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([id] int,[sdate] datetime,[edate] datetime)
insert [TB]
select 1,'2010-01-01','2010-01-03' union all
select 2,'2010-01-02','2010-01-04' union all
select 3,'2010-01-04','2010-01-02' union all
select 4,'2010-01-07','2010-01-03' union all
select 5,'2010-01-08','2010-01-08' union all
select 6,'2010-01-15','2010-01-09' union all
select 7,'2010-01-23','2010-01-20'

select * from [TB]

select id,sdate,edate
from tb
/*
准交期       數量
...
提前6天       1  
提前4天       1
提前2天       2
當天          1
延遲2天       2*/

select
[准交期]=
case when datediff(dd,edate,sdate) < 0 then '延遲' +convert(varchar(2),datediff(dd,edate,sdate)) +'天'
     when datediff(dd,edate,sdate) > 0 then '提前' +convert(varchar(2),datediff(dd,edate,sdate)) +'天'
     else '當天' end ,
[數量] = count(*)
from tb
where datediff(dd,edate,sdate)%2 = 0
group by datediff(dd,edate,sdate)

/*
准交期      數量
-------- -----------
延遲-2天    2
當天       1
提前2天     1
提前4天     1
提前6天     1

(5 行受影響)*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.