繁体   English   中英

使用MS sql服务器将所有联合的选择查询的所有产值求和

[英]Sum all produced value of select query with union all and distinct using MS sql server

我从查询中产生了具有日期值的数据。 现在我想要的是获取总天数并放在最后一行,这就是为什么我使用UNION ALL 问题是如何获取仅由查询产生的天总数。

下面是查询:

USE  ANR_Payroll
Select 
Distinct a.[from],
(Select top 1 [to] FROM [ANR_Payroll].[dbo].[tblPayrollTransactionEntry] Z where A.[From] =Z.[From] and A.employeecode = Z.EmployeeCode ) As 'TO',
'***' as 'Total Days Worked',
(Select top 1 Employeecode FROM [ANR_Payroll].[dbo].    [tblPayrollTransactionEntry] Z where A.[from] =Z.[from] and A.employeecode =     Z.EmployeeCode ) As 'Employeecode',
B.LastName,
B.FirstName,
B.Middlename,
B.Levelcode,
(Select top 1 z.[BatchCode] FROM [ANR_Payroll].[dbo].    [tblPayrollTransactionEntry] Z where A.[from] =Z.[from] and A.employeecode =     Z.EmployeeCode ) As 'BatchCode' ,
(Select top 1 Z.EarningCode FROM [ANR_Payroll].[dbo].    [tblPayrollTransactionEntry] Z where A.[from] =Z.[From] and A.employeecode = Z.EmployeeCode ) As 'Earning Code' ,
(Select top 1 Z.Rate FROM [ANR_Payroll].[dbo].[tblPayrollTransactionEntry] Z     where A.[from] =Z.[From] and A.employeecode = Z.EmployeeCode ) As 'Rate',
(Select top 1 Z.Ratetype FROM [ANR_Payroll].[dbo].    [tblPayrollTransactionEntry] Z where A.[from] =Z.[From] and A.employeecode =     Z.EmployeeCode ) As 'RateType',
(Select top 1 A.[noofunits] / 8 FROM [ANR_Payroll].[dbo].    [tblPayrollTransactionEntry] Z where A.[from] =Z.[From] and A.employeecode =     Z.EmployeeCode ) as 'DAYS',
'' Tenure,
'A' Orderkey 
from 
   tblPayrolltransactionEntry as A 
   inner join 
   tblEmployeeMainInfo as B ON a.EmployeeCode = b.EmployeeCode 
  where B.Employeecode like '%0000011350%'and EarningCode = 'SF' and PayType     = 'PayOut'  
   UNION ALL 
 Select
    CASE when '' = '1900-01-01 00:00:00' THEN '**' END,
CASE when '' = '1900-01-01 00:00:00' THEN '**' END, 
'Total Days Worked', 
A.Employeecode,
'',
'',
'',
'',
'',
'',
'',
'',
(SUM(NoOfUNits) / 8) as 'DAYS',
((SUM(NoOfUNits) / 8) / 366) as 'Tenure',     
'B' Orderkey
from tblPayrolltransactionEntry as A 
inner join tblEmployeeMainInfo as B ON a.EmployeeCode = b.EmployeeCode 
where B.Employeecode like '%0000011350%'and EarningCode = 'SF' and PayType =     'PayOut'  
    Group by a.employeecode 
 ORDER by [Employeecode],Orderkey

附件是我使用该查询的输出。

总计是从所有值中获得的,而无需使用不同的 在此处输入图片说明

您确实想在报表的报告层中执行此操作,这不是在SQL中执行此操作的最佳实践。

如果您坚持要在SQL中执行此操作,那么我采用的方法是复制原始查询并将其成为子查询。 您没有提到它将如何运行,以防万一它将成为一种视图,我将不采用将使用临时表的方法。

我这样做是为了自由地重写查询,您不应该像在SELECT查询中那样打表,这是完全不必要的,并且在处理过程中会增加很多成本。

您将需要在UNION的第二部分中重新访问“ Tenure”字段的计算,因为我无法理解您在此处尝试执行的操作。

USE  ANR_Payroll


SELECT DISTINCT
a.[from]
,[to] As 'TO'
,'***' as 'Total Days Worked'
,Z.EmployeecodeAs 'Employeecode'
,B.LastName
,B.FirstName
,B.Middlename
,B.Levelcode
,Z.[BatchCode] As 'BatchCode' 
,Z.EarningCode As 'Earning Code' 
,Z.Rate As 'Rate'
,Z.Ratetype As 'RateType',
A.[noofunits] / 8 as 'DAYS'
,'' Tenure
,'A' Orderkey 
FROM tblPayrolltransactionEntry as A 
INNER JOIN tblEmployeeMainInfo as B 
    ON a.EmployeeCode = b.EmployeeCode 
INNER JOIN (
                SELECT TOP 1 
                [from]
                , EmployeeCode
                , [to]
                , BatchCode
                , EarningCode
                , Rate
                , RateType
                , noofunits 
                FROM [ANR_Payroll].[dbo].[tblPayrollTransactionEntry]
            ) Z 
    ON A.[from] = Z.[from] 
    AND A.employeecode = Z.EmployeeCode
WHERE B.Employeecode like '%0000011350%'
    AND EarningCode = 'SF' 
    AND PayType     = 'PayOut'  


   UNION ALL 


SELECT
MIN(sub.[from]) 
,MAX(sub.[to])
,'Total Days Worked'
,A.Employeecode
,''
,''
,''
,''
,''
,''
,''
,''
,SUM(sub.[DAYS]) as 'DAYS'
,((SUM(NoOfUNits) / 8) / 366) as 'Tenure'
,'B' Orderkey
FROM 
    (
        SELECT DISTINCT
        a.[from]
        ,[to] As 'TO'
        ,'***' as 'Total Days Worked'
        ,Z.EmployeecodeAs 'Employeecode'
        ,B.LastName
        ,B.FirstName
        ,B.Middlename
        ,B.Levelcode
        ,Z.[BatchCode] As 'BatchCode' 
        ,Z.EarningCode As 'Earning Code' 
        ,Z.Rate As 'Rate'
        ,Z.Ratetype As 'RateType',
        A.[noofunits] / 8 as 'DAYS'
        ,'' Tenure
        ,'A' Orderkey 
        FROM tblPayrolltransactionEntry as A 
        INNER JOIN tblEmployeeMainInfo as B 
            ON a.EmployeeCode = b.EmployeeCode 
        INNER JOIN (
                        SELECT TOP 1 
                        [from]
                        , EmployeeCode
                        , [to]
                        , BatchCode
                        , EarningCode
                        , Rate
                        , RateType
                        , noofunits 
                        FROM [ANR_Payroll].[dbo].[tblPayrollTransactionEntry]
                    ) Z 
            ON A.[from] = Z.[from] 
            AND A.employeecode = Z.EmployeeCode
        WHERE B.Employeecode like '%0000011350%'
            AND EarningCode = 'SF' 
            AND PayType     = 'PayOut'  
    ) sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM