如何重复行值到结束日期
例如
表1
Date Name Dept
-----------------------
12-02-2009 Raja IT
13-02-2009 Ravi CSE
14-02-2009 Ramu ECE
从上表中我想重复从12-02-2009到12-03-2009两个日期之间的值。
预期产出
Date Name Dept
--------------------------
12-02-2009 Raja IT
13-02-2009 Ravi CSE
14-02-2009 Ramu ECE
15-02-2009 Raja IT
16-02-2009 Ravi CSE
17-02-2009 Ramu ECE
18-02-2009 Raja IT
19-02-2009 Ravi CSE
20-02-2009 Ramu ECE
...
...
12-03-2009
如何查询重复行值到结束日期,或者在vb.net中使用while循环如
码
select max(date) from table
while max(date) < End Date
'Repeat the row values
end while
我的表格结构
日期名称部门时间
12-02-2009 Raja IT 09:00
13-02-2009 Ravi CSE 18:00
14-02-2009 Ramu ECE 10:00
预期产出
Date Name Dept Time StartDate EndDate
12-02-2009 Raja IT 09:00 12-02-2009 12-02-2009
13-02-2009 Ravi CSE 18:00 13-02-2009 14-02-2009
14-02-2009 Ramu ECE 10:00 14-02-2009 14-02-2009
...
如果StartDate与Date列的值相同 对于EndDate,我必须检查时间列,如果时间大于17:00,它应该在EndDate列中显示下一个日期。
需要SQL查询或VB.Net代码帮助
答案 0 :(得分:3)
您可以尝试类似
的内容DECLARE @Table TABLE(
Date DATETIME,
Name VARCHAR(50),
Dept VARCHAR(50)
)
INSERT INTO @Table SELECT '12 Feb 2009', 'Raja', 'IT'
INSERT INTO @Table SELECT '13 Feb 2009', 'Ravi', 'CSE'
INSERT INTO @Table SELECT '14 Feb 2009', 'Ramu', 'ECE'
DECLARE @StartDate DATETIME,
@EndDate DATETIME,
@Count INT
SELECT @Count = COUNT(1) FROM @Table
SELECT @StartDate = '12 Feb 2009',
@EndDate = '12 Mar 2009'
--using CTE create a date range, and associated id per date
;WITH Dates AS (
SELECT @StartDate DateVal,
0 ID
UNION ALL
SELECT DateVal + 1,
(ID + 1) % @Count
FROM Dates
WHERE DateVal + 1 <= @EndDate
),
--using cte, create id per entry in the table you wish to repeat
RowNumbers AS(
SELECT *,
ROW_NUMBER() OVER(ORDER BY Date) ID
FROM @Table
)
--join dates to table to repeat id generated ids.
SELECT *
FROM Dates d LEFT JOIN
RowNumbers r on d.ID + 1 = r.ID
看看
用于执行来自vb.net/c#
的params的查询修改强>
要获得预期的输出,您可以尝试
DECLARE @Table TABLE(
Date DATETIME,
Name VARCHAR(50),
Dept VARCHAR(50),
Time VARCHAR(5)
)
INSERT INTO @Table SELECT '12 Feb 2009','Raja','IT','09:00'
INSERT INTO @Table SELECT '13 Feb 2009','Ravi','CSE','18:00'
INSERT INTO @Table SELECT '14 Feb 2009','Ramu','ECE','10:00'
SELECT *,
Date AS StartDate,
CASE
WHEN CAST(LEFT(Time,2) AS INT) > 17 THEN Date + 1
ELSE DATE
END EndDate
FROM @Table
现在您需要在ROW_NUMBER()行中决定要用于订购的日期,或者您希望按日期重复行的方式。
修改强>
This is how you would insert values
DECLARE @InsertTable TABLE(
DateVal DATETIME,
ID INT,
Date DATETIME,
Name VARCHAR(50),
Dept VARCHAR(50)
)
DECLARE @Table TABLE(
Date DATETIME,
Name VARCHAR(50),
Dept VARCHAR(50)
)
INSERT INTO @Table SELECT '12 Feb 2009', 'Raja', 'IT'
INSERT INTO @Table SELECT '13 Feb 2009', 'Ravi', 'CSE'
INSERT INTO @Table SELECT '14 Feb 2009', 'Ramu', 'ECE'
DECLARE @StartDate DATETIME,
@EndDate DATETIME,
@Count INT
SELECT @Count = COUNT(1) FROM @Table
SELECT @StartDate = '12 Feb 2009',
@EndDate = '12 Mar 2009'
--using CTE create a date range, and associated id per date
;WITH Dates AS (
SELECT @StartDate DateVal,
0 ID
UNION ALL
SELECT DateVal + 1,
(ID + 1) % @Count
FROM Dates
WHERE DateVal + 1 <= @EndDate
),
--using cte, create id per entry in the table you wish to repeat
RowNumbers AS(
SELECT *,
ROW_NUMBER() OVER(ORDER BY Date) ID
FROM @Table
)
--join dates to table to repeat id generated ids.
INSERT INTO @InsertTable --INSERT VALUES HERE
SELECT d.DateVal,
d.ID,
r.Date,
r.Name,
r.Dept
FROM Dates d LEFT JOIN
RowNumbers r on d.ID + 1 = r.ID
OPTION (MAXRECURSION 0) --this is required.
SELECT *
FROM @InsertTable