SQL通過表變數,求百分比的小例子

2021-06-07 15:36:15 字數 1595 閱讀 8549

declare @temptotaltable table (totalcount int ,task_keyword varchar(100),searchengine_name varchar(100),brand_name varchar(100))

declare @tempgiveuptable table (giveupcount int ,task_keyword varchar(100),searchengine_name varchar(100),brand_name varchar(100))

insert @temptotaltable

(totalcount,task_keyword,searchengine_name,brand_name)

(select

count(1) as 抓取總數

,task_keyword

,searchengine_name

,brand_name

from [sec_item] as a ,[iwt_brand] as b ,sec_searchengine as c

where a.brand_id=b.brand_id and a.brand_id=30015 and c.searchengine_id=a.searchengine_id group by task_keyword,searchengine_name,brand_name

) insert @tempgiveuptable

(giveupcount,task_keyword,searchengine_name,brand_name)

(select

count(1) as 放棄數

,task_keyword

,searchengine_name

,brand_name

from [sec_item] as a ,[iwt_brand] as b ,sec_searchengine as c

where a.brand_id=b.brand_id and a.brand_id=30015 and c.searchengine_id=a.searchengine_id and item_state in (4,6) group by task_keyword,searchengine_name,brand_name

) select isnull(cast(cast(b.giveupcount*1.0/a.totalcount*100 as decimal(10,1)) as varchar(20))+'%','0%')as [百分比],isnull(b.giveupcount,0) as 放棄數 , a.totalcount as 抓取總數,a.task_keyword,a.searchengine_name,a.brand_name from @temptotaltable as a left join @tempgiveuptable as b on a.brand_name=b.brand_name and a.task_keyword=b.task_keyword and a.searchengine_name=b.searchengine_name order by a.totalcount desc

sql語句求百分比

此sql語句包括了兩個聚合函式做除法求百分比,並保留兩位小數,直接輸出字串形式的百分比。以及對case when在聚合函式的應用。select ss.ss name,ss id,count ea.ea id as eacounts,回單交換單個數 count eb.eb id as ebcounts...

SQL求總和的百分比

先通過group by得到每個分類別的總和,再通過開窗函式計算整體的總和,兩者相除就得到佔比了。當over 後不加任何內容時,就是對所有的資料進行彙總。在oracle中,也可以使用專門的比例函式 ratio to report 來直接計算 上述操作可以更改如下 select sites id,cha...

oracle 求百分比

oracle求百分比 例子 求name為dead的記錄所佔百分比 select tt.retrieve retrievedrate,tc.公司名稱 companyname,tc.部門名稱 deptname,tc.使用者名稱 username,tc.nodename nodename,tc.proce...