1樓:酒意何存
靠 你問題能在坑點嗎 第一個資料 (pid)c對應的(weight)是0
我說怎麼看怎麼不對呢
select
sum( case when flag = '0' then weight else 0 end) as flag0,
sum( case when flag = '0' then weight else 0 end)/sum(weight) as per0,
sum( case when flag = '1' then weight else 0 end) as flag1,
sum( case when flag = '1' then weight else 0 end)/sum(weight) as per1
from tab1
另外:如果需要轉換成百分數 可以找一下方法 或者問我也行
2樓:
with t2 as (select flag, sum(weight) sum from t1 group by flag)
select sum(case when flag=0 then sum end) flag0, sum(case when flag=0 then sum end)/(select sum(sum) from t2) per0, sum(case when flag=1 then sum end) flag1, sum(case when flag=1 then sum end)/(select sum(sum) from t2) per1 from t2
求sql查詢語句,表的列資料按照條件不同輸出為不同的列,並統計個數。
3樓:匿名使用者
select distinct(單位),count(單位) as 型別
數量,sum(case when 型別=1 then 1 else 0 end) as 型別為1 ,
sum(case when 型別=2 then 1 else 0 end) as 型別為2,
sum(case when 型別=3 then 1 else 0 end) as 型別為3
from sh1
group by 單位
已測試,結果:
a2110
b3111
c1010
也祝福您專身體健康,多金多福 ,快屬採納,哈哈!
4樓:匿名使用者
select 單位
zhi,
sum(case 型別
dao when '1' then 1 else 0 end) as 型別為
版1,sum(case 型別 when '2' then 1 else 0 end) as 型別為2,
sum(case 型別 when '3' then 1 else 0 end) as 型別為3
from 表權a group by 單位
mysql:只用一條sql語句,如何查出一個表裡,不同條件對應的資料條數
5樓:匿名使用者
看一下這個sql
select
sum(
if((*** = 1 and age = 2),1,0)),sum(
if((*** = 1 and age = 5),1,0)),sum(
if((*** = 1 and age = 10),1,0))from a_test
這個sql現在就是得出的這個結果
mysql一條sql統計某個欄位不同值的個數 10
6樓:匿名使用者
以時間為跨度統計不同的值,在該時間出現的次數。
語言如下:
select count(*),'列名' from tablename group by '列名'
select count(*),a_yqm from user group by a_yqm
舉例:這裡,我要查詢出1年內每個月份periods欄位不同值的次數。
比如下圖中可見的2015-4月,periods為2出現了3次,3出現了1次,最關鍵的是 periods你不知道有多少種可能的值,也許這個月有1,也許沒有。
7樓:匿名使用者
可以加一個引數就可以
select name,count(*) from table where status=2 group by status,name with rollup;
如果mysql中這麼寫不行,那麼就用巢狀的寫法select * from (select status,name,count(*) from table group by status,name with rollup)
where ststus=2;
8樓:504工作室
select name,count(1)
from table
where status=2
group by name
9樓:崖墓枯
select count(*)as 總數,sum(case when create_time < '2018-01-01 00:00:00' then 1 else 0 end) as
年前資料總量,
sum(case when create_time > '2018-01-01 00:00:00' then 1 else 0 end) as 年後資料總量,
fromt_year
(create_time > '2018-01-01 00:00:00') 是查詢的條件 用法同 where一致
sql語句:統計指定欄位,等於不同值的條數
10樓:匿名使用者
方法一:通過group by ,之後count條數實現。
sql:select count(1) from tablename group by columes;
方法二:通過district函式來直接取出唯一欄位,之後統計數量:
sql:select count(ditrict(columes)) from tablename;
解釋:columes表示的是特殊欄位。
11樓:匿名使用者
select count(distinct 欄位1),count(distinct 欄位2),count(distinct 欄位3) from 表名
sql語句統計數量,統計一個欄位的值的數量
12樓:我tm不管
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 0 end) as **
from table group by type樓上的應該改改吧
13樓:
pivot
--sql server 2000 靜態sqlselect type as type ,
max(case level when '一級' then id else 0 end) 一級,
max(case level when '二級' then id else 0 end) 二級,
max(case level when '**' then id else 0 end) **,
count(id) 總數量
from tb
group by type
然後再彙總,或者可以寫個檢視就ok了
--sql server 2000 動態sql
declare @sql varchar(8000)
set @sql = 'select type '
select @sql = @sql + ' , max(case level when ''' + level + ''' then id else 0 end) [' + level + ']'
from (select distinct level from tb) as a
set @sql = @sql + ' from tb group by type'
exec(@sql)
--sql server 2005 靜態sql。
select * from (select * from tb) a pivot (max(id) for level in (一級,二級,**)) b
--sql server 2005 動態sql。
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + level from tb group by level
exec ('select * from (select * from tb) a pivot (max(id) for level in (' + @sql + ')) b')
14樓:匿名使用者
select type,sum(*) 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 0 end) as **
from table group by type
15樓:匿名使用者
select type,count(1) from table group by type
union all
select level,count(1) from table group by level
用sql語句統計資料庫某個欄位中相同的資料有多少條?
16樓:幸運的
1、可通過分組和組內計數來實現,語句如下:
select a, count(*) from a group by a
2、用group by分組:
group by + [分組欄位](可以有多個)。在執行了這個操作以後,資料集將根據分組欄位的值將一個資料集劃分成各個不同的小組。
這裡,分組欄位是a,所以資料集分成了你、我、他三個組。然後用count(*)分別按照各個組來統計各自的記錄數量。
3、count(*)函式:
count(*) 函式返回表中的記錄數。注意它和group by連用,返回組內記錄數。
17樓:匿名使用者
select a,count(*) from 表a group by a
18樓:匿名使用者
select a, count(a) from a
group by a
19樓:大瑞瑞卡哇伊
select b,count(*) from a s join b sf on a.a_id = sf.b_id group by a_id;
求一條SQL語句
樓上兩位的都有問題。應該是 select a.tid,tkh,tname,sum t2a sum t2b from table1 a,table2 bwhere a.tid b.tid and tkh 50 group by a.tid,tkh,tname select table1.tid,tkh...
求一條SQL語句
select from select from 表 where 列2 is null union select from 表 where 列2 is not null and rownum 100 case when select count from 表 where 列2 is null 100 ...
求一條SQL語句,有關聯合查詢的
試試看這樣行不行 select 表b 學號 表a 姓名 isnull sum case 表b 學科 when n 語文 then 表b 分數 end 0 as 語文 isnull sum case 表b 學科 when n 數學 then 表b 分數 end 0 as 數學 isnull sum c...