读书人

求1SELECT语句,立刻结帖.解决方法

发布时间: 2012-02-28 13:06:35 作者: rapoo

求1SELECT语句,立刻结帖.
货运公司 送货人
DHL LJM
UPS TJS
DHL SJ
UPS LHH
DHL GTM
...
----------------------
怎样得到以下结果.
----------------------
货运公司 出现率
DHL 3
UPS 2
...

[解决办法]
select 货运公司,[出现率]=count(*)
from table1 group by 货运公司
[解决办法]
create table #t3
(货运公司 varchar(50),
送货人 varchar(50)
)
insert into #t3
select 'DHL ', 'LJM ' union all select 'UPS ', 'TJS ' union all select 'DHL ', 'SJ ' union all select 'UPS ', 'LHH ' union all select 'DHL ', 'GTM '
select * from #t3


select 货运公司,count(distinct 送货人) 出现次数
from #t3
group by 货运公司


--------------------
DHL 3
UPS 2

[解决办法]
create table chinaooooo2008
(货运公司 varchar(50),
送货人 varchar(50)
)

insert into chinaooooo2008
select 'DHL ' , 'LJM ' union all
select 'UPS ' , 'TJS 'union all
select 'DHL ' , 'SJ 'union all
select 'UPS ' , 'LHH 'union all
select 'DHL ' , 'GTM '


select 货运公司,count(1) from chinaooooo2008

group by 货运公司


-----------------

货运公司

DHL 3
UPS 2

读书人网 >SQL Server

热点推荐