请问这个SQL怎么写,高手请进,跟帖有分

copico 2008-02-29 02:03:20
数据表是这样的
GC MODEL STATUS
---- ------------------------------ ------
GSM SGH-T108+ Z
CDMA DVD-P248A H
CDMA STW-CG72 C
CDMA STW-CG72 H
CDMA N70 A
GSM SGH-T108+ Z
GSM SGH-T108+ Z
GSM SGH-T108+ G

我怎么查出这样的结果
GC MODEL A B C D E F G H Z
GSM SGH-T108+ 0 2
CDMA N70 A ..........

谢谢了
...全文
390 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackeyabc 2008-03-03
  • 打赏
  • 举报
回复
不看,僅僅收藏而已
pmshuiyun 2008-03-03
  • 打赏
  • 举报
回复
不能re了
zengdukan 2008-03-02
  • 打赏
  • 举报
回复
太高手了
huayiluo 2008-03-01
  • 打赏
  • 举报
回复
.............
RongCha040 2008-03-01
  • 打赏
  • 举报
回复
在坛子里,二楼.三楼的代码见Ctrl + C 和 Ctrl + V 好多次了:P
hunhun02 2008-03-01
  • 打赏
  • 举报
回复
up
appletan 2008-03-01
  • 打赏
  • 举报
回复
做下表连接嘛
liuhua287 2008-02-29
  • 打赏
  • 举报
回复
????????????????
huzhenqi2008 2008-02-29
  • 打赏
  • 举报
回复
支持你了``
shenglei 2008-02-29
  • 打赏
  • 举报
回复
我是来接分的 o(∩_∩)o...哈哈
moonsoup 2008-02-29
  • 打赏
  • 举报
回复
mark
copico 2008-02-29
  • 打赏
  • 举报
回复
结帖了,,太感谢你们了,都是高手啊
kelph 2008-02-29
  • 打赏
  • 举报
回复
手慢跟贴混点分
xiaoliaoyun 2008-02-29
  • 打赏
  • 举报
回复

create table #a (CC varchar(10),MODEL varchar(20),STATUS char(1))
go
insert into #a values( 'GSM','SGH-T108+','Z')
insert into #a values('CDMA','DVD-P248A','H')
insert into #a values('CDMA','STW-CG72', 'C')
insert into #a values('CDMA','STW-CG72', 'H')
insert into #a values('CDMA','N70', 'A')
insert into #a values( 'GSM','SGH-T108+','Z')
insert into #a values( 'GSM','SGH-T108+','Z')
insert into #a values( 'GSM','SGH-T108+','G')

select * from #a

declare @sql varchar(8000)
select @sql = 'select CC,MODEL'

select @sql = @sql + ',sum(case when STATUS = ''' +STATUS+ ''' then 1 else 0 end) as '''+STATUS+''''
from (select distinct STATUS from #a) a

select @sql = @sql + ' from #a group by CC,MODEL'
exec (@sql)

drop table #a
wzy_love_sly 2008-02-29
  • 打赏
  • 举报
回复
create table tb(gc varchar(10),MODEL varchar(50),status varchar(10))
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'CDMA','DVD-P248A','h'
insert into tb select 'CDMA','STW-CG72','c'
insert into tb select 'CDMA','STW-CG72','h'
insert into tb select 'CDMA','N70','a'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','g'

select top 25 id=identity(int,0,1),status='a' into # from sysobjects a,sysobjects b
update # set status=char(ascii(status) +id)

declare @sql varchar(8000)
select @sql = isnull(@sql+',','') + 'sum(case when status=''' + status + ''' then 1 else 0 end) [' + status + ']'
from #
set @sql ='select gc,MODEL,'+ @sql + ' from tb group by gc,MODEL'
exec(@sql)


gc MODEL a b c d e f g h i j k l m n o p q r s t u v w x y
CDMA DVD-P248A 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CDMA N70 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
GSM SGH-T108+ 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CDMA STW-CG72 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
wzy_love_sly 2008-02-29
  • 打赏
  • 举报
回复
create table tb(gc varchar(10),MODEL varchar(50),status varchar(10))
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'CDMA','DVD-P248A','h'
insert into tb select 'CDMA','STW-CG72','c'
insert into tb select 'CDMA','STW-CG72','h'
insert into tb select 'CDMA','N70','a'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','z'
insert into tb select 'GSM','SGH-T108+','g'

declare @sql varchar(8000)
select @sql = isnull(@sql+',','') + 'sum(case when status=''' + status + ''' then 1 else 0 end) [' + status + ']'
from (select 'a' as status union all
select 'b' as status union all
select 'c' as status union all
select 'd' as status union all
select 'e' as status union all
select 'f' as status union all
select 'g' as status union all
select 'h' as status union all
select 'i' as status union all
select 'z' as status union all
select 'k' as status
) as a
set @sql ='select gc,MODEL,'+ @sql + ' from tb group by gc,MODEL'
exec(@sql)


gc MODEL a b c d e f g h i z k
CDMA DVD-P248A 0 0 0 0 0 0 0 1 0 0 0
CDMA N70 1 0 0 0 0 0 0 0 0 0 0
GSM SGH-T108+ 0 0 0 0 0 0 1 0 0 3 0
CDMA STW-CG72 0 0 1 0 0 0 0 1 0 0 0
copico 2008-02-29
  • 打赏
  • 举报
回复
太多了太多了
nzperfect 2008-02-29
  • 打赏
  • 举报
回复
很长久很长久的常见问题了.jf
dawugui 2008-02-29
  • 打赏
  • 举报
回复
--这是2000中用函数的方法,2005的方法见上.
create table tb(GC varchar(10), MODEL varchar(20), STATUS varchar(10))
insert into tb values('GSM' , 'SGH-T108+', 'Z')
insert into tb values('CDMA', 'DVD-P248A', 'H')
insert into tb values('CDMA', 'STW-CG72' , 'C')
insert into tb values('CDMA', 'STW-CG72' , 'H')
insert into tb values('CDMA', 'N70' , 'A')
insert into tb values('GSM' , 'SGH-T108+', 'Z')
insert into tb values('GSM' , 'SGH-T108+', 'Z')
insert into tb values('GSM' , 'SGH-T108+', 'G')
go

--创建一个合并的函数
create function f_hb(@GC varchar(10) , @MODEL varchar(20))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ' ' + cast(STATUS as varchar) from tb where GC = @GC and MODEL = @MODEL
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct GC , MODEL , dbo.f_hb(GC , MODEL) as STATUS from tb

drop table tb
drop function f_hb

/*
GC MODEL STATUS
---------- -------------------- -------
CDMA DVD-P248A H
CDMA N70 A
CDMA STW-CG72 C H
GSM SGH-T108+ Z Z Z G

(所影响的行数为 4 行)
*/
liangCK 2008-02-29
  • 打赏
  • 举报
回复
顶楼上这么多 位..接分.
加载更多回复(15)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧