sql-server - 检索过去 11 个月的图表
问题描述
我需要 11 月和 12 月的最后 11 个月以及图表的当前月份,但在没有数据的情况下看不到月份。它仍然被称为不应该的最后几年。
declare @TempTable TABLE
(
[Order] int,
[Month] nvarchar(3),
[Booked] int,
[Cancelled] int
)
declare @CurrentYear int = cast(substring(convert(varchar, getdate(), 23), 0,5) as int)
declare @CurrentMonth int = cast(substring(convert(varchar, getdate(), 1), 0,3) as int)
insert into @TempTable
select
(@CurrentYear - substring(convert(varchar, A.CreatedAt, 23), 0,5)) * 11 +
@CurrentMonth - cast(substring(convert(varchar,A.CreatedAt, 1),0,3) as int)
as 'Order',
substring(convert(nvarchar, A.CreatedAt, 107), 0, 4) as 'Month',
Count(*) as 'Booked',
SUM(cast(A.IsCancelled as int)) AS 'Cancelled'
FROM ReservationTbl A
GROUP BY substring(convert(nvarchar, A.CreatedAt, 107), 0, 4),
(@CurrentYear - substring(convert(varchar, A.CreatedAt, 23), 0,5)) * 11 +
@CurrentMonth - cast(substring(convert(varchar,A.CreatedAt, 1),0,3) as int)
select * from @TempTable WHERE [Order] <> (SELECT MAX([Order]) FROM @TempTable) Order By [Order] DESC
解决方案
推荐阅读
- jquery - jquery history js无法读取未定义的属性'msie'
- ionic-framework - 在 ionic IOS 应用程序中看不到有效负载数据?
- spring - @Validated @Size LongArray 控制器与 kotlin
- python - 找出相似物体的两张不同图片之间的差异
- javascript - 如何处理深度嵌套的导航
- angular - 如何从api传递基于json的嵌套formarray动态形式的对象数组
- c++ - Drogon 为线程分配资源
- ios - swift scrollToRow 或 setContentOffset 不起作用
- python-3.x - 如何在 matplotlib 中更改 quiverplot [3d] 中的箭头属性?
- java - 我正在打开 netbeans,它正在加载,但屏幕上没有显示。当我再次打开它时,它只显示一个空白窗口,任何人都可以帮助我