1? 先按人名和支付方式匯總

創(chuàng)新互聯(lián)建站成立與2013年,先為江永等服務(wù)建站,江永等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為江永企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
2 行列轉(zhuǎn)換
sqlserver行列轉(zhuǎn)換知識
create?table?A(name?varchar(10),sort?varchar(30),q?int)
insert?into?A?values('A','現(xiàn)金',19)
insert?into?A?values('A','現(xiàn)金',19)
insert?into?A?values('B','現(xiàn)金',19)
insert?into?A?values('C','現(xiàn)金',19)
insert?into?A?values('A','微信',19)
insert?into?A?values('A','銀行卡',19)
insert?into?A?values('A','支付寶',19)
go
select?*?from
(?
select?name,sort,sum(q)?as?qtyall?from?A?
group?by?name,sort?
)?as?t?
pivot(sum(qtyall)?for?sort?
in([現(xiàn)金],[銀行卡],[微信],[支付寶]))?
t?;
go
drop?table?A
如有疑問,及時溝通!
平均值
select avg(某字段) from dual;
求和
select sum(某字段) from dual;
可以有兩種解決方法,
所需工具:SQL
查詢兩個count的方法1:
SELECT paperName , COUNT (1) AS 總題數(shù) , sum (CASE WHEN statu = 1 THEN 1 ELSE 0 END) AS 審核題數(shù)FROM questionGROUP BY paperNme
查詢兩個count的方法2:
select s.總題數(shù), s.審核題數(shù), s.paperNamefrom (select COUNT(1) as 總題數(shù), case when status = 1 then count(1) else 0 end as 審核題數(shù), paperNamefrom question--where papername in (select distinct paperName from question), 這個條件可以不要了group by paperNme, stauts -- status也要作為分組字段,因為在case中有使用) s
參考sql語句
select no ,
max(case when type=1 then type) else null end) type1,
max(case when type=2 then type) else null end) type2,
sum(case when type=1 then sumsource) else null end) source1,
sum(case when type=2then sumsource) else null end) source2,
from(
selct no , type, sum(source) sumsource
group by no,type
)
select buy.cust_no,SUM(amount) from buy join customer c on buy.cust_no=c.cust_no group by buy.cust_no having SUM(amount)=100
對分組過濾用的是having子句
create?Table?T
(
id?int,
a??int,
b??int
)
Insert?into?t?values(1,1,1)????
Insert?into?t?values(2,2,1)????
Insert?into?t?values(3,3,1)??
Insert?into?t?values(4,4,1)????
Insert?into?t?values(5,5,1)???
Insert?into?t?values(6,6,1)???
/*思路:
id可能不連續(xù),所以先用id排序,產(chǎn)生一個記錄號id2
然后按3條記錄一組產(chǎn)生一個分組號G,接著按G分組求和,id取最大
最后更新
*/
With?CT
AS
(
Select?max(id)?As?id,SUM(a)?As?A,SUM(B)?As?B
from
(
Select?*,(id2+2)/3?As?G?
from?(Select?*,ROW_NUMBER()?over(order?by?id)?As?id2?from?T)?S
)M?Group?by?G
)
Update?T?Set?a=CT.A-T.a,b=CT.B-T.b
From?CT
where?T.id=CT.id
本文題目:sqlserver分組求和語句,oracle 分組求和
文章轉(zhuǎn)載:http://chinadenli.net/article30/dsgeeso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、定制網(wǎng)站、App開發(fā)、Google、微信公眾號、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)