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 -
SELECT TOP 5 * FROM
-
(Select Client_id, branch , sum(bill_amount)amount
-
from dbo.billtable
-
where trade_date >= '01 oct 2009' and trade_date <= '31 oct 2009'
-
AND BRANCH='A'
-
group by branch,Client_id)
-
ORDER BY ter_branch tr DESC
-
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
5 5589
It sounds more favourable to JOIN to this other branch table
rather than loop through al the branch names
Try this(an example to simulate ur situation) Creating some sample record set - declare @tblBill table(Clientid int, Billamount int, Branch varchar(10),BillDate datetime)
-
declare @tblBranch table(Branch varchar(10))
-
insert into @tblBill
-
select 11111,100000,'A','10/01/2009' union all
-
select 22222,199999, 'A' ,'10/21/2009' union all
-
select 33333,199998,'A','10/25/2009' union all
-
select 11,100,'A','10/02/2009' union all
-
select 22,5000, 'A' ,'10/03/2009' union all
-
select 33,34567,'A','10/04/2009' union all
-
select 44444,12315, 'B','10/24/2009' union all
-
select 55555,5444,'B' ,'09/27/2009'union all
-
select 66666, 5410, 'B' ,'10/18/2009' union all
-
select 678,33333, 'B','10/24/2009' union all
-
select 12347,444,'B' ,'10/27/2009'union all
-
select 012, 123, 'B' ,'10/18/2009' union all
-
select 77777,10000, 'C','10/23/2009' union all
-
select 88888,20000,'C' ,'08/27/2009'union all
-
select 99999, 30000, 'C' ,'10/05/2009' union all
-
select 189,40000, 'C','10/23/2009' union all
-
select 987,1110000,'C' ,'10/27/2009'union all
-
select 456, 4566, 'C' ,'10/05/2009' union all
-
select 12345,70000, 'D','10/23/2009' union all
-
select 23456,90000,'D' ,'07/27/2009'union all
-
select 34567, 1110000, 'D' ,'10/05/2009'
-
insert into @tblBranch
-
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] - select
-
Clientid
-
,Billamount
-
,Branch
-
,BillDate
-
from (
-
select
-
DENSE_RANK() over(partition by t1.Branch order by t1.Billamount desc) rn
-
,t1.*
-
from @tblBill t1
-
inner join @tblBranch t2
-
on t1.Branch = t2.Branch
-
where t1.BillDate between '10/01/2009' And '10/31/2009') X
-
where X.rn <=3
Output: Clientid Billamount Branch BillDate - 22222 199999 A 2009-10-21 00:00:00.000
-
33333 199998 A 2009-10-25 00:00:00.000
-
11111 100000 A 2009-10-01 00:00:00.000
-
678 33333 B 2009-10-24 00:00:00.000
-
44444 12315 B 2009-10-24 00:00:00.000
-
66666 5410 B 2009-10-18 00:00:00.000
-
987 1110000 C 2009-10-27 00:00:00.000
-
189 40000 C 2009-10-23 00:00:00.000
-
99999 30000 C 2009-10-05 00:00:00.000
-
34567 1110000 D 2009-10-05 00:00:00.000
-
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.
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
can some one give reference for Cursor writing , it will be big help
plz reply
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
...
|
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...
|
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.
...
|
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...
|
by: debasisdas |
last post by:
Using FOR LOOP in CURSOR----no need to open and close.
-----------------------------------------------------------------------------------------
DECLARE
CURSOR DD IS SELECT * FROM EMP WHERE...
|
by: debasisdas |
last post by:
Sample example to show FOR UPDATE CURSOR
-----------------------------------------------------------------------------
DECLARE
CURSOR EMPREC IS SELECT * FROM EMP FOR UPDATE OF SAL;
MYREC...
|
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...
|
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...
|
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')...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
| |