views:

160

answers:

1

How to repeat the row values up to end date

For Example

Table1

Date         Name   Dept 
-----------------------
12-02-2009   Raja   IT
13-02-2009   Ravi   CSE
14-02-2009   Ramu   ECE

From the above table value i want to repeat the value between the two dates from 12-02-2009 to 12-03-2009.

Expected Output

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

How to make a query for repeating the row values upto end date or is possible in vb.net by using while loop like

code

select max(date) from table

while max(date) < End Date

'Repeat the row values

end while

My Table Structue

Date Name Dept Time

12-02-2009   Raja   IT  09:00 
13-02-2009   Ravi   CSE 18:00 
14-02-2009   Ramu   ECE 10:00

Expected Output

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

...,

If StartDate is same value of Date column For EndDate i have to check the time column, if time is greater than 17:00 it should display a next date in EndDate column.

Need SQL Query or VB.Net Code Help

+3  A: 

You could try something like

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

Have a look at

for executing queries with params from vb.net/c#

EDIT

To get the expected output, you can try

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

Now you need to decide which date you want to use to order by in the ROW_NUMBER() line, or how you wish to repeat the rows by date.

EDIT

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
astander
@astander - Thanks for your repsonse - Can any explain, what your query is exactly doing
Gopal
I edited the answer to add some comments to the query. hope that helps.
astander
@astander - you used this line - % 3 means 3 rows - Suppose there is n number of rows means, how to modify your query
Gopal
I changed the answer to take into account a variable called *@Count*
astander
@astander - One more question - why you are making id. With out id query will work or not.
Gopal
I use the ID to allow for easy joining between the 2 CTE selects. You could try doing it without the ID, but this seems like the easiest way.
astander
@Astander - I want to run this query through my code. Already i have the start date, end date, row count. @table. How to make a sub query by using your query.
Gopal
You should be able to execute this query, as a standard query from vb.net, and just use StartDate and EndDate as params.
astander
@Astander. Thankyou, It was working fine. Sorry am not mentioned in my question, i Have three date column in my table like date1, date2, date3. All the three dates should increment upto end date. How to do this. Please.
Gopal
I am sorry, not following you. Can you provide sample table structure and data, so i can have a look?
astander
Astander - I added my table structure. please check my question
Gopal
Please try to clarrify and show the expected result from *If StartDate is same value of Date column For EndDate i have to check the time column, if time is greater than 17:00 it should display a next date in EndDate column.*
astander
I Edited my Question. Can you check again.
Gopal
@Astander - How to write a insert command by using your query. For example "insert into table1 .... your query...." It tried like this. It was showing error, Can you tell how to modify your for inserting. Please...
Gopal
Have a look at the edit answer. Otherwise, please specify the error you are getting.
astander
@Astander - Now it was showing error as "The statement terminated. The maximum recursion 100 has been exhausted before statement". Is limitation of temp table?
Gopal
See edit, you need to add **OPTION (MAXRECURSION 0)**
astander
@Astander - Thank you, Can i make temporary table in the database instead of creating @temp table, because am facing the performance problem. Which is the best way to run this query.
Gopal
You can create a temp table, or insert into a normal table if you wish X-)
astander
@Astander, Query is working fine. Thank You very much for your support and Help.
Gopal
That is what the site is here for X-)
astander