我对MSSQL有点生疏,但我正在尝试编写一个查询,显示30天内的库存移动。
简而言之,我有以下查询,我使用不同的约会参数进行了几次
SELECT * FROM InvMovements
Where EntryDate between DATEADD(day,datediff(DAY,0,GETDATE()) -30,0)
and
DATEADD(day,datediff(day,0,getdate()) - 0)
Order by EntryDate DESC
和
SELECT * FROM InvMovements
Where EntryDate between DATEADD(day,datediff(DAY,0,GETDATE()) - 60,0)
and
DATEADD(day,datediff(day,0,getdate()) -31,0)
Order by EntryDate DESC
我想要达到的目标,即使它使用SSRS都是按照各个查询进行分组,因此结果集在其分组下 30 60 90
不确定在普通查询中是否可行,或者我是否需要使用SSRS和列个别查询。
感谢任何帮助
答案 0 :(得分:0)
我会在 EntryDate 之间获得 DATEDIFF ,然后将其除以 30 和 INTERGER ize it对于团体。然后添加1 ,以便0 - 29天= 1,乘以30 ,以获得日期组。
SELECT * ,
(CAST(DATEDIFF(DAY, EntryDate, GETDATE()) / 30 as INTEGER) + 1) * 30 AS PERIOD
FROM InvMovements
Order by EntryDate DESC
不确定MySQL的语法有何不同。 :(
答案 1 :(得分:0)
declare @CurrentDate datetime = convert(date,GETDATE())
;with M(i,o) as (
select * from (
values (0,30),(31,60),(61,90),(91,120)) R(i,o)
)
select
Period = convert(nvarchar,m.i)+'-'+convert(nvarchar,m.o),
*
FROM InvMovements
Where EntryDate between DATEADD(day,-m.o,@CurrentDate)
and DATEADD(day,-m.i,@CurrentDate)+0.99995
Order by EntryDate DESC
答案 2 :(得分:0)
这是一个快速的代码示例,除以30.41666666666667,因为它是365天除以12个月 TimeCreated = TIMEstamP
分组依据是1到12,每组30天
在MSSQL上,当您将int转换为int时,它只需要整数就可以了,它不会四舍五入。对于MYSQL不确定,可以通过选择cast(0.99 int)对其进行测试,您应该收到0
/****** Script for SelectTopNRows command from SSMS ******/
SELECT sum(isnull(Quantity,1)) as QTY,cast(DATEDIFF(day,TimeCreated,getdate())/30.41666666666667 as int) Which30Days
FROM [estimatelinedetail] left join [estimate] on estimate.TxnID=estimatelinedetail.IDKEY
where ItemRef_ListID ='800022B3-1567464337' and TimeCreated>= GETDATE()-365
group by cast(DATEDIFF(day,TimeCreated,getdate())/30.41666666666667 as int)