如何使用SQL Server从当前日期获取最近的12个月

时间:2018-09-21 10:25:21

标签: sql sql-server database

查询: 我有一个名为CouponDataEdate列的表 我想从当前日期过去12个月。例如从2018年9月21日到2017年9月1日,如何使用我的查询?

with yearlist as  
( 
select MONTH(GETDATE()) as MONTH 
union all 
select  yl.MONTH - 1 as MONTH 
from yearlist yl 
where (yl.MONTH  - 1 <= MONTH(GetDate())) and  (yl.MONTH - 1 >=   MONTH(DATEADD(MONTH ,-8,GETDATE()))) 
) 
Select  month,(Select IsNull(Sum(BillAmount),0) from CouponData Where month(EDate) = YL.month) 
as Collection,(Select IsNull(Sum(AdultsQty+ChildQty),0) from CouponData Where month(EDate) = YL.month) 
as PaxTotal  from yearlist YL order by MONTH  asc 

4 个答案:

答案 0 :(得分:3)

我不清楚您的范围是什么意思。还不完全是12个月。但是,我建议您使用 dates

with months as ( 
      select dateadd(day, 1, eomonth(getdate(), -1)) as yyyymm
      union all 
      select  dateadd(month, -1, yyyymm)
      from months m
      where yyyymm >= dateadd(-13, month, getdate())
     ) 
Select m.yyyymm, coalesce(sum(BillAmount), 0) as Collection,
       coalesce(Sum(AdultsQty + ChildQty), 0) as PaxTotal
from months m left join
     CouponData cd
     on edate >= m.yyyymm and edate < dateadd(1, month, m.yyyym)
group by m.yyyymm
order by m.yyyymm;

答案 1 :(得分:0)

with totals as
(
  Select month(edate) as [month], 
    Sum(BillAmount) as bamt, 
    Sum(AdultsQty+ChildQty) as qty
  from CouponData
  Where edate >= dateadd(year,-1,getdate())
  group by month(edate)
)
select [Month],Isnull(bAmt,0) as Collection, qty as PaxTotal
from totals
order by [Month];

要拥有全部月份:

WITH totals
AS (SELECT MONTH(edate) AS [month],
           SUM(BillAmount) AS bamt,
           SUM(AdultsQty + ChildQty) AS qty
    FROM CouponData
    WHERE edate >= DATEADD(YEAR, -1, GETDATE())
    GROUP BY MONTH(edate))
SELECT m.mno AS [Month],
       ISNULL(bamt, 0) AS Collection,
       ISNULL(qty, 0) AS PaxTotal
FROM
(
    VALUES
        (1),(2),(3),
        (4),(5),(6),
        (7),(8),(9),
        (10),(11),(12)
        ) m (mno)
    LEFT JOIN totals t
        ON t.month = m.mno
ORDER BY m.mno;

答案 2 :(得分:0)

您不能只工作几个月,还需要考虑年份

with yearlist as  
( 
select MONTH(GETDATE()) + 12 * year(GETDATE()) as MONTH 
union all 
select  yl.MONTH - 1 as MONTH 
from yearlist yl 
where yl.MONTH  - 1 >= MONTH(GETDATE()) + 12 * year(GETDATE()) -12 
) 
Select  month /12 as 'year', month % 12 as 'month',(Select IsNull(Sum(BillAmount),0) from CouponData Where month(EDate)+ YEAR(EDate) * 12 = YL.month) 
as Collection,(Select IsNull(Sum(AdultsQty+ChildQty),0) from CouponData Where month(EDate) + YEAR(EDate) * 12 = YL.month) 
as PaxTotal  from yearlist YL order by MONTH  asc ; 

答案 3 :(得分:0)

您可以使用以下查询来获取日期范围。替换开始日期。

DECLARE @Joiningdate DATE
Declare @StartDate DATE= cast('2017/09/01' as datetime) 
Declare @EndDate DATE=getdate()

SET @StartDate = dateadd(DAY,-365 ,getdate()) 
print @StartDate
;with DateRange As
(
SELECT  TOP (DATEDIFF(DAY, @StartDate, @EndDate) + 1)
        Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @StartDate),'' as NoOfday
FROM    sys.all_objects a
        CROSS JOIN sys.all_objects b
)select * from DateRange