sql 查詢怎麼統計多個欄位各自的個數

時間 2021-12-23 01:36:15

1樓:匿名使用者

--所有算的地方都用cast(個數 as int)

create table test05

(a varchar(10),

b varchar(10),

c varchar(10))

insert into test05 select '#','一','三' union all

select '@','一','三' union all

select '¥','一','二' union all

select '%','二','二'

select * from test05

select b,count(b) 個數 from test05 group by b

select c,count(c) 個數 from test05 group by c

select sum(isnull(t1.個數,0)+isnull(t2.個數,0)) 總數,sum(isnull(t1.

個數,0)) 個數,t1.b,sum(isnull(t2.個數,0)) 個數,t2.

c from

( select b,count(b) 個數 from test05 group by b ) as t1 full join

( select c,count(c) 個數 from test05 group by c ) as t2 on t1.b=t2.c

group by t1.b,t2.c

2樓:

一種查詢sql如下, 利用union獲得b和c各自的統計結果, 然後再一次統計整合到最終結果:

select sum(d.b_cnt) + sum(d.c_cnt) as total_cnt, sum(d.

b_cnt) as b_cnt, case when sum(d.b_cnt) = 0 then '' else d.val end as b_label, sum(d.

c_cnt) as c_cnt, case when sum(d.c_cnt) = 0 then '' else d.val end as c_label

from(

select b as val, count(b) as b_cnt, 0 as c_cnt

from a

group by b

union all

select c, 0, count(c) as c_cnt

from a

group by c

) dgroup by d.val

sql serer上的測試結果(欄位次序有變化),

total_cnt為總數, b_label為b欄值, b_cnt為b欄個數, c_labe為c欄值, c_cnt為c欄個數.

這個結果跟欄位是否為整型無關, 它是統計記錄出現的次數.

sql中如何用select 語句查詢統計多個非空列欄位的數量?

3樓:匿名使用者

select '列1' as 列名,count(*) as 數量 from 表1 where 列1 is null

union all

select '列2' as 列名,count(*) as 數量 from 表1 where 列2 is null

union all

select '列3' as 列名,count(*) as 數量 from 表1 where 列3 is null

這樣?還有,你用的什麼資料庫

sql中根據表中一個欄位分組分別統計每個分組的記錄數

4樓:baby愛吃水煎包

分組統計可以參考以下操作:

當陣列重複的時候分組才有意義,因為一個人也可以分為一組,只是沒有意義而已,分組採用group by語句完成,語法如下:

例子:按照部門編號分組,求出每個部門的人數,平均工資

按照職位分組,求出每個職位的最高和最低工資:

一旦分組之後,實際上對於語法上就會出現新的限制:

分組函式可在沒有分組的時候單獨使用,卻不能出現其他的查詢欄位:

ename就是其它查詢欄位。在select子句之後,只能出現分組的欄位和統計函式,其它的欄位不能出現

分組函式允許巢狀,但是巢狀之後的分組函式的查詢之中不能再出現任何其它欄位

例子:按照職位分組,統計平均工資最高的工資

當新增其它欄位『job』之後出現錯誤

例子:查詢出每個部門的名稱、位置、部門的人數、平均工資

確定所需的資料表:

emp表:部門的人數,平均工資

dept表:部門的名稱,位置

確定已知的關聯欄位:

emp.deptno = dept.deptno

發現dname存在重複,可以進行分組去除重複,按照之前對分組的理解,只要資料重複那麼就有可能進行分組的查詢操作,但是此時與之前的分組操作 不太一樣,之前的分組是針對一張實體表進行分組的(emp,dept都屬於實體表),但是對於以上的資料是通過查詢結果顯示的,所以是一張臨時的虛擬表,但是不管是否是實體表還是虛擬表,只要是有重複,那麼就直接進行分組

5樓:匿名使用者

select 欄位,count(欄位) from tablename group by 欄位

sql語句統計數量 統計一個欄位出現的數量

6樓:匿名使用者

1、建立測試表,62616964757a686964616fe78988e69d8331333431373863

create table test_stu(id number,  u_name varchar2(20), subject varchar2(20));

create table test_subj(id number,  subject varchar2(20));

2、插入測試資料

insert into test_stu values(1,'張三','英語');

insert into test_stu values(2,'李四','德語');

insert into test_stu values(3,'王五','日語');

insert into test_stu values(4,'小明','英語');

insert into test_stu values(5,'小狗','法語');

insert into test_subj values(1,'英語');

insert into test_subj values(2,'德語');

insert into test_subj values(3,'日語');

insert into test_subj values(4,'法語');

3、查詢表中所有記錄數,select t.*, rowid from test_subj t,

4、編寫sql,統計test_subj總記錄數,及每個科目選修學生數;

select count(distinct t.id) as "小計",

count(case when subject='英語' then 1 end) as "英語",

count(case when subject='德語' then 1 end) as "德語",

count(case when subject='日語' then 1 end) as "日語"

from (select t.*

from test_subj t, test_stu b

where t.subject = b.subject) t

7樓:匿名使用者

sqlserver為例

建立表及插入資料

create table 姓名錶

(id int,

u_name varchar(10),

subject varchar(10))

create table 科目表

(id int,

s_name varchar(10))

insert into 姓名錶 values (1,'張三','英語

62616964757a686964616fe59b9ee7ad9431333337373562')

insert into 姓名錶 values (2,'李四','德語')

insert into 姓名錶 values (3,'王五','日語')

insert into 姓名錶 values (4,'小明','英語')

insert into 姓名錶 values (5,'小狗','法語')

insert into 科目表 values (1,'英語')

insert into 科目表 values(2,'德語')

insert into 科目表 values(3,'日語')

insert into 科目表 values(4,'法語')

然後需要建立一個檢視

create view v_subject

asselect a.s_name,sum(case when a.s_name=b.subject then 1 else 0 end) counts

from 科目表 a left join 姓名錶 b on a.s_name=b.subject

group by a.s_name

執行語句

declare @sql varchar(4000)

set @sql = 'select sum(counts) as 合計'

select @sql = @sql + ',sum(isnull(case [s_name] when '''+[s_name]+''' then [counts] end,0)) as

['+[s_name]+']'

from (select distinct [s_name] from v_subject) as a

select @sql = @sql+' from [v_subject]'

exec (@sql)

結果截圖

你結果裡為什麼會少呢?

這個主要是動態顯示才這麼複雜,比如你在科目表裡再加個阿拉伯語,用這個也沒問題,否則用case when的寫法會比較有侷限性

8樓:2一瞬間

select subject,count(subject) from 姓名錶 group by subject order by id

sqlserver怎麼實現同一個表中多個count查詢並且分組並且統計總數

9樓:折柳成萌

可以有兩抄

種解決方法,

所需工具:sql

查詢兩個count的方法1:

select *****name , count (1) as 總題

數 , sum (case when statu = 1 then 1 else 0 end) as 稽核題數from questiongroup by *****nme

查詢兩個count的方法2:

select s.總題數, s.稽核題數, s.

*****namefrom (select count(1) as 總題數, case when status = 1 then count(1) else 0 end as 稽核題數, *****namefrom question--where *****name in (select distinct *****name from question), 這個條件可以不要了group by *****nme, stauts -- status也要作為分組欄位,因為在case中有使用) s

sql用update語句一次更新多個欄位應該怎麼寫

小圳軍 update 表名 set 欄位1,欄位2,欄位3,select 數值1,數值2,數值3,where 條件 多個欄位可以使用逗號隔開,每一個 欄位名 值 就是賦值,其後的where 條件語句可加可不加。拓展資料 結構化查詢語言 structured query language 簡稱sql ...

SQL語句怎樣查詢並刪除單個欄位下的所有重複值,重複的記錄只保留

張愛民 浦東 需要一個主鍵 id 如果是要刪除是姓名重複的話,試試以下 delete ta where id not in select max id from group by nch 如果要顯示不重複項的資料 select from ta where id in select max id fr...

sql當中如何查詢某個欄位中的值的第幾個字元中含有相應的值

1 首先,我們在資料庫中建立一個資料表fruit,包含id,name,loc三個欄位。用desc語句查詢一下資料表是否建立成功。2 接下來就是對資料表中填充資料。用insert into語句,對資料表進行填充資料。3 按要求輸入各欄位的資料,分條輸入。5 接下來就是查詢資料表中的所有欄位的資料值。m...