两个日期之间的天数与年,月,日

时间:2019-09-23 16:22:19

标签: sql sql-server

是否可以通过查询提供两个日期之间的天数进行计数    具有今天的日期和输出的出色天数:年,月,日和    提供30天,60天,90天的出色日子?

如果我从“ Check_Date”和“ Created_Date”声明“年,月,日”。    SQL提供了一个单独的窗口,并且,我只看到声明的“ FromDate”时间    使用“ ToDate”显示总计的年,月,日。我在寻找一种方法    要获得结果,请按“ Created_Date”记录进行滚动,并包括:    年,月,日和出色的日子提供30天,60天,90天    天?

抄写代码:

DECLARE @FromDate DATETIME = '2015-01-01 00:00:00', 
        @ToDate   DATETIME = '2019-09-18 00:00:00',
        @Years INT, @Months INT, @Days INT, @tmpFromDate DATETIME
SET @Years = DATEDIFF(YEAR, @FromDate, @ToDate)
 - (CASE WHEN DATEADD(YEAR, DATEDIFF(YEAR, @FromDate, @ToDate),
          @FromDate) > @ToDate THEN 1 ELSE 0 END) 


SET @tmpFromDate = DATEADD(YEAR, @Years , @FromDate)
SET @Months =  DATEDIFF(MONTH, @tmpFromDate, @ToDate)
 - (CASE WHEN DATEADD(MONTH,DATEDIFF(MONTH, @tmpFromDate, @ToDate),
          @tmpFromDate) > @ToDate THEN 1 ELSE 0 END) 

SET @tmpFromDate = DATEADD(MONTH, @Months , @tmpFromDate)
SET @Days =  DATEDIFF(DAY, @tmpFromDate, @ToDate)
 - (CASE WHEN DATEADD(DAY, DATEDIFF(DAY, @tmpFromDate, @ToDate),
          @tmpFromDate) > @ToDate THEN 1 ELSE 0 END) 

SELECT @FromDate FromDate, @ToDate ToDate, 
       @Years Years,  @Months Months, @Days Days



SELECT DISTINCT  
ge.Name, --table columns
ge.Entity_Type, 
ge.Entity_Number, 
bc.Super_Entity_ID, 
ch.Check_Date, --check created
ch.Created_Date, --if payment was received
ch.Check_Number, 
ch.Amount,
vn.Vendor_Name



Check_Date,Created_Date,DATEDIFF("DAY",Check_Date,Created_Date) AS DAY


FROM dbo.gl_entities AS ge
INNER JOIN
dbo.super_entity AS se
ON ge.Super_Entity_ID = se.Super_Entity_ID 
INNER JOIN
dbo.bank_codes AS bc
ON se.Super_Entity_ID = bc.Super_Entity_ID 
INNER JOIN
dbo.checks AS ch
ON bc.Bank_Code_ID = ch.Bank_Code_ID 
INNER JOIN
dbo.vendors AS vn
ON ch.Vendor_ID = vn.Vendor_ID


WHERE 
ge.Active = 1 and vn.active = 1 and (ge.IS_Shadow = 1 OR se.IS_Tiered = 0)

AND CHECK_DATE > '20150101 00:00:00'
AND CHECK_DATE< '20190918 00:00:00'

ORDER BY ch.Check_Date, ch.Created_Date

2 个答案:

答案 0 :(得分:0)

仅在查询中自己的列中重复它们:

Execution time (hr): 54s 105.970357ms

如果DISTINCT子句遇到问题,则始终可以使用子查询。如果您希望年,月,日基于各列之一而不同,则必须进行详细说明,然后我们才能将在SET语句中所做的所有操作移至SELECT语句中。

没有表和数据,我无法很好地进行测试。

答案 1 :(得分:0)

伪代码尝试演示移动集语句以选择语句

declare @begin date = '2018-01-01'
declare @end date = '2019-01-01'

declare @middletest int = datediff("dd", @begin, @end)/2
declare @middledate date = dateadd("dd", @middletest, @begin)
declare @middlemonth int = month(@middledate)

select @middlemonth half_month, @middledate middle_date
   -- ,other_columns here
from table
where something

开始更换。 @middlemonth在select语句中,因此将其替换为month(@middledate)。

declare @begin date = '2018-01-01'
declare @end date = '2019-01-01'

declare @middletest int = datediff("dd", @begin, @end)/2
declare @middledate date = dateadd("dd", @middletest, @begin)

select month(@middledate) half_month, @middledate middle_date
   -- ,other_columns here
from table
where something

@middledate现在在select语句的2个位置中,因此每次将其替换为dateadd(“ dd”,@middletest,@begin)。

declare @begin date = '2018-01-01'
declare @end date = '2019-01-01'

declare @middletest int = datediff("dd", @begin, @end)/2

select month(dateadd("dd", @middletest, @begin)) half_month,
    dateadd("dd", @middletest, @begin) middle_date
    -- ,other_columns here
from table
where something

继续。

declare @begin date = '2018-01-01'
declare @end date = '2019-01-01'

select month(dateadd("dd", datediff("dd", @begin, @end)/2, @begin)) half_month,
    dateadd("dd", datediff("dd", @begin, @end)/2, @begin) middle_date
    -- ,other_columns here
from table
where something

您可以根据需要用表中的值替换内容。如果表中有一列start_date,并且您希望将其作为开始,然后使用当前日期作为结束:

select month(dateadd("dd", datediff("dd", start_date, getdate())/2, start_date)) half_month,
    dateadd("dd", datediff("dd", start_date, getdate())/2, start_date) middle_date
    -- ,other_columns here
from table
where something