简体   繁体   English

从年份和周数的组合中提取一周中的 7 天作为日期?

[英]Extract 7 days of the week as a date from a combination of the year and week number?

I have a table (SQL Server 2017) where the data is stored at the level of the year number and week number.我有一个表(SQL Server 2017),其中数据存储在年号和周号级别。

    +---------+-------------+---------+----------+-----+
    | year_id | week_number | good_id | store_id | qty |
    +---------+-------------+---------+----------+-----+
    | 2019    | 42          | 113466  | 41       | 7   |
    +---------+-------------+---------+----------+-----+

I need to get a similar table, but at the day level, where the quantity ( qty ) will be divided into 7 parts evenly for each day.我需要一张类似的表格,但在日级别,数量( qty )将每天平均分为 7 个部分。

+---------+-------------+---------+----------+-----+------------+
| year_id | week_number | good_id | store_id | qty | date_id    |
+---------+-------------+---------+----------+-----+------------+
| 2019    | 42          | 113466  | 41       | 1   | 2019-10-14 |
+---------+-------------+---------+----------+-----+------------+
| 2019    | 42          | 113466  | 41       | 1   | 2019-10-15 |
+---------+-------------+---------+----------+-----+------------+
| 2019    | 42          | 113466  | 41       | 1   | 2019-10-16 |
+---------+-------------+---------+----------+-----+------------+
| 2019    | 42          | 113466  | 41       | 1   | 2019-10-17 |
+---------+-------------+---------+----------+-----+------------+
| 2019    | 42          | 113466  | 41       | 1   | 2019-10-18 |
+---------+-------------+---------+----------+-----+------------+
| 2019    | 42          | 113466  | 41       | 1   | 2019-10-19 |
+---------+-------------+---------+----------+-----+------------+
| 2019    | 42          | 113466  | 41       | 1   | 2019-10-20 |
+---------+-------------+---------+----------+-----+------------+

I found a way to get a date from the year and week number , but how do I get 7 rows from one at once?我找到了一种从年和周数中获取日期的方法,但是如何一次从一个中获取 7 行?

You can create a calendar table as shown here and join the original table as shown below to get the desired output.您可以创建一个如此处所示的日历表并加入如下所示原始表以获得所需的 output。

Select 
      CalendarDate
      , year_id
      , week_number
      , good_id
      , store_id
      , 1 as qty
from dbo.RunningNumbers
inner join ToBeGenerated on CalendarYear = year_id and week_number = CalendarWeek

Here is the live db<>fiddle demo.这是现场db<>fiddle演示。

There you go.那里有 go。 You need to join a table with all the dates and then divide the qty by the value you want:您需要加入一个包含所有日期的表格,然后将数量除以您想要的值:


use tempdb

CREATE TABLE tbl1
(
   year_id INT, week_number INT, good_id INT, store_id INT, qty INT
)

INSERT INTO tbl1
VALUES (2019, 42, 113466, 41, 7)
GO
WITH sample AS(
   SELECT CAST('2019-10-01' AS DATE) as DT
   UNION ALL
   SELECT DATEADD(dd,1,dt)
    FROM sample
   WHERE DATEADD(dd,1,dt) < CAST('2019-12-31' AS DATE))

SELECT dt, YEAR(dt) AS [Year], DATEPART(WEEK,dt) AS Week
INTO wks
FROM sample

SELECT t.year_id, t.week_number, t.good_id, t.store_id, qty/7 AS [Qty], dt AS [Date]
  FROM tbl1 t
 INNER JOIN wks S on s.Week = t.week_number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM