這個用SQL語句怎麼寫,這個用SQL語句怎麼寫

2022-10-12 22:00:34 字數 1254 閱讀 3509

1樓:匿名使用者

select namef 名字,sum(decode(score,'勝',1,0)) 勝,sum(decode(score,'負',1,0)) 負

from ss

group by namef

//ss是表名, namef是欄位名,就是每個隊名對應的欄位,「名字」是別名,

「勝」「負」都是別名,sum(decode(score,'勝',1,0)) 這是把score欄位下為「勝」的資料轉換成1,然後再按名字進行分組相加。sum(decode(score,'負',1,0)) 這是把score欄位下為「負」的資料轉換成1,然後再按名字進行分組相加。

2樓:匿名使用者

欄位值是字元型,勝和負,為什麼要用sum()?sum的作用是整型。這題多明顯count()求記錄條數,按球隊名分組,把勝的統計,負的統計,返回整型結果。

select t1.name,t1.count_y as 勝,t2.count_n as 負

from (select name,count(score) as count_y from t_scores where score='勝' group by name)as t1,(select name,count(score) as count_n from t_scores where score='負' group by name)as t2

where t1.name=t2.name

3樓:

select name,sum(decode(score,'勝',1,0)) '勝',sum(decode(score,'負',1,0)) '負' from t_scores

group by name

4樓:匿名使用者

select name,sum(case score when '勝' then 1 else 0 end) as '勝',sum(case score when '負' then 1 else 0 end) as '負' from t_scores group by name

5樓:匿名使用者

經過sql server2000下驗證通過:

select name,count(case when score='勝' then 1 end) as '勝',count(case when score='負' then 1 end) as '負'

from t_scores

group by name

請教要實現這個查詢的SQL語句怎麼寫啊

select 姓名 城市 性別 工資 max 日期 from 表 group by 日期 select a.from 表 a left join select 姓名,城市,max 日期 as 日期 from 表 group by 姓名,城市 b on a.姓名 b.姓名 and a.城市 b.城市 ...

sql 語句中count函式怎麼用

count 函式返回匹配指定條件的行數。sql count column name 語法 count column name 函式返回指定列的值的數目 null 不計入 select count column name from table name sql count 語法 count 函式返回表...

SQL基礎問題,用SQL語句完成下列問題。

一樓的很正確 把語句也都寫出來了。在這我只是給解釋一下。create table tb user 建立一個表 create table 表名。userid int not null,列明userid 資料型別 int 整數 null not null 指該列是否接受空值 null表示接受。其它列依次...