dbTalk Databases Forums  

CROSS TAB query

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss CROSS TAB query in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Venkata
 
Posts: n/a

Default CROSS TAB query - 01-25-2004 , 05:58 PM






Hello SQL Gurus,

There are 3 tables x ,Y AND XY

i.e.,
X is one Dimenstion table
Y is another Dimension table
By Joining the Row and the Column dimension
table we get the cell which we need to check
in the XY table whether that data exists or not
If it exists then display the description
otherwise leave it blank

X table contains 5 records

x1
x2
x3
x4
x5

Y table contains 4 records

y1
y2
y3
y4


XY table may contain

x1y1
x2y3
x4y4



I need to get the Output as follows

y1 y2 y3 y4

x1 x1y1 NULL NULL NULL

x2 NULL NULL x2y3 NULL

x3 NULL NULL NULL NULL

x4 NULL NULL NULL x4y4

x5 NULL NULL NULL NULL


I am not expecting the complete solution
atleast some idea how to go through it
so that I can go forward .


Thanks in Advance
Have a Nice time

Regards
Venkata


Reply With Quote
  #2  
Old   
Valmir Meneses
 
Posts: n/a

Default RE: CROSS TAB query - 01-26-2004 , 11:31 AM






Hi venkata
See if this might give you an idea

REATE PROCEDURE [DBO].[CRIA CUBO] A

Set NoCount O
declare @t_NAME char(50
declare @Csql char(250

DECLARE TIPO_CHAMADA CURSOR FO
Select Distinct RTRIM([Tipo de Chamada]
From TMP_CUBE_ORIGINAL

OPEN TIPO_CHAMAD
DROP TABLE TMP_CUB
SELECT Site,Tipo_Linha,[Nr Origem],[Grand Total] INTO TMP_CUBE FROM TMP_CUBE_ORIGINAL WHERE [Tipo de Chamada] IS NUL

FETCH NEXT FROM TIPO_CHAMADA into @t_nam
WHILE @@FETCH_STATUS =
BEGI
SELECT @Csql = ' ALTER TABLE TMP_CUBE ADD [' + RTRIM(@t_name) +'] [MONEY] NULL
execute(@Csql
FETCH NEXT FROM TIPO_CHAMADA into @t_nam
EN
CLOSE TIPO_CHAMAD

OPEN TIPO_CHAMAD

FETCH NEXT FROM TIPO_CHAMADA into @t_nam
WHILE @@FETCH_STATUS =
BEGI
SELECT @Csql = 'UPDATE TMP_CUBE SET [' + RTRIM(@t_name) +'] = B.[Grand Total] FROM TMP_CUBE A,TMP_CUBE_ORIGINAL B WHERE A.SITE=B.SITE AND A.TIPO_LINHA=B.TIPO_LINHA AND A.[Nr Origem]=B.[Nr Origem] and B.[Tipo de Chamada]='''+ RTRIM(@t_name) +'''
Execute(@Csql
FETCH NEXT FROM TIPO_CHAMADA into @t_nam
EN

DEALLOCATE TIPO_CHAMAD
G



----- Venkata wrote: ----

Hello SQL Gurus

There are 3 tables x ,Y AND XY

i.e.
X is one Dimenstion table
Y is another Dimension tabl
By Joining the Row and the Column dimension
table we get the cell which we need to check
in the XY table whether that data exists or no
If it exists then display the descriptio
otherwise leave it blan

X table contains 5 records

x
x
x
x
x

Y table contains 4 record

y
y
y
y


XY table may contain

x1y
x2y
x4y



I need to get the Output as follow

y1 y2 y3 y4

x1 x1y1 NULL NULL NUL

x2 NULL NULL x2y3 NUL

x3 NULL NULL NULL NUL

x4 NULL NULL NULL x4y

x5 NULL NULL NULL NUL


I am not expecting the complete solution
atleast some idea how to go through it
so that I can go forward


Thanks in Advanc
Have a Nice tim

Regard
Venkat



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.