『壹』 查詢出每個部門的人員總數,sql語句怎麼寫
sql 使用sum 與 group by
可以統計每個部門的總人數
sum統計總人數 group by根據部門分組
例子
id departmentname number
1 技術 10
2 技術 3
3 銷售 50
sql語句
select departmentname ,sum(number)number from table group by departmentname ;
結果
departmentname number
技術 13
銷售 50
『貳』 統計一個各部門有多少人的SQL語句,解決馬上給分
createtable[User]
(
idint,
[name]varchar(10),
deptidint
)
createtabledept
(
idint,
[name]varchar(10)
)
insertinto[user]select1,'user1',1
unionselect2,'user2',1
unionselect3,'user3',1
unionselect4,'user4',2
unionselect5,'user5',2
insertintodeptselect1,'dept1'
unionselect2,'dept2'
selectcount([user].id),dept.[name]
from[User]
innerjoindepton[user].deptid=dept.id
groupbydept.id,dept.[name]
『叄』 sql里 如何統計一個公司的部門人數 可以有很多子公司 每個子公司可以有相同的部門 每個部門有多個員工
使用GROUP BY分組。
假設表的信息為:Employee(CompName,DeptName,EmpName)
--創建數據表
CREATETABLEEmployee
(
CompName VARCHAR(20),--子公司名稱
DeptName VARCHAR(20),--部門名稱
EmpName VARCHAR(20)--員工姓名
)
--插入測試數據
INSERTINTOEmployeeVALUES('北京公司','人事部','張')
INSERTINTOEmployeeVALUES('北京公司','財務部','趙')
INSERTINTOEmployeeVALUES('北京公司','人事部','孫')
INSERTINTOEmployeeVALUES('上海公司','人事部','王')
INSERTINTOEmployeeVALUES('上海公司','財務部','李')
--SQL查詢
SELECT CompName,DeptName,Number=COUNT(*)
FROM Employee
GROUPBY CompName,DeptName
測試結果:
『肆』 SQL資料庫查詢 顯示在不同部門下工作的員工的總人數的語句怎麼寫
select 部門,count(*) as 總人數 from 表 group by 部門
『伍』 查找員工數量超過3個以上的部門,並列出這個部門員工數量,用sql語句怎麼寫
操作示例步驟:
比如:你的表名稱為「員工表」,表內含有欄位:姓名、部門等等。
『陸』 sql列出部門編號和員工數量
select count(員工),部門編號 from 表名。
『柒』 sql統計數量
select 部門名稱,count(id) as '員工人數 ' from A inner join B on B.a_id=A.id
『捌』 用sql語句實現'查詢各部門名稱和該部門員工數'
具體語句如下:
『玖』 sql語句統計各部門不同人員類別的人數
第一步,依據你上邊給的語句創建一個視圖
createviewv_dept
as
selectbd_deptdoc.deptcodeasdeptcode,
bd_deptdoc.deptlevelasdeptlevel,
bd_deptdoc.deptnameasdeptname,
bd_psndoc.psnnameaspsnname,
bd_psncl.psnclassnameaspsnclassname,
bd_psncl.psnclasscodeaspsnclasscode
frombd_psndoc
innerjoinbd_deptdoc
onbd_psndoc.pk_deptdoc=bd_deptdoc.pk_deptdoc
innerjoinbd_psncl
onbd_psndoc.pk_psncl=bd_psncl.pk_psncl
第二步,動態執行sql,由於你人員類別可能不止就3種,所以要動態執行
declare@sqlvarchar(4000)
set@sql='selectdeptcode,deptname'
select@sql=@sql+',sum(isnull(case[psnclassname]when'''+[psnclassname]+'''then1end,0))as
['+[psnclassname]+']'
from(selectdistinct[psnclassname]fromv_dept)asa
select@sql=@sql+'fromv_deptgroupbydeptcode,deptname'
exec(@sql)
『拾』 如何用SQL語言實現計算人員工資總額及人員人數
計算工資總額:
這是查詢:sql=「selest
sum(工資)
as
總共工資
from
工資表」
這是顯示:rs(「總共工資」)
計算員工總數:
這是查詢:sql=「selest
count(員工姓名)
as
總員工數
from
員工「
顯示同上:rs(」總員工數「)
註:以上語句中的」總工資數「」總員工數「為自定義欄位,數據表中沒有
只是臨時建立的一個欄位
追分的話你看著給
golongfly
你沒用AS轉換
怎麼顯示出來啊?