MSSQL SELECT添加自定义行

时间:2016-06-17 07:43:19

标签: sql sql-server select row

选择此选项。它按团队生成用户列表并汇总他们的数据。我需要创建自定义行,它将是所有团队总和的最后一行,而不是用户。我已经编写了可以获取该数据的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

有什么想法吗?

1 个答案:

答案 0 :(得分:-2)

你可以尝试在PARTITION上使用OVER来聚合同一个select调用中的数据。