【问题标题】:WeekOfYear -1 SQL HELPP?WeekOfYear -1 SQL 帮助?
【发布时间】:2015-08-21 12:39:09
【问题描述】:
select distinct
pos.DivNo 
,WeekOfYear
,[ProductCode]
,sum(Quantity)[Quantity]
into #sales1
from cbis799p.dbo.pos pos
LEFT JOIN DataWarehouse.dbo.Calendar c on pos.PosDate = c.Datetime
Where pos.DivNo = 772 and ProductCode = '1020'
and WeekOfYear = 1
and YearWeek = 2015
group by pos.DivNo 
,WeekOfYear
,[ProductCode]

select * from #sales1
drop table #sales1


select distinct
pos.DivNo 
,Min(WeekOfYear) WeekOfYear
,[ProductCode]
,SUM(quantity) PreQuantity
into #sales2
from cbis799p.dbo.pos pos
LEFT JOIN DataWarehouse.dbo.Calendar c on pos.PosDate = c.Datetime
where pos.DivNo = 772 and ProductCode = '1020'
and WeekOfYear = 1
and YearWeek = 2015
group by pos.DivNo 
,WeekOfYear
,[ProductCode]

select * from #sales2
drop table #sales2

这是我的输出 我需要从一年中的一周来的前量 - 1周 这将使它成为 2014 年的第 52 周

我需要这个值是动态的,以便参数可以理解

【问题讨论】:

  • @lad2025 这只是要告诉我 2 天之间的差异
  • 用户也想搜索一年中的一周作为参数
  • 我不明白这个问题。是否有某些原因 DATEADD() 不是答案?

标签: sql-server visual-studio week-number product-quantity


【解决方案1】:
Declare @d Date = '2015-01-01'

    select 
        --Date
        @d
        --WeekYear of Date
        , DatePart(WEEK, @D)
        --Last day of 2014
        , DATEADD(DAY, -1, @D) 
        --Last WeekYear of 2014
        , DatePart(WEEK, DATEADD(DAY, -1, @D))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2012-09-26
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多