日期:2014-05-19  浏览次数:20384 次

求一查询语句~
t1表
id         number     class_id
06               2               596
74               4               597
74               1               598
74               5               599
74               13             600
t2表
id         number       class_id
06               2                 596
74               1                 598
74               2                 599
74               4                 597
要得到如下结果,该如何写sql?
id         number       class_id
74               3                 599
74               13               600

------解决方案--------------------
74 3 599

这个是怎么出来的阿~~~~
------解决方案--------------------
看不明白.
楼主解释一下结果是怎么出来的.
------解决方案--------------------
74 13 600,这个是取最大.

74 3 599这个呢?
------解决方案--------------------
select id , max(number) number,max(class_id) class_id from t1 where id = 74 group by id
union all
select id , count(*) number,max(class_id) class_id from t1 where id = 74 group by id

------解决方案--------------------
create table t1 (id varchar(10), number int, class_id varchar(10))
insert into t1 select '06 ', 2, '596 '
union all select '74 ', 4, '597 '
union all select '74 ', 1, '598 '
union all select '74 ', 5, '599 '
union all select '74 ', 13, '600 '


create table t2( id varchar(10), number int, class_id varchar(10))
insert into t2 select '06 ', 2, '596 '
union all select '74 ', 1, '598 '
union all select '74 ', 2, '599 '
union all select '74 ', 4, '597 '


select max(a.id)id,isnull(avg(b.number),avg(a.number))number,max(a.class_id)class_id
from t1 a full join t2 b on b.class_id=a.class_id
group by isnull(b.number*0,a.class_id)


id number class_id
---------- ----------- ----------
74 2 599
74 13 600

(所影响的行数为 2 行)
------解决方案--------------------
try


Select
A.id,
A.number - IsNull(B.number, 0) As number,
A.class_id
From
t1 A
Left Join