473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Writing a cursor with do while loop

Hi

i want to write a cursor where in branch name should be passed into the cursor one by one, I have a bill table with following detail

Client, bill_date,bill_amount, branch ...

I need to fetch data for top 5 client from every branch

below query gives me the data across for branch A

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 5 * FROM 
  2. (Select Client_id, branch , sum(bill_amount)amount
  3. from dbo.billtable
  4. where trade_date >= '01 oct 2009' and trade_date <= '31 oct 2009'
  5. AND BRANCH='A'    
  6. group by branch,Client_id)
  7. ORDER BY ter_branch tr DESC
  8.  
for all the branches i dont want to hard code the branch value as there are around 900 braches

there is one more table which contain branch names, if do while loop can take branch name as a variable

please help
Dec 1 '09 #1
5 5589
code green
1,726 Expert 1GB
It sounds more favourable to JOIN to this other branch table
rather than loop through al the branch names
Dec 1 '09 #2
nbiswas
149 100+
Try this(an example to simulate ur situation)

Creating some sample record set

Expand|Select|Wrap|Line Numbers
  1. declare @tblBill table(Clientid int, Billamount int, Branch varchar(10),BillDate datetime)
  2. declare @tblBranch table(Branch varchar(10))
  3. insert into @tblBill 
  4.     select 11111,100000,'A','10/01/2009' union all 
  5.     select 22222,199999, 'A' ,'10/21/2009' union all
  6.     select 33333,199998,'A','10/25/2009' union all 
  7.     select 11,100,'A','10/02/2009' union all 
  8.     select 22,5000, 'A' ,'10/03/2009' union all
  9.     select 33,34567,'A','10/04/2009' union all 
  10.     select 44444,12315, 'B','10/24/2009' union all
  11.     select 55555,5444,'B' ,'09/27/2009'union all 
  12.     select 66666, 5410, 'B' ,'10/18/2009' union all 
  13.     select 678,33333, 'B','10/24/2009' union all
  14.     select 12347,444,'B' ,'10/27/2009'union all 
  15.     select 012, 123, 'B' ,'10/18/2009' union all 
  16.     select 77777,10000, 'C','10/23/2009' union all
  17.     select 88888,20000,'C' ,'08/27/2009'union all 
  18.     select 99999, 30000, 'C' ,'10/05/2009' union all 
  19.     select 189,40000, 'C','10/23/2009' union all
  20.     select 987,1110000,'C' ,'10/27/2009'union all 
  21.     select 456, 4566, 'C' ,'10/05/2009' union all 
  22.     select 12345,70000, 'D','10/23/2009' union all
  23.     select 23456,90000,'D' ,'07/27/2009'union all 
  24.     select 34567, 1110000, 'D' ,'10/05/2009' 
  25. insert into @tblBranch 
  26.     select 'A' union all select 'B' union all select 'C' union all select 'D'
Query- [ Picking up top 3 clients from every branch as per the highest bill payment for a particular data range]

Expand|Select|Wrap|Line Numbers
  1. select 
  2.         Clientid
  3.         ,Billamount
  4.         ,Branch
  5.         ,BillDate
  6. from (
  7. select 
  8. DENSE_RANK() over(partition by t1.Branch order by t1.Billamount desc) rn
  9. ,t1.* 
  10. from @tblBill t1
  11. inner join @tblBranch t2
  12. on t1.Branch = t2.Branch
  13. where t1.BillDate between '10/01/2009' And '10/31/2009') X
  14. where X.rn <=3
Output:

Clientid Billamount Branch BillDate
Expand|Select|Wrap|Line Numbers
  1. 22222    199999    A    2009-10-21 00:00:00.000
  2. 33333    199998    A    2009-10-25 00:00:00.000
  3. 11111    100000    A    2009-10-01 00:00:00.000
  4. 678    33333    B    2009-10-24 00:00:00.000
  5. 44444    12315    B    2009-10-24 00:00:00.000
  6. 66666    5410    B    2009-10-18 00:00:00.000
  7. 987    1110000    C    2009-10-27 00:00:00.000
  8. 189    40000    C    2009-10-23 00:00:00.000
  9. 99999    30000    C    2009-10-05 00:00:00.000
  10. 34567    1110000    D    2009-10-05 00:00:00.000
  11. 12345    70000    D    2009-10-23 00:00:00.000
Hope this helps.

One advice: Try to avoid procedural approaches(like cursor, while loops , if ..else etc.). Adopt Set Based approach(e.g. CTE etc.) . Your query performance will be higher.
Dec 2 '09 #3
Hi,

I am useing MS SQL 2000 where in 'DENSE_RANK' is not a recognized function name.

so that I can not use this function, thats the reason I am writing a cursor in MS SQL 2000
Dec 2 '09 #4
can some one give reference for Cursor writing , it will be big help

plz reply
Dec 2 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: robert | last post by:
Mr. Kyte's article doesn't use cursors, while Mr. Feuerstein's book examples do. my recollection of conventional wisdom is to avoid using cursors. is this difference merely a question of style,...
2
by: satishchandra999 | last post by:
I have SP, which has a cursor iterations. Need to call another SP for every loop iteration of the cursor. The pseudo code is as follows.. Create proc1 as Begin Variable declrations... ...
0
debasisdas
by: debasisdas | last post by:
SAMPLE CODE USING RECORD =========================== declare cursor c1 is select * from dept; type drec is record (a dept.deptno%type, b dept.dname%type, c dept.loc%type); type ttype is...
0
debasisdas
by: debasisdas | last post by:
RESTRICTIONS ON CURSOR VARIABLES ================================= Currently, cursor variables are subject to the following restrictions: Cannot declare cursor variables in a package spec. ...
0
debasisdas
by: debasisdas | last post by:
This thread contains some useful tips/samples regarding some advance concepts in cursors. FEW MORE EXAMPLES =================== declare er emp%rowtype; cursor c1 is select * from emp; begin...
0
debasisdas
by: debasisdas | last post by:
Using FOR LOOP in CURSOR----no need to open and close. ----------------------------------------------------------------------------------------- DECLARE CURSOR DD IS SELECT * FROM EMP WHERE...
0
debasisdas
by: debasisdas | last post by:
Sample example to show FOR UPDATE CURSOR ----------------------------------------------------------------------------- DECLARE CURSOR EMPREC IS SELECT * FROM EMP FOR UPDATE OF SAL; MYREC...
0
debasisdas
by: debasisdas | last post by:
SAMPLE CODE TO SHOW USE OF REFCURSOR ======================================= EXAMPLE #1 ---------------------- declare --declare the fer cursor. type my_ref_cur_typ is ref cursor; --declare...
0
debasisdas
by: debasisdas | last post by:
Cursor Variable Returning %ROWTYPE ----------------------------------------------------------- DECLARE TYPE TmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE; tmp_cv TmpCurTyp;TYPE EmpCurTyp IS REF...
6
by: t_rectenwald | last post by:
Hello, I attempting to execute an Oracle query, and write the results to a file in CSV format. To do so, I've done the following: import cx_Oracle db = cx_Oracle.connect('user/pass@DBSID')...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.