日期:2014-05-17 浏览次数:20599 次
create table T_A
(
Cid int,
CompanyName varchar (30)
)
create Table T_B
(
id int ,
Cid int,
Name varchar(50)
)
insert into T_A values ('1','百度')
insert into T_A values ('2','新浪')
insert into T_B values ('1','1','张三')
insert into T_B values ('2','2','李四')
insert into T_B values ('3','1','王五')
insert into T_B values ('4','2','赵六')
select distinct A.Cid,A.CompanyName,
COUNT(B.id) over (partition by A.Cid)
from T_A A inner join T_B B on A.Cid=B.Cid
select A.*,B.number
from T_A A,
(select Cid,COUNT(1) as number from T_B group by Cid ) B
where A.Cid=B.Cid
USE test
GO
-->生成表tb_Master
if object_id('tb_Master') is not null
drop table [tb_Master]
Go
Create table [tb_Master](id smallint,[companyname] nvarchar(2))
Insert into [tb_Master]
Select 1,N'百度'
Union all Select 2,N'新浪'
Go
-->生成表tb_s1
if object_id('tb_s1') is not null