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.
Author |
Topic |
roy_kty
Starting Member
29 Posts |
Posted - 2006-09-20 : 23:59:46
|
Dear all,I use the crosstab function but face the follow problemI have records 1,2,3,4,5 and they have types A,B,C,D,E now I have data 1 A1 A2 A1 B1 B2 B4 C5 Dso the result is : A B C D1 2 2 0 02 1 1 0 04 0 0 1 05 0 0 0 1However the 3 and E cannot show as the data not exist in the table so how can I show the record 3 and E that A B C D E1 2 2 0 0 02 1 1 0 0 03 0 0 0 0 04 0 0 1 0 05 0 0 0 1 0if I want to add a column in the store procedure?? Moreover, how to I call store procedure in view or functions??Thx for yours answer!! |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-21 : 00:10:37
|
[code]declare @table table( col1 int, col2 char(1))insert into @tableselect 1, 'A' union allselect 1, 'A' union allselect 2, 'A' union allselect 1, 'B' union allselect 1, 'B' union allselect 2, 'B' union allselect 4, 'C' union allselect 5, 'D'select num, [A] = sum(case when col2 = 'A' then 1 else 0 end), [B] = sum(case when col2 = 'B' then 1 else 0 end), [C] = sum(case when col2 = 'C' then 1 else 0 end), [D] = sum(case when col2 = 'D' then 1 else 0 end), [E] = sum(case when col2 = 'E' then 1 else 0 end) from ( select 1 as num union all select 2 union all select 3 union all select 4 union all select 5 ) n left join @table t on n.num = t.col1group by num/* RESULTnum A B C D E ----------- ----------- ----------- ----------- ----------- ----------- 1 2 2 0 0 02 1 1 0 0 03 0 0 0 0 04 0 0 1 0 05 0 0 0 1 0(5 row(s) affected)*/[/code] KH |
|
|
roy_kty
Starting Member
29 Posts |
Posted - 2006-09-25 : 04:17:35
|
Thx for your ans!!I try try sin |
|
|
roy_kty
Starting Member
29 Posts |
Posted - 2006-09-25 : 04:52:03
|
However I cannot insert the code into the view as it does not support CASE=.=""""how can I solve this problem!!Really thx for your answer!! |
|
|
roy_kty
Starting Member
29 Posts |
Posted - 2006-09-25 : 04:54:07
|
Moreover I use MS SQL 2000 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-25 : 05:00:57
|
Post what you have tried till now ?Chirag |
|
|
roy_kty
Starting Member
29 Posts |
Posted - 2006-09-25 : 21:59:13
|
I have tried the sql khtan given and it normal run in sql query,however when I put it into view it cannot work as view don't support CASE WHEN statement,So I try to use Openquery to try the sql statement however it have the errorServer: Msg 7411, Level 16, State 1, Line 1Server 'MyServer' is not configured for DATA ACCESS.Which MyServer is one of the data server in my MSSQLthen I try to set its optionexec sp_serveroption '<MyServer>', 'data access', 'true'however it occur error :Server: Msg 15015, Level 16, State 1, Procedure sp_serveroption, Line 32The server '<MyServer>' does not exist. Use sp_helpserver to show available servers.I used sp_helpserver but the server exist in the first record.By the way, I login into MyServer to execute the sqlSo now I cannot solve the problem.Moreover I don't really want to change the server option as I afraid some security problemThx for all replies!! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-26 : 01:56:53
|
"however when I put it into view it cannot work as view don't support CASE WHEN statement,"Can you post your query ? KH |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-26 : 02:06:50
|
quote: however when I put it into view it cannot work as view don't support CASE WHEN statement
View does support Case When EndPost what you are trying to code in your view... Openquery , is normally used when you want to retrive records from the different servers... why are you using the OpenQuery.. BTW, you are getting that error since you must have not registered your server for registering the server, check sp_addlinkedserver in BOL (Book online. SQL Server Help)Chirag |
|
|
|
|
|
|
|