473,672 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Cross Tab Using Cursors.

I have two tables
1)Rollout_detai l
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_numb er + store ) are unique.

Now here is the problem.

I need to generate a cross tab report with
store,pan1_cont ract_date,pan1_ budget_amt,pan1 _start_date,
pan2_Contract_d ate,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_i d
OPEN al_cursor
SELECT @sql='SELECT skey, '
FETCH NEXT FROM al_cursor INTO @panNumber
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql=@sql+'(CAS E pan_number WHEN
'''+rtrim(@panN umber)+''' THEN start_date END) as
P'+rtrim(@panNu mber)+'_sd,(CAS E pan_number WHEN
'''+rtrim(@panN umber)+''' THEN budget_amt END) as
P'+rtrim(@panNu mber)+'_ba,(CAS E pan_number WHEN
'''+rtrim(@panN umber)+''' THEN contract_date END) as
P'+rtrim(@panNu mber)+'_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.s tore_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 4083

"Jaidev Paruchuri" <ja****@hotmail .com> wrote in message
news:40******** *************@n ews.frii.net...
I have two tables
1)Rollout_detai l
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_numb er + store ) are unique.

Now here is the problem.

I need to generate a cross tab report with
store,pan1_cont ract_date,pan1_ budget_amt,pan1 _start_date,
pan2_Contract_d ate,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_i d
OPEN al_cursor
SELECT @sql='SELECT skey, '
FETCH NEXT FROM al_cursor INTO @panNumber
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @sql=@sql+'(CAS E pan_number WHEN
'''+rtrim(@panN umber)+''' THEN start_date END) as
P'+rtrim(@panNu mber)+'_sd,(CAS E pan_number WHEN
'''+rtrim(@panN umber)+''' THEN budget_amt END) as
P'+rtrim(@panNu mber)+'_ba,(CAS E pan_number WHEN
'''+rtrim(@panN umber)+''' THEN contract_date END) as
P'+rtrim(@panNu mber)+'_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.s tore_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
7509
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 temp table inside the stored procedure, and then use this temp table as the source for the cross-tab. However, the problem seems to be that the dynamic SQL string generated by the script is longer than what can be stored in the @SQL variable. The...
1
7405
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 a foreign key to A). Using ODBC I am executing the following loop 10,000 times, expressed below in pseudo-code: "select * from A order by a_pk option (fast 1)"
2
1298
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 int 2)store
5
18654
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 name FROM dbo.sysobjects; OPEN @my_cur', N'@my_cur cursor OUTPUT', @my_cur OUTPUT FETCH NEXT FROM @my_cur
5
6181
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 from the cli is for all intents practicaly instantaneous. After much research I discovered that PHP by default uses a dynamic cursor type which can be quite a bit slower than a forward only cursor. BTW I have been searching forward only/read...
1
3039
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 pseudo-conversational CICS due to these cursors directly using the base table?
3
6819
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 figure out, if there is a way of using polymorphic way (if that is a word) of doing this particular property. A sample of my code is as follows: DynamicControls.ButtonControl(this,btnSearchByName, new Point(5, 75), new Size(95, 20),...
2
2234
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
3767
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 (int_num1 IN number, int_num2 IN Number, v_cur OUT s_pkg.s_cur) pr_sp4 (int_num1 IN number, int_num2 IN Number, v_cur OUT s_pkg.s_cur) pr_sp5 (int_num1 IN number, int_num2 IN Number, v_cur OUT s_pkg.s_cur) If you see, all these SPs are of same...
0
8406
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8609
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8683
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2821
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.