select b.c_No,max(i_Type) i_Type,b.i_Value from tb_table1 a,tb_table2 b where a.c_No=b.c_No and a.i_Value=b.i_Value group by b.c_No,b.i_Value
select b.* from tb_table2 b where b.i_Type=(select top 1 i_Type from tb_table2 where c_No=b.c_No order by i_Type desc)
--try select c_No, i_Value number=(select top 1 i_Type from tb_table2 where c_No=a.c_No and i_Value=a.i_Value) from tb_table2 a where c_No in (select distinct c_No from tb_table1 ) group by c_No,i_Value
select b.c_No,max(i_Type) i_Type,b.i_Value from tb_table1 a,tb_table2 b where a.c_No=b.c_No and a.i_Value=b.i_Value group by b.c_No,b.i_Value
--創建表 create table tb_table1(c_No nvarchar(10),i_Value float) create table tb_table2(c_No nvarchar(10),i_Type nvarchar(5),i_Value float)--查入數據 insert into tb_table1 select '1111111',1.26 union select '2222222',2.23insert into tb_table2 select '1111111','abc',1.26 union select '1111111','bcd',1.26 union select '1111111','dce',2.22 union select '2222222','dce',2.23--查詢 select B.c_No,max(i_Type) as i_Type,B.i_Value from tb_table1 A,tb_table2 B where A.c_No=B.c_No and A.i_Value=B.i_Value group by B.c_No,B.i_Value --結果 /* c_No i_Type i_Value 1111111 bcd 1.26 2222222 dce 2.23 */--drop表 drop table tb_table1 drop table tb_table2
b.*
from
tb_table2 b
where
b.i_Type=(select top 1 i_Type from tb_table2 where c_No=b.c_No order by i_Type desc)
select c_No,
i_Value
number=(select top 1 i_Type from tb_table2 where c_No=a.c_No and i_Value=a.i_Value)
from tb_table2 a
where c_No in
(select distinct c_No from tb_table1 )
group by c_No,i_Value
create table tb_table1(c_No nvarchar(10),i_Value float)
create table tb_table2(c_No nvarchar(10),i_Type nvarchar(5),i_Value float)--查入數據
insert into tb_table1
select '1111111',1.26 union
select '2222222',2.23insert into tb_table2
select '1111111','abc',1.26 union
select '1111111','bcd',1.26 union
select '1111111','dce',2.22 union
select '2222222','dce',2.23--查詢
select B.c_No,max(i_Type) as i_Type,B.i_Value
from tb_table1 A,tb_table2 B
where A.c_No=B.c_No
and A.i_Value=B.i_Value
group by B.c_No,B.i_Value --結果
/*
c_No i_Type i_Value
1111111 bcd 1.26
2222222 dce 2.23
*/--drop表
drop table tb_table1
drop table tb_table2