當前位置:首頁 » 編程語言 » sql把兩個不同表的數據相除
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql把兩個不同表的數據相除

發布時間: 2023-04-07 10:34:53

1. sqlServer中SQL語句如何對兩個group By後的查詢結果進行相除

你錯誤的問題點在於兩個子查詢之間沒有任何關聯,不能直接除。首先我們要建立關聯關系。再除
select a.值1,(case when isnull(b.值2,0)=0 then 0 else a.值1/b.值2) ---記得除數為0處理
from (select 值2,SUM(值1) AS 值1 from 表1 group by 值2) a
innor join (看情況使用innor join 還是 full 還是Left)
(select 值2,sum(值1) AS 值1 from 表2 group by 值2) b on A.值2=b.值2

2. SQL 語句刪除問題同時刪除兩個表內關聯的數據

一個sql語句是沒辦法執行兩個刪除操作,如果你要實現上面的功能,有以下幾個選擇:
1.用外鍵關聯刪除,把B表的uid設成外鍵關聯A表的ID,並關聯刪除操作
2.用存儲過程,用事務來處理實現;

望採納!

3. sql server 2個查詢結果相除

declare @a float
declare @b float
select @a= count(*) from table1
select @b = count(*) from table2
select @a/@b

樓上的方法,如果只是COUNT條數,沒有小數點啊。。。 要轉格式:
select a.a/b.b from
(select cast(count(*)as float) as a from table1) as a,
(select cast(count(*)as float) as b from table2) as b

4. SQL如何做除法

SQL做除法的步驟:

select

t.[origin-destination],t.[SH/LANE/MOT] /(select count(1) from ['TMS$'])ASPERCENTAGEFROM (代碼1) t

group by [origin-destination],t.[SH/LANE/MOT]

having t.[SH/LANE/MOT] /count(*) <= 0.01

註:兩個count都是int,相除會沒有小數部分,所以應該都給轉成帶小數的數。

cast as numeric(10,4) 。

(4)sql把兩個不同表的數據相除擴展閱讀


例題,表明為chuqinqk 列名 遲到 1(次)一個月的總天數為30 求遲到率。

select * from table1 where 工資>2500 and 工資<3000 //同上

select 姓名 from table1 where 性別='0' and 工資='4000'

select * from table1 where not 工資= 3200

select * from table1 order by 工資desc //將工資按照降序排列

select * from table1 order by 工資 asc //將工資按照升序排列

select * from table1 where year(出身日期)=1987 //查詢table1 中所有出身在1987的人select * from table1 where name like

'%張' /'%張%' /'張%' //查詢1,首位字『張』3,尾位字『張』2,模糊查詢

select * from table1 order by money desc //查詢表1按照工資的降序排列表1 (升序為asc)

select * from table1 where brithday is null //查詢表1 中出身日期為空的人

SQL語言,是結構化查詢語言(StructuredQueryLanguage)的簡稱。

5. sql 兩個表記錄數相除

可以試下如下方法:
select table1.count(*)*1.00/table2.count(*) as 百分比
from table1, table2
另處,你的方法在sql server中可以的話,一般來說在access中也可用,兩種資料庫都是完全遵守SQL語法標準的。只是一些規則存儲過程等無法移植。

6. SQL如何做除法

這樣:

select

t.[origin-destination],t.[SH/LANE/MOT] /(select count(1) from ['TMS$'] )ASPERCENTAGE
FROM (代碼1) t

group by [origin-destination],t.[SH/LANE/MOT]

having t.[SH/LANE/MOT] /count(*) <= 0.01

註:兩個count都是int,相除會沒有小數部分,所以應該都給轉成帶小數的數。

cast as numeric(10,4) 。

(6)sql把兩個不同表的數據相除擴展閱讀:

SQL中除法運算的實現

R(X,Y)÷S(Y,Z)的運算用結構化語言SQL 語句可表達為下列形式:

select distinct R.X from R R1

where not exists

(

select S.Y from S

where not exists

(

select * from R R2

where R2.X=R1.X and R2.Y=S.Y

)

)

7. sql語句的寫法——把兩個數相除(Oracle)

select (select count(*) as a from...)/(select count(*) as b from...
) from al

8. sql統計兩個表數據然後相除,怎麼寫

要相除的是什麼?某個欄位的統計還是數據量統計還是其他的?

selectt1.a/t2.bfrom(
(selectcount(1)afromtable1)t1,
(selectcount(1)bfromtable2)t2
);