27,579
社区成员
发帖
与我相关
我的任务
分享
Create Table A(MID int,Persons int)
go
insert into A values(1,3)
insert into A values(2,1)
insert into A values(3,2)
insert into A values(55,55)
Create Table B(ID int,MID int, EmpID int, State int)
go
insert into B values(1, 1, 201, 1 )
insert into B values(2, 3, 222, 3)
insert into B values(3, 2, 180, 1 )
insert into B values(4, 55, 199, 2)
insert into B values(5, 1, 199, 2 )
insert into B values(6, 1, 222, 2 )
insert into B values(7, 3, 199, 1 )
select
A.MID
,count(1) as Persons
,sum(case when State = 1 then 1 else 0 end) as [按時到人数]
,sum(case when State = 2 then 1 else 0 end) as [未来参加人数]
,sum(case when State = 3 then 1 else 0 end) as [迟到早退人数]
from
B
join A on
A.MID = B.MID
group by
A.MID
drop table a
drop table b
/*MID Persons 按時到人数 未来参加人数 迟到早退人数
----------- ----------- ----------- ----------- -----------
1 3 1 2 0
2 1 1 0 0
3 2 1 0 1
55 1 0 1 0
(4 行受影响)
*/
if object_id('ta') is not null drop table ta
go
create table ta(MID int,Persons int)
insert into ta select
1, 3 union all select
2, 1 union all select
3, 2 union all select
55, 55
if object_id('tb') is not null drop table tb
go
create table tb(id int identity,MID int,EmpID int,State int)
insert into tb select
1, 201 ,1 union all select
3, 222 ,3 union all select
2, 180 ,1 union all select
55, 199 ,2 union all select
1, 199 ,2 union all select
1, 222 ,2 union all select
3, 199 ,1
select a.mid,persons=count(*),
按时到人数=sum(case when State=1 then 1 else 0 end),
未来参加人数=sum(case when State=2 then 1 else 0 end),
迟到人数=sum(case when State=3 then 1 else 0 end)
from ta a
left join tb b on a.mid=b.mid
group by a.mid
mid persons 按时到人数 未来参加人数 迟到人数
----------- ----------- ----------- ----------- -----------
1 3 1 2 0
2 1 1 0 0
3 2 1 0 1
55 1 0 1 0
(4 行受影响)
SELECT
A.MID
,count(*) Persons
,sum(case when State = 1 then 1 else 0 end) as [按時到]
,sum(case when State = 2 then 1 else 0 end) as [未来参加]
,sum(case when State = 3 then 1 else 0 end) as [迟到早退]
FROM B INNER JOIN A ON
A.MID = B.MID
GROUP BY B.TrainID,A.TrainID,A.TrainPersons
select mid,count(*) as Persons,
sum(case when State=1 then 1 else 0 end) as 按时到人数, sum(case when State=2 then 1 else 0 end) as 未来参加人数, sum(case when State=3 then 1 else 0 end) as 迟到人数
from B
group by mid
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-10-12 15:30:24
declare @A table([MID] int,[Persons] int)
Insert @A
select 1,3 union all
select 2,1 union all
select 3,2 union all
select 55,55
declare @B table([ID] int,[MID] int,[EmpID] int,[State] int)
Insert @B
select 1,1,201,1 union all
select 2,3,222,3 union all
select 3,2,180,1 union all
select 4,55,199,2 union all
select 5,1,199,2 union all
select 6,1,222,2 union all
select 7,3,199,1
select a.mid,
sum(case when State = 1 then 1 else 0 end) as N'按時到',
sum(case when State = 2 then 1 else 0 end) as N'未来参加',
sum(case when State = 3 then 1 else 0 end) as N'迟到早退'
from @A a,@B b where a.mid=b.mid group by a.mid
/*
(7 個資料列受到影響)
mid 按時到 未来参加 迟到早退
----------- ----------- ----------- -----------
1 1 2 0
2 1 0 0
3 1 0 1
55 0 1 0
(4 個資料列受到影響)
*/
-------------------------------------
-- Author : liangCK 梁爱兰
-- Comment: 小梁 爱 兰儿
-- Date : 2009-10-12 15:32:37
-------------------------------------
--> 生成测试数据: @tb1
DECLARE @tb1 TABLE (MID int,Persons int)
INSERT INTO @tb1
SELECT 1,3 UNION ALL
SELECT 2,1 UNION ALL
SELECT 3,2 UNION ALL
SELECT 55,55
--> 生成测试数据: @tb2
DECLARE @tb2 TABLE (ID int,MID int,EmpID int,State int)
INSERT INTO @tb2
SELECT 1,1,201,1 UNION ALL
SELECT 2,3,222,3 UNION ALL
SELECT 3,2,180,1 UNION ALL
SELECT 4,55,199,2 UNION ALL
SELECT 5,1,199,2 UNION ALL
SELECT 6,1,222,2 UNION ALL
SELECT 7,3,199,1
--SQL查询如下:
SELECT A.*,ISNULL(B.按时到人数,0) as 按时到人数,
ISNULL(B.未来参加人数,0) as 未来参加人数,
ISNULL(B.迟到早退,0) AS 迟到早退
FROM @tb1 AS A
LEFT JOIN (SELECT MID,SUM(CASE WHEN State=1 THEN 1 ELSE 0 END) AS 按时到人数,
SUM(CASE WHEN State =2 THEN 1 ELSE 0 END) AS 未来参加人数,
SUM(CASE WHEN State=3 THEN 1 ELSE 0 END) AS 迟到早退
FROM @tb2 GROUP BY MID) AS B
ON A.MID = B.MID
/*
MID Persons 按时到人数 未来参加人数 迟到早退
----------- ----------- ----------- ----------- -----------
1 3 1 2 0
2 1 1 0 0
3 2 1 0 1
55 55 0 1 0
(4 行受影响)
*/
if object_id('[B]') is not null drop table [B]
go
create table [B]([ID] int,[MID] int,[EmpID] int,[State] int)
insert [B]
select 1,1,201,1 union all
select 2,3,222,3 union all
select 3,2,180,1 union all
select 4,55,199,2 union all
select 5,1,199,2 union all
select 6,1,222,2 union all
select 7,3,199,1
select
MID,
count(1) as Persons,
sum(case when State=1 then 1 else 0 end) as 按时到人数,
sum(case when State=2 then 1 else 0 end) as 未来参加人数,
sum(case when State=3 then 1 else 0 end) as 迟到人数
from
B
group by
MID
--测试结果:
/*
MID Persons 按时到人数 未来参加人数 迟到人数
----------- ----------- ----------- ----------- -----------
1 3 1 2 0
2 1 1 0 0
3 2 1 0 1
55 1 0 1 0
(4 行受影响)
*/