這個SQL怎麼寫

時間 2022-05-12 15:40:11

1樓:賓士

主要過程如下:

1、查詢t2表中按照【field001】分組的【field002】和

select field001,sum(field002) as field002 from t2 group by field001;

2、查詢t3表中按照【field001】分組的【field002】和

select field001,sum(field002) as field002 from t3 group by field001;

3、將上面兩個查詢結果作為結果集和 t1 相關聯,求出最終結果:

select t1.field001,t2.field002,t3.field002

from t1,

(select field001,sum(field002) as field002 from t2 group by field001) t2,

(select field001,sum(field002) as field002 from t3 group by field001) t3

where t1.field001=t2.field001

and t1.field001=t3.field001;

說明:這個問題很多人很可能會想把三個表關聯起來,但是請注意,由於t2表t3表中的【field001】是【多對多關係】,直接關聯會產生【笛卡爾積】從而不會達到預期的結果。

---以上,希望對你有所幫助。

2樓:

我要統計出以下結果

a 5 2

b 2 7

---------

--在t2\t3不存在時用0填充

select

a.field002 as t1_field002,

isnull(b.field002,0) as t2_field002,

isnull(c.field002,0) as t3_field002

from t1 as a

left join (select field001,sum(field002) as field002 from t2 group by field001)b on a.field001=b.field002

left join (select field001,sum(field002) as field002 from t3 group by field001)c on a.field001=c.field002

3樓:匿名使用者

select t1.field002,test1.sum1,test2.sum2 from t1

inner join

(select field001,sum(field002) as sum1 from t2 group by field001) test1

on t1.field001=test1.field001

inner join

(select field001,sum(field002) as sum2 from t3 group by field001) test2

on t1.field001=test2.field001;

補充一下:試驗情況

sql@kokooa>select t1.field002,test1.sum1,test2.sum2 from t1

2 inner join

3 (select field001,sum(field002) as sum1 from t2 group by field001) test1

4 on t1.field001=test1.field001

5 inner join

6 (select field001,sum(field002) as sum2 from t3 group by field001) test2

7 on t1.field001=test2.field001;

field002 sum1 sum2

---------- ---------- ----------

101 5 2

102 2 7

為了方便 a,bjiu用101,102代替了

4樓:

mysql下估計能運動。。 orcal肯定不行。...差不多這意思。我也沒仔細去寫

(select distinct(field001) as field001 from t1) a ((select distinct(field001) as field001 from t1) b left join

(select t2.field001,sum(field002) from t1,t2 where t1.field001=t2.

field001 group by t2.field001) c where b.field001 =c.

field001 ) left join on

(select t2.field001,sum(field002) from t1,t3 where t1.field001=t3.

field001 group by t3.field001) d where a.field001 =d.

field001 )

5樓:匿名使用者

select a.field002,sum(b.field002) ,sum(c.field002) from t1 a,t2 b,t3 c

where a.field001=b.field001 and c.field001=a.field001

group by a.field002

三表聯查一下就行了.

6樓:

select a.field002, b.sum1, c.sum2from t1 a

join (select field001, sum(field002) as sum1 from t2) b on a.field001=b.field001

join (select field001, sum(field002) as sum2 from t3) c on a.field001=c.field001

7樓:匿名使用者

select t1.field002,a.sum ,b.

sumfrom t1,(select field001,sum(field002) sum from t3 groupb by field001) a,

(select field001,sum(field002) sum from t2 groupb by field001) b

where a.field001=b.field001 and t1.field001=a.field001;

8樓:盒子裡的房間

select field002,(select isnull(sum(field002),0) from t2 where field001 = t1.field001),select isnull(sum(field002),0) from t3 where field001 = t1.field001)

from t1

9樓:

select a.field001,b.s1,c.s2from t1 a,(select field001,sun(field002) s1

from t2 group by field001) b,(select field001,sun(field002) s2

from t3 group by field001) cwhere a.field001=b.field001 and b.field001=c.field001

這個sql語句怎麼寫?怎麼寫sql的語句?

select id,userid,platform,time,reward serven,reward thirty from kids activity order by reward thirty desc不知道你是不是這個意思。按,reward thirty排序。select count t2...

oracle資料庫,這個sql應該怎麼寫

如果只是單純這幾條資料的話 select t.id,max case when rn 1 then 手術名 end 第一次手術,max case when rn 1 then 手術時間 end 第一次手術時間,max case when rn 1 then 記錄時間 end 第一次記錄時間,max ...

sql語句 包含怎麼寫,SQL語句 包含怎麼寫

使用sql 萬用字元可以替代一個或多個字元,即模糊查詢,也就是包含關係。sql 萬用字元必須與 like 運算子一起使用。在 sql 中,可使用以下萬用字元如下 1 替代一個或多個字元 2 僅替代一個字元 3 charlist 字元列中的任何單一字元 4 charlist 或者 charlist 不...