473,386 Members | 1,621 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,386 software developers and data experts.

Dynamic Cross Tab Using Cursors.

I have two tables
1)Rollout_detail
start_date Datetime,
contract_date Datetime,
budget_amt Money
store_id int(foriegn key referring store.store_id)
pan_number varchar(20)
roll_id int

2)store
store_id int(primary key)
skey varchar(10)

these two tables are tied with store_id
and in rollout_detail there can be many pan_numbers for a given
store.(pan_number + store ) are unique.

Now here is the problem.

I need to generate a cross tab report with
store,pan1_contract_date,pan1_budget_amt,pan1_star t_date,
pan2_Contract_date,pan2_budget_amt,pan2_start_date and so on.

I tried this with in a procedure.
-----------------------------------------------------------
CREATE PROCEDURE crosstab
@roll_id INT
WITH ENCRYPTION
AS
DECLARE @sql VARCHAR(8000),@panNumber VARCHAR(20)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
DECLARE al_cursor CURSOR FOR SELECT DISTINCT pan_number AS p_pannumber
FROM rollout_detail WHERE roll_id=@roll_id
OPEN al_cursor
SELECT @sql='SELECT skey, '
FETCH NEXT FROM al_cursor INTO @panNumber
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql=@sql+'(CASE pan_number WHEN
'''+rtrim(@panNumber)+''' THEN start_date END) as
P'+rtrim(@panNumber)+'_sd,(CASE pan_number WHEN
'''+rtrim(@panNumber)+''' THEN budget_amt END) as
P'+rtrim(@panNumber)+'_ba,(CASE pan_number WHEN
'''+rtrim(@panNumber)+''' THEN contract_date END) as
P'+rtrim(@panNumber)+'_cd,'

FETCH NEXT FROM al_cursor INTO @panNumber
END
SELECT @sql=left(@sql, len(@sql)-1)+' '
SELECT @sql=@sql+'FROM rollout_detail rd,store s
WHERE rd.store_id=s.store_id AND roll_id='+cast(@roll_id as varchar)

EXEC (@sql)
CLOSE al_cursor
DEALLOCATE al_cursor
SET ANSI_WARNINGS ON

EXECUTE crosstab 1
-----------------------------------------------------------
I am getting multiple records for the same store
the result set is.

skey pan1_cd pan1_ba pan1_sd pan2_cd pan2_ba pan2_sd
1 12/2/04 400.0 3/4/05 NULL NULL NULL
1 NULL NULL NULL 5/6/04 566.00 3/4/04
I want the result set merged for each store.

Please help,

Thanks a million

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
2 4067

"Jaidev Paruchuri" <ja****@hotmail.com> wrote in message
news:40*********************@news.frii.net...
I have two tables
1)Rollout_detail
start_date Datetime,
contract_date Datetime,
budget_amt Money
store_id int(foriegn key referring store.store_id)
pan_number varchar(20)
roll_id int

2)store
store_id int(primary key)
skey varchar(10)

these two tables are tied with store_id
and in rollout_detail there can be many pan_numbers for a given
store.(pan_number + store ) are unique.

Now here is the problem.

I need to generate a cross tab report with
store,pan1_contract_date,pan1_budget_amt,pan1_star t_date,
pan2_Contract_date,pan2_budget_amt,pan2_start_date and so on.

I tried this with in a procedure.
-----------------------------------------------------------
CREATE PROCEDURE crosstab
@roll_id INT
WITH ENCRYPTION
AS
DECLARE @sql VARCHAR(8000),@panNumber VARCHAR(20)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
DECLARE al_cursor CURSOR FOR SELECT DISTINCT pan_number AS p_pannumber
FROM rollout_detail WHERE roll_id=@roll_id
OPEN al_cursor
SELECT @sql='SELECT skey, '
FETCH NEXT FROM al_cursor INTO @panNumber
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql=@sql+'(CASE pan_number WHEN
'''+rtrim(@panNumber)+''' THEN start_date END) as
P'+rtrim(@panNumber)+'_sd,(CASE pan_number WHEN
'''+rtrim(@panNumber)+''' THEN budget_amt END) as
P'+rtrim(@panNumber)+'_ba,(CASE pan_number WHEN
'''+rtrim(@panNumber)+''' THEN contract_date END) as
P'+rtrim(@panNumber)+'_cd,'

FETCH NEXT FROM al_cursor INTO @panNumber
END
SELECT @sql=left(@sql, len(@sql)-1)+' '
SELECT @sql=@sql+'FROM rollout_detail rd,store s
WHERE rd.store_id=s.store_id AND roll_id='+cast(@roll_id as varchar)

EXEC (@sql)
CLOSE al_cursor
DEALLOCATE al_cursor
SET ANSI_WARNINGS ON

EXECUTE crosstab 1
-----------------------------------------------------------
I am getting multiple records for the same store
the result set is.

skey pan1_cd pan1_ba pan1_sd pan2_cd pan2_ba pan2_sd
1 12/2/04 400.0 3/4/05 NULL NULL NULL
1 NULL NULL NULL 5/6/04 566.00 3/4/04
I want the result set merged for each store.

Please help,

Thanks a million

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


You're probably looking for something like this - using MAX() and GROUP BY:

select skey,
max(case when ...) as A,
max(case when ...) as B,
....
from
....
where
....
group by skey

Simon
Jul 20 '05 #2

Simon,

That worked.
Thankyou very much for the solution.
Though it was simple,I was thinking about alternate solutions.I really
appreaciate you guys.

--Jay

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tim Pascoe | last post by:
I am using the Dynamic Cross-Tab code supplied in an article from SQL Server Magazine (http://www.winnetmag.com/SQLServer/Article/ArticleID/15608/15608.html). I modified the script to generate a...
1
by: Kevin Frey | last post by:
Hello, I have a test database with table A containing 10,000 rows and a table B containing 100,000 rows. Rows in B are "children" of rows in A - each row in A has 10 related rows in B (ie. B has...
2
by: Jaidev Paruchuri | last post by:
I have two tables 1)Rollout_detail start_date Datetime, contract_date Datetime, budget_amt Money store_id int(foriegn key referring store.store_id) pan_number varchar(20) roll_id...
5
by: Joško Šugar | last post by:
On this site: http://www.sommarskog.se/dynamic_sql.html I have found an example how to use cursor with dynamic SQL: DECLARE @my_cur CURSOR EXEC sp_executesql N'SET @my_cur = CURSOR FOR SELECT...
5
by: Todd Huish | last post by:
I have noticed something disturbing when retrieving datasets over a relatively slow line (multiple T1). I am looking at about 25 seconds to retrieve 500 rows via a php-odbc link. This same select...
1
by: david.joyce | last post by:
I am aware that v7 Static Scrollable Cursors do not work with pseudo-conversational CICS because of the temp work file being destroyed but will v8 Dynamic Scrollable Cursors work with...
3
by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
2
by: Łukasz W. | last post by:
Hello everybody! I have a small table "ABC" like this: id_position | value --------------------------- 1 | 11 2 | 22 3 | 33
4
ayanmitra2007mindtree
by: ayanmitra2007mindtree | last post by:
Consider I have five stored procedures viz. pr_sp1 (int_num1 IN number, int_num2 IN Number, v_cur OUT s_pkg.s_cur) pr_sp2 (int_num1 IN number, int_num2 IN Number, v_cur OUT s_pkg.s_cur) pr_sp3...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.