2016-06-17 60 views
0

請選擇此項。它會按團隊生成用戶列表並彙總數據。我需要創建自定義行,這將是所有團隊總和的最後一行,而不是用戶。我已經寫入選擇將採取該數據,但我不知道如何添加將其添加到所有此選擇。MSSQL SELECT添加自定義行

SELECT 
     Drive.Owner 
     ,round(cast((sum(case when dock = 'yes' then 1 else 0 end)+sum(case when dock = 'old' then 1 else 0 end))as float)/cast(count(dock)as float)*100,2) as ava 
     ,round(cast(sum(case when dock = 'yes' then 1 else 0 end)as float)/cast((sum(case when dock = 'yes' then 1 else 0 end)+sum(case when dock = 'old' then 1 else 0 end))as float)*100,2) as uptodate 
     ,ROUND(sum(case when firsta != 'none' then 1 else 0 end)/ROUND(CAST(count(firsta)AS FLOAT),2)*100,2) nones1 
     ,ROUND(sum(case when seconda != 'none' then 1 else 0 end)/ROUND(CAST(count(seconda)AS FLOAT),2)*100,2) nones2 
     ,ISNULL(ROUND(Sum(CAST(Ontime AS FLOAT))/Sum(task_count), 2),0) as ontime 
     ,ISNULL(ROUND(AVG(CAST(actual AS FLOAT)), 2),0) as actual 


FROM (
SELECT 
    users.user_name+ ' ' +users.user_surname AS Owner 
    ,users.split as test 
    ,sc.firstbackup as firsta 
    ,sc.secondbackup as seconda 
    ,sc.documentation as dock 
    ,sc.active as active 
    ,(SELECT CASE WHEN non_sc = '1' THEN 'regular' ELSE 'sc' END) as sc_or_non_sc 
    FROM 
    [bstplanning].[dbo].[sc] 

INNER JOIN 
    [bstplanning].[dbo].[users] 
ON sc.user_id = users.user_id 

WHERE 
    users.split='Rep' 
) Drive 
left join 
(
SELECT 
    dbo.sc_data.track 
    ,dbo.users.split 
    ,dbo.sc_data.country 
    ,dbo.sc_data.client 
    ,dbo.sc_data.task_group 
    ,MAX(ISNULL(dbo.users.user_name,'') + ' ' + ISNULL(dbo.users.user_surname,'')) as Owner 
    ,AVG(CASE WHEN dbo.sc_data.ontime = 'on time' THEN 100 ELSE 0 END)*COUNT(dbo.sc_data.country) AS Ontime 
    ,AVG(CASE WHEN dbo.sc_data.accuracy = 'accurate' THEN 100 ELSE 0 END) AS actual 
    ,COUNT(dbo.sc_data.country) AS task_count 

FROM 
    [bstplanning].[dbo].[sc_data] 

INNER JOIN dbo.users 
    ON dbo.sc_data.user_id = dbo.users.user_id 

WHERE 
    dbo.sc_data.date >= '2016-06-01' AND dbo.sc_data.date <= '2016-06-15' AND dbo.sc_data.actual > 0 AND 
    ((dbo.sc_data.ontime='on time' OR dbo.sc_data.ontime = 'late') OR (dbo.sc_data.accuracy='accurate' OR dbo.sc_data.accuracy = 'error')) 

GROUP BY 
    dbo.sc_data.country 
    ,dbo.sc_data.client 
    ,dbo.sc_data.task_group 
    ,dbo.users.split 
    ,dbo.sc_data.track 
    ) Drive2 
    on Drive.Owner=Drive2.Owner 
    where sc_or_non_sc ='sc' 
    group by 
    Drive.Owner 

我需要添加的代碼。

select 
round(cast((sum(case when aa.documentation = 'yes' then 1 else 0 end)+sum(case when aa.documentation = 'old' then 1 else 0 end))as float)/cast(count(aa.documentation)as float)*100,2) as docas, 
round(cast(sum(case when aa.documentation = 'yes' then 1 else 0 end)as float)/cast((sum(case when aa.documentation = 'yes' then 1 else 0 end)+sum(case when aa.documentation = 'old' then 1 else 0 end))as float)*100,2) as uptodates 

from dbo.sc aa , dbo.users bb 

where bb.split='Rep' and bb.user_id=aa.user_id 

任何想法?

+2

UNION [ALL]? – Serg

+0

是的,它的工作。謝謝。 – Macb3th

回答

-2

你可以嘗試用分區上的OVER來聚合同一個select調用中的數據。

+0

雖然這可能是解決問題的有價值的提示,但 的答案確實需要比此更詳細的信息。請 [編輯]解釋這將如何解決問題。 或者,請考慮將其寫爲註釋。 –