首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一条 SQL 话语不会写

2013-07-16 
一条 SQL 语句不会写大神们:一个公司,在不同的城市有很多家店 如下的查询出来的结果company_info(表1)C_ID

一条 SQL 语句不会写
大神们:
    一个公司,在不同的城市有很多家店 如下的查询出来的结果

company_info(表1)
C_IDCompany_Name            Company_Name_F
198宏图三胞高科技术有限公司HISAP
199国美电器控股有限公司Gome
200苏宁云商集团股份有限公司Suning
201美承(中国)投资有限公司One zero
202顺电连锁股份有限公司Sundan

 Shop_info(表2)
S_IDC_IDShop_NameCity_ID
10003198山西路宏图大厦店371
10004198南京国贸店371
10005198南京旗舰店371
10006198川沙店          370
10007198徐州王陵路店374
10008198芜湖中山路店441
10016198苏果联营店江宁分部371
10017198苏州亚细亚店372
10018198苏州阊胥路店372
10019198徐家汇旗舰店370


所以我想写个sql语句 查询出一下结果


           HISAP          Gome         Suning       Onezero        Sundan
城市(北京)  店数量         店数量       店数量        店数量           店数量
天津        5家             10家         6家           1家              4家
上海        3家             6家         29家          5家               7家
……

结果是手敲的! 谢谢了!  在线等! 

急急 SQL sql语句
[解决办法]
if object_id('[company_info]') is not null drop table [company_info]
go
create table [company_info] (C_ID int,Company_Name nvarchar(24),Company_Name_F nvarchar(12))


insert into [company_info]
select 198,'宏图三胞高科技术有限公司','HISAP' union all
select 199,'国美电器控股有限公司','Gome' union all
select 200,'苏宁云商集团股份有限公司','Suning'union all
select 201,'美承(中国)投资有限公司','One zero' union all
select 202,'顺电连锁股份有限公司','Sundan'

if object_id('[Shop_info]') is not null drop table [Shop_info]
go
create table [Shop_info] (S_ID int,C_ID int,Shop_Name nvarchar(18),City_ID int)
insert into [Shop_info]
select 10003,198,'山西路宏图大厦店',371 union all
select 10004,198,'南京国贸店',371 union all
select 10005,198,'南京旗舰店',371 union all
select 10006,198,'川沙店',370 union all
select 10007,198,'徐州王陵路店',374 union all
select 10008,198,'芜湖中山路店',441 union all
select 10016,198,'苏果联营店江宁分部',371 union all
select 10017,198,'苏州亚细亚店',372 union all
select 10018,198,'苏州阊胥路店',372 union all
select 10019,198,'徐家汇旗舰店',370

select * from [company_info]
select * from [Shop_info]


declare @sql varchar(8000)
select @sql = isnull(@sql + '],[' , '') + Company_Name_F from (SELECT A.Company_Name_F,b.city_id,COUNT(1) AS num
FROM [company_info] A
LEFT JOIN [Shop_info] B ON A.c_id =B.c_id
GROUP BY A.Company_Name_F,b.city_id)T group by Company_Name_F
set @sql = '[' + @sql + ']'
exec ('select * from (select * from (SELECT A.Company_Name_F,b.city_id,COUNT(1) AS num
FROM [company_info] A
LEFT JOIN [Shop_info] B ON A.c_id =B.c_id
GROUP BY A.Company_Name_F,b.city_id)T) a pivot (max(num) for Company_Name_F in (' + @sql + ')) b')

/*
city_idGomeHISAPOne zeroSundanSuning      --城市名称,lz再链接以下即可。
NULL1NULL111
370NULL2NULLNULLNULL
371NULL4NULLNULLNULL
372NULL2NULLNULLNULL
374NULL1NULLNULLNULL
441NULL1NULLNULLNULL*/


[解决办法]

SELECT city_id,sum(case when c_id='198' then ci_id else null end as hisap)hisap,
sum(case when c_id='199' then ci_id else null end as hisap)Gome,
sum(case when c_id='200' then ci_id else null end as hisap)Suning,


sum(case when c_id='201' then ci_id else null end as hisap)Onezero,
sum(case when c_id='202' then ci_id else null end as hisap)Sundan from Shop_info group by city_id


[解决办法]
行列转换问题,请参考:
http://blog.csdn.net/hdhai9451/article/details/5026933  

热点排行
Bad Request.