K3

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
use Northwind;

if object_id('tb_Letters',N'U') is not null
begin
 drop table tb_Letters;
 end;

create table tb_Letters(
    letter char(1)
);

insert into tb_Letters select 'A';
insert into tb_Letters select 'B';
insert into tb_Letters select 'C';
insert into tb_Letters select 'D';
insert into tb_Letters select 'E';
insert into tb_Letters select 'F';
insert into tb_Letters select 'G';
insert into tb_Letters select 'H';
insert into tb_Letters select 'I';
insert into tb_Letters select 'J';
insert into tb_Letters select 'K';
insert into tb_Letters select 'L';
insert into tb_Letters select 'M';
insert into tb_Letters select 'N';
insert into tb_Letters select 'O';
insert into tb_Letters select 'P';
insert into tb_Letters select 'Q';
insert into tb_Letters select 'S';
insert into tb_Letters select 'T';
insert into tb_Letters select 'U';
insert into tb_Letters select 'V';
insert into tb_Letters select 'W';
insert into tb_Letters select 'X';
insert into tb_Letters select 'Y';
insert into tb_Letters select 'Z';

select C.letter CustomerNameStartWith, isnull(B.num,0) CustomersCount
from tb_Letters C
left join(
select CustomerNameStartWith, count(*) num
from(
    select
        left(customerid,1) CustomerNameStartWith
    from dbo.customers
    ) A
group by A.CustomerNameStartWith) B on C.letter = B.CustomerNameStartWith;

if object_id('tb_Letters',N'U') is not null
begin
 drop table tb_Letters;
 end;
posted on 2011-04-08 18:26  K3  阅读(281)  评论(0编辑  收藏  举报