sql語句求解COUNT CASE WHEN THEN排除重複項

2022-02-04 20:41:20 字數 5459 閱讀 1041

1樓:常樂常知足

可以用count函式,但是else時用null值,不能用0,即:

count(case when a.close_id='t' then a.mo_no else null end)

這個else還可以不寫,因為預設情況下不寫就是空值。即:

count(case when a.close_id='t' then a.mo_no end)

2樓:匿名使用者

select sum(case when a.close_id='t' then 1 else 0 end)

from (select distinct a.close_id,a.mo_no from a) t

'完成種數'=count(case when a.close_id='t' then a.mo_no else 0 end)這句的邏輯就有問題,你要求a.

close_id='t' 時, a.mo_no 的個數嗎?那樣你直接寫

select count(a.mo_no) from a where a.close_id='t' 就好了

就算你一定要用case when...then,你也要用sum而不是count,你用count是所以(case when a.close_id='t' then a.

mo_no else 0 end)的個數,就算是0,它也是個值,仍然會被count進去啊,你只有sum時,0才是不會起做用的數。

3樓:一直就很笨

select count(*) from (select distinct * from tablename) as a

你參考這種方式試一下

如果實在不行,就用檢視吧

sql語句為什麼在這種case when情況下要用sum而不是count

4樓:匿名使用者

case when price <= 1000 then 1 else 0 end

三個case語句效果相同,其實就是類似於增加一個欄位,這個欄位,滿足條件的為1,不滿足的是0,這樣sum的效果,就是將所有的1加起來,也就是所有滿足條件的記錄個數。

而count,會不管是1還是0,都會統計,這樣怎麼算都是總條目數8個。

語句這東西,靈活使用的情況太多,慢慢來吧,做到遇到一個理解一個,很快也就熟練了。

5樓:匿名使用者

select count (case when price <= 1000 then price end)as low, count (case when price between 1001 and 3000 then price end)as mid, count (case when price > 3000 then price end)as high from test;

這樣試試,0也是會被計數的

6樓:撒比西大哥

count是計算個數,sum是求和。

你用sum(then 1 else 0),意思就是1+0+1+0(按條件返回值)。。語義上=計算符合條件的個數(不符合的為0加了也等於沒加),所以結果符合預期,你改成count是計算個數,無論返回1還是0,個數都是8個,所以結果錯誤。

你把語句改成count(then price),或者count(then 1)不加else 0,語義上就是計算符合條件的個數,結果就符合預期了。

7樓:匿名使用者

sum (case when price <= 1000 then 1 else 0 end)

這句話是對當**小於1000的則返回1,如果不是則返回0,然後進行求和。而count則是計數,無論返回1還是0都會記一次數。所以使用count會一直返回總數8.

8樓:常樂常知足

也可以用count函式,但是else時用null值,不能用0,因為count不統計空值,但0不是空值。即:

count (case when price <= 1000 then price else null end)as low

這個else還可以不寫,因為預設情況下不寫就是空值。

9樓:匿名使用者

哎。我本人也是個新手,剛剛接觸sql。

你這個問題,真的,很簡單。

count是計數,我就問你,你then的0難道就不算數了嗎?

你把0改成null試試

關於使用sql語句sum(case when……)來實現分類彙總功能

10樓:大野瘦子

有兩處有筆誤,如果結果不同的話,需要檢查一下,先對比下總金額,再對比各個的銷售額,再檢查姓名是否完全一致,excel中經常有前後空格或是不容易顯示出來的字元會有影響,用select 月份,姓名,sum(銷售數量*產品單價) from ** group by 月份,姓名

例如:select

月份,sum (case when 銷售人員='姓名1' then 銷售數量*產品單價 else 0 end) as 姓名1銷售額,

sum (case when 銷售人員='姓名2' then 銷售數量*產品單價 else 0 end) as 姓名1銷售額,

sum (case when 銷售人員='姓名3' then 銷售數量*產品單價 else 0 end) as 姓名1銷售額

from **

group by 月份,銷售人員

注意事項

case when有用兩種用法如下:

用法一:

case case_value

when when_value then statement_list

[when when_value then statement_list] ...

[else statement_list]

end case

用法二:

case

when search_condition then statement_list

[when search_condition then statement_list] ...

[else statement_list]

end case

11樓:

你是要按月分和銷售人員來對銷售額透視,而sql語句只對月份分組,正確結果應該增加分組維度

select

月份,sum (case when 銷售人員='姓名1' then 銷售數量*產品單價 else 0 end) as 姓名1銷售額,

sum (case when 銷售人員='姓名2' then 銷售數量*產品單價 else 0 end) as 姓名1銷售額,

sum (case when 銷售人員='姓名3' then 銷售數量*產品單價 else 0 end) as 姓名1銷售額

from **

group by 月份,銷售人員

12樓:

語句應該不是這樣用的,試試:

select 月份,姓名,sum(銷售數量*產品單價) from ** group by 月份,姓名

這樣跟excel資料透視表得到的結果相同

13樓:匿名使用者

語句沒問題,有兩處有筆誤,如果結果不同的話,需要檢查一下,先對比下總金額,再對比各個的銷售額,再檢查姓名是否完全一致,excel中經常有前後空格或是不容易顯示出來的字元會有影響,推薦用select 月份,姓名,sum(銷售數量*產品單價) from ** group by 月份,姓名

14樓:

因為欄位裡面有null值,透視表算的資料應該是不對的

sql 語句 count 使用請教

15樓:小周子

過多繁瑣的sql影響**質量,及維護成本,以下為兩種小技巧處理方式,僅供參考。

第一種,用case ---when---方法

select id

,sum(case when type in (1,2) then [count] else 0 end) as sum1

,sum(case when type in (3) then [count] else 0 end) as sum2

,sum(case when type in (4,5) then [count] else 0 end) as sum3

from 表名

group by id

第二種,if 判斷

select   sum( goods_amount ) as money,

count( * ) as num,

count(if(pay_status=1,true,null)) as success,

count(if(pay_status=2,true,null)) as fall

from `tab_order_info`

where user_id = 11

16樓:黃老邪傳人

sql count() 語法

sql count(column_name) 語法

count(column_name) 函式返回指定列的值的數目(null 不計入):

select count(column_name) from table_namesql count(*) 語法

count(*) 函式返回表中的記錄數:

select count(*) from table_namesql count(distinct column_name) 語法

count(distinct column_name) 函式返回指定列的不同值的數目:

select count(distinct column_name) from table_name註釋:count(distinct) 適用於 oracle 和 microsoft sql server,但是無法用於 microsoft access。

count()的三種語法中,沒有可以滿足你的條件的,唯一的方法就是使用where,就是你列出的那種

使用count(distinct column_name)語法的話,你的返回值應該是2...

17樓:匿名使用者

下面的幾條語句完成向a表插入10000條c=1,d=2,e=3的記錄:

declare @c int,@d int,@e int,@count int

select @c=1,@d=2,@e=3,@count=10000while @count>0

begin

insert into a表(c,d,e) values(@c,@d,@e)

set @count=@count-1end

18樓:霸王龍

過分發圖服服帖帖讓他人與人

19樓:匿名使用者

count()裡面的引數是某個欄位值

這表是男女混合的資料

不篩選能做麼?

求解SQL語句,求解一個SQL語句

用外聯接是合適的方案 用b表左聯接a表,如下 select b.bid,b.aid,b.atitle,a.aid,a.atitle from b left outer join aon b.aid a.aid so easy.select b.a.atitle from b left join a ...

複雜sql語句查詢,sql語句查詢

select id,name,isnull select sum 金額 from b表 where id a表.id and bno 20 0 正常消費,isnull select sum 金額 from b表 where id a表.id and bno 30 0 充值,select top 1 ...

C解析sql語句,C 中 SQL 查詢語句

語法錯誤 bai你在from後面的是一串dusql語句,所以語zhi句後是要加別名的。試試 daoselect sum 總額 回 from select distinct 合同,總額 from table where 專案 aa and型別答 a as tab select sum 總額 from ...