March 17, 2010 at 3:16 am
i have a query like this:
SELECT StartDate,R1,SUM(Total),ROW_NUMBER() OVER(ORDER BY StartDate ) AS 'id'
FROM [MonthlySummary] (NOLOCK)
Group by [StartDate],R1
Order by StartDate,R1
is there a way to loop through these result, withought using cursor, and based on the id value i get from the ROW_NUMBER?
Thanks
Peleg
March 17, 2010 at 4:07 am
peleg k (3/17/2010)
i have a query like this:
SELECT StartDate,R1,SUM(Total),ROW_NUMBER() OVER(ORDER BY StartDate ) AS 'id'
FROM [MonthlySummary] (NOLOCK)
Group by [StartDate],R1
Order by StartDate,R1
is there a way to loop through these result, withought using cursor, and based on the id value i get from the ROW_NUMBER?
Peleg,
You can use a variable and a WHILE loop. Not really any better than using a cursor, though.
If you can say more about why you need the loop, it might help suggest an alternate solution.
March 17, 2010 at 4:16 am
i want to build a table which based on the StartDate, where i will have
StartDate(1),StartDate(2).........,R1,total
and this table at the end i will wriite to a file.
March 17, 2010 at 4:25 am
Ok. Some example data and expected output would help.
Otherwise, I'd just say use an INSERT statement straight from your SELECT.
March 17, 2010 at 4:36 am
this is how the table will look: (the total value will be add to the column with the date)
20100101000000,20100301000000,.......,R1
130,0,,,..., ,'a'
10,330,,6,..., ,'b'
March 17, 2010 at 4:56 am
Perhaps someone else will have the patience to extract some workable data and requirements from you.
Good luck!
March 17, 2010 at 9:27 am
Peleg... for better answers quicker, please see the first link in my signature line below.
I see what you're trying to do... it's called "Concatenation". There's also several other ways to do this. Lookup BCP in Book Online for a method to export such data.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply