要求结果如下:
aaa ccc1
bbb ddd1
select colum1,max(colum3) into #tmp1 from Bgroup by colum1
select B.colum1,B.colum2 from B where colum1 in ( select colum1 from A)
create table B
(
stu varchar (3),
mm varchar (4),
dd int ,
)
insert into A(stu) values('aaa')
insert into A(stu) values('bbb')
insert into B(stu,mm,dd) values ('aaa','ccc2',2005)
insert into B(stu,mm,dd) values ('bbb','ddd1',2006)
insert into B(stu,mm,dd) values ('bbb','ddd2',2005)
insert into B(stu,mm,dd) values ('aaa','ccc1',2006)
select B.stu ,B.mm from B left join A on B.stu=A.stu where dd=2006 order by mm asc
declare @a table
(
col1 char(3)
)
insert into @a
select 'aaa' union all
select 'bbb'
declare @b table
(
col1 char(3),
col2 char(4),
col3 int
)
insert into @b
select 'aaa','ccc1',2006 union all
select 'aaa','ccc2',2005 union all
select 'bbb','ddd1',2006 union all
select 'bbb','ddd2',2005
select a.col1,a.col2 from @b a
left join (select col1,max(col1+cast(col3 as char(6))) as tt from @b group by col1)b on
a.col1=b.col1
where a.col1 in (select col1 from @a) and a.col1+cast(a.col3 as char(6))=b.tt
仅供参考!
要求结果如下:
aaa ccc1
bbb ddd1
select colum1,max(colum3) into #tmp1 from Bgroup by colum1
select B.colum1,B.colum2 from B where colum1 in ( select colum1 from A)
declare @a table
(
col1 char(3)
)
insert into @a
select 'aaa' union all
select 'bbb'
declare @b table
(
col1 char(3),
col2 char(4),
col3 int
)
insert into @b
select 'aaa','ccc1',2006 union all
select 'aaa','ccc2',2005 union all
select 'bbb','ddd1',2006 union all
select 'bbb','ddd2',2005
select a.col1,min(a.col2) as col2 from @b a
where a.col1 in (select col1 from @a)
group by a.col1
if exists(select 1 from sysobjects where object_id('A')=id and type='u')
drop table A
if exists(select 1 from sysobjects where object_id('B')=id and type='u')
drop table B
go
create table A(bh varchar(20) not null)
create table B(bh varchar(20) not null,content varchar(20) not null,year int not null)
go
insert A
select 'aaa' union all
select 'bbb'
insert B
select 'aaa','ccc1',2006 union all
select 'aaa','ccc2',2005 union all
select 'bbb','ddd1',2006 union all
select 'bbb','ddd2',2005
go
select * from A
/*
bh
aaa
bbb
*/
select * from B
/*
bh content year
aaa ccc1 2006
aaa ccc2 2005
bbb ddd1 2006
bbb ddd2 2005
*/
select a.bh,b.content from A join B on a.bh=b.bh join (select bh,max(year) year from B group by bh)c on b.bh=c.bh and b.year=c.year order by 1
/*
bh content
aaa ccc1
bbb ddd1
*/
insert into #tmp_a values ('aaa')
insert into #tmp_a values ('bbb')
insert into #tmp_b values ('aaa','ccc1','2006')
insert into #tmp_b values ('aaa','ccc2','2005')
insert into #tmp_b values ('bbb','ddd1','2006')
insert into #tmp_b values ('bbb','ddd2','2005')
select #tmp_a.data_field_1,#tmp_b.data_field_2 from #tmp_a,#tmp_b
where #tmp_a.data_field_1 = #tmp_b.data_field_1
and #tmp_b.data_field_3 in (select max(t.data_field_3) from #tmp_b,#tmp_b t where #tmp_b.data_field_1 = t.data_field_1)
order by #tmp_b.data_field_3 desc
create table B
(
stu varchar (3),
mm varchar (4),
dd int ,
)
insert into A(stu) values('aaa')
insert into A(stu) values('bbb')
insert into B(stu,mm,dd) values ('aaa','ccc2',2005)
insert into B(stu,mm,dd) values ('bbb','ddd1',2006)
insert into B(stu,mm,dd) values ('bbb','ddd2',2005)
insert into B(stu,mm,dd) values ('aaa','ccc1',2006)
select * from a
select * from b
select a.stu,b.mm from a left outer join b on a.stu = b.stu where dd=2006 order by mm asc