當前位置:首頁 » 編程語言 » sql根據添加不同列匯總
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql根據添加不同列匯總

發布時間: 2023-02-27 02:00:32

1. sql server 分類匯總問題 如何針對單獨一列進行匯總

我看你這個不像是需要匯總的,你就按供應商排序,然後顯示的時候,程序計算一下數據,每個供應商增加一個合計行就可以了。

2. SQL 把不同列的號碼匯總代碼怎麼寫

這個不是匯總,就是欄位合並
只是你的SQL是什麼資料庫(有幾十個常用資料庫,它們都支持SQL語言的,但字串合並方法不同的),若是SQLServer(注意,SQL與SQLserver是不同的概念)
可用
select 員工姓名,手機號碼+'*'+電話號碼 可聯系號碼 from 你的表

3. SQL如何在union all多表查詢出結果後再新增列匯總

select 商品名,
sum(期初庫存),
sum(入庫數),
sum(消耗數),
sum(期初庫存) + sum(入庫數) - sum(消耗數) as 期末庫存
from (select 商品名,
期初庫存,
0 as 入庫數,
0 as 消耗數
from 期初庫存
union all
select 商品名,
0 as 期初庫存,
入庫數,
0 as 消耗數
from 入庫
union all
select 商品名,
0 as 期初庫存,
0 as 入庫數,
消耗數
from 消耗)
group by 商品名

4. sql 怎麼查詢出來的 弄一個匯總的列

查詢表格中的指定列數據,是select 語句其中一種語法
即:select columna,columnb from table

如表格table有列 a,b,c,d
1、select * from table = select a,b,c,d from table 即顯示表格式所以列
2、select a,b from table 即指定顯示表格式a,b兩列

5. 表末添加一列,用來計算前面各列的總和,SQL語句怎麼寫

例如添加列total
alter table 表名
add total int

然後將各列的值刷到total列中
update 表名
set total=列1+列2+列3...
where total is not null

6. 求一個多列匯總的sql語句

select[代碼],[名稱],[規格],[單位],sum([列1數量])as[列1數量],sum([列2數量])as[列2數量]],sum([列3數量])as[列3數量]from[表一]groupby[代碼],[名稱],[規格],[單位]

7. sql分類匯總如何實現

select片區,客戶,產品名稱,sum(數量)frombiaogroupby片區,客戶,產品名稱

8. 就SQL如何將不同行的數據分類匯總到列

將不同行的數據分類匯總到列語句如下:

1)insert into table1 select * from table2

2) select into table 1 select *from table2

這兩種寫法有什麼區別吶,首先insert into這種寫法必須有table1列,select則是在access中必須不能存在table1,否則在ado介面中會出現報錯的現象。

進行資料庫的操作,這里使用的是ado+access進行的資料庫的操作,目前來看,ado這種方式還算是比較簡單。

9. sql里查詢一個欄位里的記錄的多個類的匯總(幾個欄位按不同分類的匯總)

--技術要點:行轉列
--以下提供SQL SERVER語句

--創建測試環境
create table tab
(
machine int,
sernum varchar(10),
area varchar(2),
PF varchar(1)
)
--製造數據
insert into tab select 462,'16205R3E','AT','P'
insert into tab select 462,'16203H0N','AT','F'
insert into tab select 316,'1620A7WP','AT','S'
insert into tab select 316,'16206CCC','AT','S'

--1. 靜態行轉列(所謂靜態,是指的PF列只有P,F,S這三個值,或者值是固定的某幾個值)
select machine,
max(case pf when 'P' then num else 0 end) as P,
max(case pf when 'F' then num else 0 end) as F,
max(case pf when 'S' then num else 0 end) as S
from
(select machine,pf,count(1) as num from tab group by machine,pf
)tb
group by machine

/* 結果集
machine P F S
----------- ----------- ----------- -----------
316 0 0 2
462 1 1 0

(2 row(s) affected)
*/

--2. 動態行轉列(相對於靜態的概念)
declare @sql varchar(8000)
set @sql = 'select machine as ' + 'machine'
select @sql = @sql + ' , max(case pf when ''' + pf + ''' then num else 0 end) [' + pf + ']'
from (select distinct pf from tab) as a
set @sql = @sql + ' from (select machine,pf,count(1) as num from tab group by machine,pf
)tb group by machine'
exec(@sql)

/* 結果集
machine F P S
----------- ----------- ----------- -----------
316 0 0 2
462 1 1 0

(2 row(s) affected)
*/

--刪除環境
drop table tab