[英]Select 7 days ago in 7 week
@status [nvarchar](max),
@fromDate [datetime],
@toDate [datetime],
@companyId [int]
select
ISNULL(SUM(rec.SubTotal), 0) AS SubTotal,
sto.Id AS StoreId,
CAST(rec.CreatedDate AS DATE) as CreatedDate,
sto.Name AS StoreName from Receipts rec
left join(
select ReceiptId, SUM(Quantity) as Quantity from ReceiptDetails
group by ReceiptId
) red on rec.Id = red.ReceiptId
left outer join(
select Id,Name from Stores
group by Id,Name
) sto on rec.StoreId = sto.Id
where rec.CompanyId = @companyId
and rec.Status = @status
and rec.CreatedDate <= @todate
and rec.CreatedDate >= @fromDate
group by sto.Id, sto.Name,CAST(rec.CreatedDate AS DATE)
This is my current query SQL ,at the present i select data in everydays in a rangeDate by @todate and @fromdate 这是我当前的查询SQL,目前我每天都在@Date和@fromdate的rangeDate中选择数据
Now i want to select data by CreatedDate by dates of the last 7 weeks, Example when @fromdate is Today : 2018-12-1 the data expected will be 现在我想按过去7周的日期按CreatedDate选择数据,例如@fromdate为Today:2018-12-1时的预期数据为
2018-10-20
2018-10-27
2018-11-3
2018-11-10
2018-12-17
2018-11-24
2018-12-1
My data at the present 我目前的数据
...
...
2018-11-29
2018-11-30
2018-12-1
I mean the date in 6 week ago 我的意思是6周前的日期
You could create this as a function and pass in the number of weeks but that's up to you. 您可以将其创建为函数并传入星期数,但这取决于您。
See below: 见下文:
declare @i as int
set @i = 7
select
ISNULL(SUM(rec.SubTotal), 0) AS SubTotal,
sto.Id AS StoreId,
case
when CAST(rec.CreatedDate as Date) between CAST(rec.CreatedDate AS DATE) -(7 * (@i)) and CAST(rec.CreatedDate AS DATE) -((7*(@i - 1)) + 1)
then
Cast(CAST(rec.CreatedDate -(7*(@i)) AS DATE))
when CAST(rec.CreatedDate as Date) between CAST(rec.CreatedDate AS DATE) -(7 * (@i-1)) and CAST(rec.CreatedDate AS DATE) -((7*(@i-2)) + 1)
then
Cast(CAST(rec.CreatedDate AS DATE) -(7*(@i-1)) AS DATE)
when CAST(rec.CreatedDate as Date) between CAST(rec.CreatedDate AS DATE) -(7 * (@i - 2)) and CAST(rec.CreatedDate AS DATE) -((7*(@i-3)) + 1)
then
Cast(CAST(rec.CreatedDate AS DATE) -(7*(@i-2)) AS DATE)
when CAST(rec.CreatedDate as Date) between CAST(rec.CreatedDate AS DATE) -(7 * (@i - 3)) and CAST(rec.CreatedDate AS DATE) -((7*(@i-4)) + 1)
then
Cast(CAST(rec.CreatedDate AS DATE) -(7*(@i-3)) AS DATE)
when CAST(rec.CreatedDate as Date) between CAST(rec.CreatedDate AS DATE) -(7 * (@i - 4)) and CAST(rec.CreatedDate AS DATE) -((7*(@i-5)) + 1)
then
Cast(CAST(rec.CreatedDate AS DATE) -(7*(@i-4)) AS DATE)
when CAST(rec.CreatedDate as Date) between CAST(rec.CreatedDate AS DATE) -(7 * (@i - 5)) and CAST(rec.CreatedDate AS DATE) -((7*(@i-6)) + 1)
then
Cast(CAST(rec.CreatedDate AS DATE) -(7*(@i-5)) AS DATE)
when CAST(rec.CreatedDate as Date) between CAST(rec.CreatedDate AS DATE) -(7 * (@i - 6)) and CAST(rec.CreatedDate AS DATE) -((7*(@i-7)) + 1)
then
Cast(CAST(rec.CreatedDate AS DATE) -(7*(@i-6)) AS DATE)
else ''
end as CreatedDate,
sto.Name AS StoreName
from Receipts rec
left join(
select ReceiptId, SUM(Quantity) as Quantity from ReceiptDetails
group by ReceiptId
) red on rec.Id = red.ReceiptId
left outer join(
select Id,Name from Stores
group by Id,Name
) sto on rec.StoreId = sto.Id
where rec.CompanyId = @companyId
and rec.Status = @status
and rec.CreatedDate <= @todate
and rec.CreatedDate >= @fromDate
group by sto.Id, sto.Name,CAST(rec.CreatedDate AS DATE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.