sql語句怎麼統計欄位的出現的次數

2022-06-27 18:00:07 字數 6171 閱讀 3133

1樓:

select a.[userpass] +'-'+ a.[userright], count(a.[userpass] +'-'+ a.[userright])

from (select * from for_deliver_user where for_deliver_user.userid='你的引數' ) as a group by a.[userpass] +'-'+ a.

[userright]

你自己把表和字段替換掉!

@badkano  的答案不錯。

2樓:匿名使用者

distinct 可以和group by 連用的。

但是distinct貌似後面不能給連個字段,給乙個可以。因為count的物件是1個,並不是2個。

如果要改的話。

你可以這麼試試

select a,b ,count(c) over(partition by a,b) as num

from sh

where c='5'

group by a,b

3樓:匿名使用者

不知道你用的什麼庫,也不知道你a,b都是字元型還數字型,我這裡以sqlserver並且你a,b是字元型為例

select a,b,count(a+','+b) from sh where c='5' group by a,b

也就是把a和b當做乙個整體,來計算

如果你a,b是數字型,你可用cast或convert轉換成字元

4樓:

distinct 怎麼能和 group by 連用呢

sql語句統計數量 統計乙個字段出現的數量

5樓:匿名使用者

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

6樓:匿名使用者

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的寫法會比較有侷限性

7樓:2一瞬間

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

sql 統計在兩個字段分別出現的次數

8樓:匿名使用者

select t.col,count(*)from

(select a as col from 表名union all

select b as col from 表名) as tgroup by t.col

表名替換一下

9樓:匿名使用者

select a,count(*) from table group by a

select b,count(*) from table group by b

sql語句,如何統計一列中的值重複出現的次數,查詢出的結果按次數的倒序排列?

10樓:陽光上的橋

select 字段,count(*) from 表名 group by 1 order by 2 desc

11樓:匿名使用者

select 重複字段, count(重複字段) from 表 group by 重複字段 order by 重複字段 desc

12樓:匿名使用者

這樣就可以了

select 字段,count(*) a from 表名 group by 1 t order by count(*)desc

13樓:匿名使用者

試試吧。

select 列名, count(*) from 表名 group by 列名 order by count(*) desc;

sql怎麼統計在某個欄位中某個值出現的總次數

14樓:匿名使用者

select sum(case when value='sh' then 1 esle 0 end) sh, sum(case when value='bj' then 1 esle 0 end) bj,name

from table

group by name

15樓:匿名使用者

左轉數學吧——from here』s to change 8❎

sql計算表中乙個列中各數的出現次數

16樓:淡了流年

select countb)from a group by b order by b

sql語句就可以做了,例檢索**aaa,新增一列「re_num」,顯示a列值重複的次數,

次數為**按a列分組計算記錄數:

select a,count(a) as 're_num' from aaa group by a,

select a1.a2.re_num from

aaa a1

join

(select a,count(a) as 're_num' from aaa group by a) a2

on a1.a = a2.a;

select sum(datac.count_value)

from t_jl_count_data datac

, t_jl_xc_result a

where datac.count_no=45 and a.id=datac.result_id。

17樓:匿名使用者

select b, count(*) -- 計算個數,速度快於count(字段). 因為 count(字段)會先判斷這個字段是否為空,只計算非空的個數。所以這裡直接用count(*)。

from a

group by b

18樓:匿名使用者

select b, count(b) from a group by b

sql server如何查詢出某個字段重複出現兩次以上的資料

19樓:

用關鍵字 stinct

select stinct 字段

不重複的意思

oracle裡怎麼統計某個字段出現的重複次數

20樓:匿名使用者

可用count函式來計算某個字段重複次數。

如test表中資料如下:

現在要查詢name列中,各個名字重複的次數,可用如下語句:

select name,count(*) from test group by name;

查詢結果:

21樓:齋溶

請看下面,當然我是用sql server來寫。但是在oracle能通用。

select b.id,

case when b.h is null then 0 else b.h end as h,

case when  b.t is null then 0 else  b.t end as t,

case when  b.g is null then 0 else  b.g end as g from (

select  distinct id,

(select [count] from 表 where id=a.id and ra=100) as h,

(select [count] from 表 where id=a.id and ra=200) as t,

(select [count] from 表 where id=a.id and ra=300) as g

from 表 a) b

sql語句統計數量,統計欄位的值的數量

select type,count as 總數量,sum case when level 一級 then 1 else 0 end as 一級,sum case when level 二級 then 1 else 0 end as 二級,sum case when level then 1 else...

SQL欄位累加求和的問題,SQL 多個字段值相加後排序問題

select id,string1,string2 from table union select id 0,string1 string1 小計 countresult count 1 sum1 sum string2 from talbe group by string1 union selec...

sql語句怎樣進行資料庫欄位的條件查詢

可以通過where條件語句實現。sql select from tablename where name like 張 and id 5 解釋 以上就是通過條件查詢出名字以張開始的,id大於5的表資料。備註 多條件之間如果是同時滿足用and,如果是滿足其中乙個用 or。呼叫處直接用 getchann...