Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How to Label my column in this crosstab?

Author  Topic 

Clages1
Yak Posting Veteran

69 Posts

Posted - 2007-07-27 : 14:40:28

Hi
This small CrossTab Works fine
but i have two questions

1 - How can i use @dep1 as label?
if you check in Script , the label is '001' not @dep1

2 - Can i get this result set as a Return table from a SP?

tks

Carlos Lages




Declare
@Dep1 Char(03),
@Dep2 Char(03),
@Dep3 Char(03)

set @Dep1 = '001'
set @Dep2 = '002'
set @Dep3 = '003'

SELECT
[Produto / Deposito]=CASE WHEN row IS NULL THEN 'Sum'
ELSE CONVERT(VARCHAR(80),[row]) END ,
[001] = SUM( CASE col WHEN @dep1 THEN data ELSE 0 END ),
[002] = SUM( CASE col WHEN @dep2 THEN data ELSE 0 END ),
[003] = SUM( CASE col WHEN @dep3 THEN data ELSE 0 END ),
[Total] = SUM( data )
FROM
(SELECT [row] = pro_codigo ,
[col] = dep_codigo ,
[data] = sum(pro_saldo_fis1)
FROM Produto INNER JOIN deposito
ON (produto.pro_deposito =deposito.dep_codigo)
GROUP BY pro_codigo , dep_codigo
) f
GROUP BY row WITH ROLLUP
ORDER BY GROUPING(row),total DESC



tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-07-27 : 15:24:37
If you want a dynamic label, then you'd need to use dynamic SQL which is not recommended. Is there a reason why you need the value of @Dep1 to be the label and that it can't be a static label?

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Clages1
Yak Posting Veteran

69 Posts

Posted - 2007-07-27 : 16:02:49
Because if in the CASE statetment i use @DEP1
label should by @Dep1

ex. @dep1 = '555'

the Case evaluate '555' to be true, then the label should by '555' too
otherwise i will have always the same label '001'

note that is a SP and @dep1 it is a Parameter.

tks
Carlos Lages
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-07-27 : 16:30:41
Are you displaying this in a front end application? Why not keep it as you are just have the application stick its own label on, using the value of @dep1?
Long and short is this sort of output contradicts relational and SQL concepts so you can't do it without dynamic SQL unless you get the FE to handle it.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-07-27 : 16:39:16
Just call the label Dep1 then. Handle the rest through your application as pootle_flump mentioned.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -