❶ sql中如何將兩個查詢結果相加
做個簡單的。
兩個數據表db1,db2
查詢結果A是從數據表db1獲取的:
select names, sale from db1
查詢結果B是從數據表db2獲取的:
select names, sale from db2
則查詢結果C:
select names,sum(sale) as sale
from
(select names, sale from db1
union
select names, sale from db2
)
group by names
❷ sql兩張表,如何將符合條件的值相加
createtableT1
(
idint,
時間1datetime,
值1int,
時間2datetime,
值2int,
時間3datetime,
值3int
)
createtableT2
(
idint,
[2015-1-1]int,
[2015-1-2]int,
[2015-1-3]int
)
insertintoT1values(1,'2015-1-1',23,'2015-2-3',21,'2015-3-9',21)
insertintoT1values(2,'2015-1-1',23,'2015-2-3',21,'2015-3-9',21)
insertintoT2values(1,45,56,13)
insertintoT2values(2,12,17,54)
--把T2列轉行,再和T1合計
withT
As
(
select*fromT2
unpivot
(
val
for
datin([2015-1-1],[2015-1-2],[2015-1-3])
)upvt
)
selectid,
時間1,值1+isnull((selectvalfromtwhereid=t1.idanddat=時間1),0)As值1,
時間2,值2+isnull((selectvalfromtwhereid=t1.idanddat=時間2),0)As值2,
時間3,值3+isnull((selectvalfromtwhereid=t1.idanddat=時間3),0)As值3
fromT1
❸ 如何用sql語句將資料庫表中欄位內容中的數值進行求和
我把邏輯演算法告訴你 ,語句你就自己寫了。用replace函數 去掉對應的字元
例如:select replace(replace(欄位,'尊敬……金額',''),'元……變化','')
使用兩次REPLACE 第一次去掉前面的字元,第二次去掉後面的字元,只剩下數字了,再格式化這串數字為數值,不然語句出來只是個字元串
❹ sql兩表連接後數據相加
select goods_name as good_name,sum(sale_price) as total_price
from goods inner join saleflow
on goods.goods_id = saleflow.good_id
group by goods_name;
你試試
❺ sql 兩個表數值相加
select nvl(A.價格,0)+nvl(B.價格,0)
from A full join B
where A.商品名=B.商品名
這個是oracle的
如果是其他的我也不知道好不好用
你可以
update A set 價格=0 where 價格 is NULL
update B set 價格=0 where 價格 is NULL
select A.價格+B.價格
from A,B
WHERE A.商品名=B.商品名
❻ 如何通過 SQL 語句實現兩行數據的相加
你可以把符合條件的匯總或是分類匯總
select sum(欄位名) f1 from 表名 where 條件
❼ sql語句查詢結構相同的兩個表,對應的表頭相加求和,怎麼寫
select *
into 新表名
from (select * from T1 union all select * from T2)
這個語句可以實現將合並的數據追加到一個新表中。
不合並重復數據
select * from T1 union all select * from T2
合並重復數據
select * from T1 union select * from T2
兩個表,表1 表2
如果要將 表1的數據並入表2用以下語句即可
insert into 表2(欄位1,欄位2) select 欄位1,欄位2 from b1
注意,必須把欄位名全部寫清楚,而且不允許把自動編號進去寫進去,要合並自動編號欄位必須重寫一個演算法一條一條記錄地加進去
1 insert into b1 select * from b2
2 select * into newtable from (select * from b1 union all select * from b2)
❽ sql兩個表數據求和
樓上的想法是這樣,先把倆張表的數據都查出來,使用union關鍵字,相應列使用同樣的同名。 這樣可以把倆張表當成一張表來操作,應該是可行的。
select t.name , t.brand , t.type, t.package , sum(t.totalcount), sum(t.weight) from (
select 商品名稱1 as name , 商品品牌1 as brand , 商品型號1 as type, 商品包裝1 as package , 商品數量1 as totalcount, 商品重量1 as weight from 商品表1 union all
select 商品名稱2 as name , 商品品牌2 as brand , 商品型號2 as type, 商品包裝2 as package , 商品數量2 as totalcount, 商品重量2 as weight from 商品表2
) t group by t.name ,t.brand , t.type, t.package
但是我不明白的是,你這是倆張表嗎,這是什麼樣的兩張表。。。 完全一樣的列,完全一樣的類型,干嗎要成兩張表。
❾ 怎麼把兩列的數據求和(先每列求和,再把結果再相加)sql資料庫
1、在數據中打開一個存在整數數值的表,然後可以看到右下角就有查看的表格數據。
❿ sql server 提取2個表的數據再累計相加
你的B表中,是怎麼存儲或表示某一個帶有「#」標識符的員工在每天是去了1、2結構還是去了2、3機構?你的B表應該還有其他欄位表示某個員工一天去了那些機構吧?還是表B是一天中所有員工上門服務的記錄表?
不知道下面是不是符合你的要求。
統計結果