22,302
社区成员




with A(ID,会员名)
as
(
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'
),
B(ID,会员名,分享号)
as
(
select 1,'张三','张小山1' union all
select 2,'张三','张小山2' union all
select 3,'李四','李小四1' union all
select 4,'李四','李小四2' union all
select 5,'王五','王小五1' union all
select 6,'王五','王小五2'
),
C(ID,会员名,分享号,使用时间 )
as
(
select 1,'张三','李小四1','2015-9-10' union all
select 2,'张三','李小四1','2015-8-10' union all
select 3,'张三','王小五2','2015-9-7' union all
select 4,'李四','张小山1','2015-8-19' union all
select 5,'李四','王小五2','2015-7-19' union all
select 6,'王五','李小四1','2015-8-4' union all
select 7,'王五','张小山1','2015-9-20'
)
select B.*,coalesce(t.使用时间,'--') as 使用时间
from B
left join
(
select 会员名,分享号,使用时间,
row_number() over(partition by 会员名,分享号 order by 使用时间 desc) rn
from C
where 会员名='张三'
)t
on b.分享号 = t.分享号
and t.rn = 1
/*
ID 会员名 分享号 使用时间
1 张三 张小山1 --
2 张三 张小山2 --
3 李四 李小四1 2015-9-10
4 李四 李小四2 --
5 王五 王小五1 --
6 王五 王小五2 2015-9-7
*/
create table A表
(ID int,会员名 varchar(10))
insert into A表
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'
create table B表
(ID int,会员名 varchar(10),分享号 varchar(10))
insert into B表
select 1,'张三','张小山1' union all
select 2,'张三','张小山2' union all
select 3,'李四','李小四1' union all
select 4,'李四','李小四2' union all
select 5,'王五','王小五1' union all
select 6,'王五','王小五2'
create table C表
(ID int,会员名 varchar(10),分享号 varchar(10),使用时间 date)
insert into C表
select 1,'张三','李小四1', '2015-9-10' union all
select 2,'张三','李小四1','2015-8-10' union all
select 3,'张三','王小五2','2015-9-7' union all
select 4,'李四','张小山1','2015-8-19' union all
select 5,'李四','王小五2','2015-7-19' union all
select 6,'王五','李小四1','2015-8-4' union all
select 7,'王五','张小山1','2015-9-20'
declare @UserName varchar(10)
select @UserName='张三'
select b.*,
isnull(convert(varchar(10),c.使用时间,23),'--') '使用时间'
from B表 b
left join(select 分享号,
max(使用时间) '使用时间'
from C表
where 会员名=@UserName
group by 分享号 ) c on b.分享号=c.分享号
/*
ID 会员名 分享号 使用时间
----------- ---------- ---------- ----------
1 张三 张小山1 --
2 张三 张小山2 --
3 李四 李小四1 2015-09-10
4 李四 李小四2 --
5 王五 王小五1 --
6 王五 王小五2 2015-09-07
(6 row(s) affected)
*/
with A(ID,会员名)
as
(
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'
),
B(ID,会员名,分享号)
as
(
select 1,'张三','张小山1' union all
select 2,'张三','张小山2' union all
select 3,'李四','李小四1' union all
select 4,'李四','李小四2' union all
select 5,'王五','王小五1' union all
select 6,'王五','王小五2'
),
C(ID,会员名,分享号,使用时间 )
as
(
select 1,'张三','李小四1','2015-9-10' union all
select 2,'张三','李小四1','2015-8-10' union all
select 3,'张三','王小五2','2015-9-7' union all
select 4,'李四','张小山1','2015-8-19' union all
select 5,'李四','王小五2','2015-7-19' union all
select 6,'王五','李小四1','2015-8-4' union all
select 7,'王五','张小山1','2015-9-20'
)
select b.*,isnull(c.使用时间,'--')使用时间 from B b left join
(
select 会员名,分享号,MAX(使用时间)使用时间 from C where 会员名='张三' group by 会员名,分享号
) c on b.分享号=c.分享号
结果
ID 会员名 分享号 使用时间
----------- ---- ------- ---------
1 张三 张小山1 --
2 张三 张小山2 --
3 李四 李小四1 2015-9-10
4 李四 李小四2 --
5 王五 王小五1 --
6 王五 王小五2 2015-9-7
思路是这样的,先找出张三最后使用分享号的时间,当然是根据会员号和分享号分组的,得到结果后再跟表B链接查询