Click here to Skip to main content
16,011,711 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all
Good morning ,


i have issue with sql data processing ....my table data is given below

Route Days

VB
113007  1
113007  1
113007  1
113007  1
113007  1
113007  1
113007  2
113007  2
113007  2
113007  2
113007  2
113007  2
113008  1
113008  1
113008  1
113008  1
113008  1
113008  1
113008  2
113008  2
113008  2
113008  2
113008  2
113008  2




but i need alter table with column record,after that my table must be look like...

Route Days record


VB
113007  1   1
113007  1   2
113007  1   3
113007  1   4
113007  1   5
113007  1   6
113007  2   1
113007  2   2
113007  2   3
113007  2   4
113007  2   5
113007  2   6
113008  1   1
113008  1   2
113008  1   3
113008  1   4
113008  1   5
113008  1   6
113008  2   1
113008  2   2
113008  2   3
113008  2   4
113008  2   5
113008  2   6




so please help me how can i archive the above result ,Route Data come through parameter (multiple value).

please help me
Posted

1 solution

I supposed that your table data is sorted by Route , Days criteria as you listed above, and I named it route

So here it is :

SQL
with a(r, d, cnt) 
as
(
select route, days , COUNT(*) from route
group by route, Days
union all
select r , d , cnt-1 from a where cnt >1 
)
select * from a
order by r,d, cnt


Thanks to CTE's :)

EDIT :
a = output table name of our CTE which have 3 fields r,d,cnt
r = is route id from your route table
d = is days field from your route table
cnt = a decreasing counter

our anchor statement in a recursive CTE :
SQL
select route, days , COUNT(*) from route
group by route, Days


our recursive part of our CTE :
SQL
select r , d , cnt-1 from a where cnt >1 


after these two parts we can use our CTE output in another CTE or in an ordinary SQL statement

so this is our ordinary SQL statement that uses our CTE output :
SQL
select * from a
order by r,d, cnt


Try to make a test table with your mentioned data in the question and run this query against it :)


EDIT 2 :
After seeing your data , try this one :
SQL
SELECT DepotCode, RouteCode, RouteName , ArbRouteName, TotalSale, DayNames, DayNumber ,ROW_NUMBER() OVER(PARTITION BY DepotCode, DayNumber  ORDER BY DepotCode, DayNumber) AS 'record'
FROM routes


If it was solved your problem do not forget to accept it as an answer or vote it up ;)
 
Share this answer
 
v4
Comments
mohd vaquas 30-Oct-11 3:03am    
can you explain with a(r,d,cnt) what is the a,r,d,cnt please .....
Amir Mahfoozi 30-Oct-11 3:17am    
I don't know how much you are familiar with CTE's but I try to express this query to some extents.
mohd vaquas 30-Oct-11 3:22am    
when i process the above its give me error
'The statement terminated. The maximum recursion 100 has been exhausted before statement completion'.
mohd vaquas 30-Oct-11 3:21am    
when i process the above its give me error

'The statement terminated. The maximum recursion 100 has been exhausted before statement completion'.
Amir Mahfoozi 30-Oct-11 3:30am    
Are you sure that you are running SQL to the same data that where mentioned in you question ?

(cnt >1) statement is for preventing unlimited recursions.

I didn't get that error here :-? So please give me the data that you are working on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900