多表關聯彙總在MS SQL和ACCESS中的寫法

2021-04-12 19:08:20 字數 2326 閱讀 3076

--多表關聯彙總在ms sql和access中的寫法

--ms sql

--建立測試環境

create table ws_employee

(id int,

name varchar(10)

)insert into ws_employee

select 1,'a' union all

select 2,'b' union all

select 3,'c' union all

select 4,'d' union all

select 5,'e' union all

select 6,'f'

create table ws_jjgz

(id int,

員工id int,

price int

)insert into ws_jjgz

select 1,1,125 union all

select 2,2,122 union all

select 3,3,111 union all

select 4,1,212 union all

select 5,1,111 union all

select 6,2,121

create table ws_jsgz

(id int,

員工id int,

price int

)insert into ws_jsgz

select 1,1,125 union all

select 2,2,122 union all

select 3,3,111 union all

select 4,1,212 union all

select 5,1,111 union all

select 6,2,121

go--測試

select

id,name,

isnull(b.price,0) as 計件總工資,

isnull(c.price,0) as 計時總工資,

isnull(b.price,0) + isnull(c.price,0) as 總工資

from

ws_employee a

left join

(select 員工id, sum(price) as price from ws_jjgz group by 員工id) b

on a.id = b.員工id

left join

(select 員工id, sum(price) as price from ws_jsgz group by 員工id) c

on a.id = c.員工id

--刪除測試環境

drop table ws_employee, ws_jjgz, ws_jsgz

--結果

/*id name 計件總工資 計時總工資 總工資

1 a  448   448   896

2 b  243   243   486

3 c  111   111   222

4 d  0   0   0

5 e  0   0   0

6 f  0   0   0

*/--access

/*select

id,

name,

iif(isnull(b.sumprice), 0, b.sumprice) as 計件總工資,

iif(isnull(c.sumprice), 0, c.sumprice) as 計時總工資,

iif(isnull(b.sumprice), 0, b.sumprice) + iif(isnull(c.sumprice), 0, c.sumprice) as 總工資

from

(ws_employee as a

left join [select 員工id, sum(price) as sumprice from ws_jjgz group by 員工id]. as b on a.id = b.員工id)

left join [select 員工id, sum(price) as sumprice from ws_jsgz group by 員工id]. as c on a.id = c.員工id;

*//*

注意的地方

1. ms sql中的isnull在access中用iif代替,access中的isnull只是用來判斷是否為null

2. access中多個join,前乙個join的子句要用"()"括起來

3. access會自動給子查詢前後加上"",這時候"]"後面的"."不能刪除,否則會報錯

*/ 

多表關聯彙總在MS SQL和ACCESS中的寫法

多表關聯彙總在ms sql和access中的寫法 ms sql 建立測試環境 create table ws employee id int,name varchar 10 insert into ws employee select 1,a union all select 2,b union a...

Python報錯彙總(在須)

1 indentationerror unindent does not match any outer indentation level 縮排錯誤 未縮排與任何外部縮排級別不匹配 原因分析 存在相同等級的錯誤縮排,當copy的時候盡量注意相同等級是否是相同的縮排 解決辦法 按照錯誤提示,修改縮排...

多表關聯更新

用優惠表裡面的70006569的優惠的開始時間 來更新lik.temp yangmm 1115 discnt 的開始時間。這就出現問題了第乙個問題 同乙個使用者的70006569 優惠的開始時間可能有好幾個 取哪乙個?這就需要rank 函式來解決。第二個問題更新的時候會出現無法將null值插入.這個...