㈠ sql語句,根據存款明細更新賬戶余額
運行前做好賬戶信息表的備份
看欄位類型應該像是oracle,就按oracle的寫法給你寫了
updatet_accountaseta.balance=(selectb.trans_amountfrom
(selectaccount_no,sum(trans_amount)trans_amountfromt_depositorgroupbyaccount_no)b
wherea.account_no=b.account_no)anda.account_noin(selectaccount_nofromt_depositor)
如果你數據量特別大的話,可能執行效率不會高
㈡ SQL查詢統計某表的男女各個人數
select s.sex,count(s.sex) from student s GROUP BY sex;
GROUP BY 語句
GROUP BY 語句用於結合合計函數,根據一個或多個列對結果集進行分組。
測試student表紀錄如下圖,根據自己需求增刪欄位。
student s ,s是自己為student表定義的別名,count()為統計的人數。
拓展資料:
SQL GROUP BY 語法:
SELECT column_name(列名), aggregate_function(column_name) (函數名) FROM table_name(表名) WHERE column_name operator value GROUP BY column_name
㈢ sql語句如何查詢兩個值之間的差
工具/材料:Management Studio。
1、首先在桌面上,點擊「Management Studio」圖標。
㈣ Microsoft SQL Server 2005 中如何實現差額優先匹配
按照你的數據,我隨便寫了一個,你看一下,並且我這已經測試過,對於你上述數據沒問題的
建表及數據
createtablet
(orderidvarchar(1),
flagint,
xqslint,
kyslint)
insertintotvalues('D',1,10,NULL)
insertintotvalues('C',2,20,NULL)
insertintotvalues('B',3,30,NULL)
insertintotvalues('A',4,40,NULL)
執行以下
declare@countint
set@count=50
declarecur_ccursorforselectflag,kysl,xqslfromtorderbyflag
declare@xqslint
declare@flagint
declare@kyslint
declare@count1int
opencur_c
FETCHNEXTFROMcur_cinto@flag,@kysl,@xqsl
while@@FETCH_STATUS=0
begin
set@count1=@count-@xqsl
if@count1>=0
begin
updatetsetkysl=xqslwhereflag=@flag
set@count=@count-@xqsl
end
elseif@count1<=0and@count<=@xqsl
begin
updatetsetkysl=@countwhereflag=@flag
set@count=0
end
else
begin
updatetsetkysl=0whereflag=@flag
end
fetchnextfromcur_cinto@flag,@kysl,@xqsl
end
closecur_c
deallocatecur_c
最終結果
㈤ 要求用一條SQL語句查詢出每個用戶ID,交費次數及所交的個人交費金額
交費次數:select 用戶ID,COUNT(*) as 交貨次數,sun(交費金額) as 交費金額 FROM Info where 用戶ID=John group by 用戶ID
㈥ 用SQL語言從現有的資料庫里根據性別屬性分別統計出男女相應的一個屬性里各類型的數量
現過幾次 2出現過幾次
select count(1) from table1 where A=1;
select count(1) from table1 where A=2;
select [1]=count(case when A=1 then 1 else null end),[2]=
(case when A=1 then 1 else null end) from table1
A為1、B為2的情況幾次,
select count(1) from table where a=1 and b=2
當A為1,B出現幾次 等等
select count(distinct B ) from table1 where A=1
㈦ 使用SQL語句程序計算學生表中男女平均年齡之差
SELECT (SELECT avg([年齡]) FROM [表] WHERE [性別] = '男') - (SELECT avg([年齡]) FROM [表] WHERE [性別] = '女') as SubValue
㈧ 用SQL命令創建名為"男女人數"的查詢,查詢男生,女生人數分別是多少
1.2.3.4創建表和主外鍵關系,5加一個where條件6排序order by "日期"desc 7分組查詢group by,另外求記錄數用count(*)函數 8分組後差平均值,用avg("平均身高"),9修改update 表set 英語成績=英語成績*(1+0.1),10連接查詢,先按學生分組,然後總分sum(),平均分avg()11,按科目分組12,連接查詢13,連接查詢
㈨ SQL語句計算男女員工各佔比例。
假設表員工檔案里有性別、姓名兩個欄位
select count(*) as 員工總數,sum(case when 性別=男 then 1 else 0 end) 男員工數,sum(case when 性別=男 then 1 else 0 end)/nullif(count(*),0) 男所佔比例,sum(case when 性別=女 then 1 else 0 end) 女員工數,sum(case when 性別=女 then 1 else 0 end) /nullif(count(*),0)女所佔比例
from 員工檔案
你自己試試看吧
樓上的寫的不錯,但是最好注意一些除0問題,要是張空表會報除0錯誤的。
㈩ SQL計算語句請求問題
使用下面寫法就行了,其實把男生數和女生數的case when 相比就行了。
SELECT Convert(VARCHAR(100), Zixrq, 23) AS 日期,
SUM(CASE WHEN Xingb = '女' THEN 1 ELSE 0 END) AS 女生數,
SUM(CASE WHEN Xingb = '男' THEN 1 ELSE 0 END) AS 男生數,
SUM(CASE WHEN Xingb = '男' THEN 1 ELSE 0 END) / SUM(CASE WHEN Xingb = '女' THEN 1 ELSE 0 END) AS 男女比例
FROM Huiyuan
WHERE Zixrq >= '2012-11-23'
AND Zixrq <= '2012-12-3'
GROUP BY Convert(VARCHAR(100), Zixrq, 23)
帝狼神我好像答過你的問題