在具有相似值的表上选择distinct和max

时间:2017-06-13 16:18:07

标签: sql sql-server tsql stored-procedures

我遇到了使用多个表的存储过程的问题,但我的一个表每个员工ID有多行。除非CoverageEndDate有一个日期,否则我需要该表将CoverageEndDate作为空值返回。然后我想要日期而不是空。当我使用不同的时候,如果两者都存在,我会得到其中一个

以下是1 empID

表格的一部分

enter image description here

这就是我想要的

enter image description here

我只能通过使用此基本调用来获取日期,但它在存储过程中不起作用: SELECT max(EmployeeID),max(CoverageEndDate) 来自EmployeeCoverageValue 其中EmployeeID = 5844

这是查询(我把--CoverageEndDate放在哪里引用):

        ALTER PROCEDURE [EligibilityExport_ExportCurrentEmployeeData_Select] 
-- Add the parameters for the stored procedure here
@TerminationDeadLine datetime = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @EligibilityExportDetailRows as [udtEligibilityExportDetail]
DECLARE @EligibilityExportEmployeePlans as [udtEmployeePlansToExport]
DECLARE @Header as varchar(MAX) = ''

SELECT DISTINCT @Header += name + CHAR(9)
  FROM sys.columns
 WHERE OBJECT_ID=OBJECT_ID('[fn_EligibilityExport_Export_RowData]')


INSERT @EligibilityExportDetailRows
    SELECT DISTINCT eed.[EligibilityExportID], eed.[EmployeePlanID], eed.[EmployeePlanValueID], 
        eed.[EmployeeCoverageValueID], eed.[EmployeeInformationPersonalValueID],
        eed.[DependentValueID], eed.[EmployeeID]
    FROM [EligibilityExportDetail] eed
        INNER JOIN (
                SELECT EligibilityExportDetailID = MAX(EligibilityExportDetailID), EmployeeID
                FROM [EligibilityExportDetail]
                GROUP BY EmployeeID
            ) AS eed_max 
            ON eed_max.EligibilityExportDetailID = eed.EligibilityExportDetailID

INSERT @EligibilityExportEmployeePlans
    SELECT DISTINCT EligibilityExportID = eedr.EligibilityExportID, 
        EmployeePlanID = MAX(EmployeePlanID), 
        EmployeeID = EmployeeID, 
        AsOfDate = ee.CreateDate
    FROM @EligibilityExportDetailRows eedr
        INNER JOIN [EligibilityExport] ee
            ON ee.EligibilityExportID = eedr.EligibilityExportID
    GROUP BY eedr.EligibilityExportID, eedr.EmployeeID, ee.CreateDate

--Select employees
SELECT DISTINCT Selected = CAST(0 as bit), vEI.EmployeeID, vEI.LastName, vEI.FirstName, vEI.MiddleName, 
    vEI.Address1, vEI.City, vEI.State, vEI.ZipCode, vEi.HireDate, vEi.TerminationDate, 
--CoverageEndDate
    CoverageEndDate = eCV.CoverageEndDate,
--CoverageEndDate
    DivisionNumber = dv.DivisionNumber,
    DivisionName = ISNULL(dv.DivisionName,'None'), 
    DepartmentName = ISNULL(dp.DepartmentName,'None'), 
    WorkcenterName = ISNULL(wc.WorkcenterName,'None'),
    ExportRowData = CAST (
         fnEE.EmployeeNumber + CHAR(9) +
         fnEE.LastName  + CHAR(9) +
         fnEE.FirstName  + CHAR(9) +
         fnEE.MiddleName  + CHAR(9) +
         fnEE.Suffix  + CHAR(9) +
         fnEE.SocialSecurityNumber  + CHAR(9) +
         fnEE.DateOfBirth  + CHAR(9) +
         fnEE.EE_Gender  + CHAR(9) +
         fnEE.Address1  + CHAR(9) +
         fnEE.Address2  + CHAR(9) +
         fnEE.City  + CHAR(9) +
         fnEE.State  + CHAR(9) +
         fnEE.ZipCode + CHAR(9) +
         fnEE.HireDate  + CHAR(9) +
         fnEE.Department  + CHAR(9) +
         fnEE.EffectiveDate  + CHAR(9) +
         fnEE.TerminationDate + CHAR(9) +
         fnEE.CoverageCode + CHAR(9) +
         fnEE.CobraEventCode + CHAR(9) +
         fnEE.Dependent2_Name + CHAR(9) +
         fnEE.Dependent2_DateOfBirth + CHAR(9) +
         fnEE.Dependent2_EffectiveDate + CHAR(9) +
         fnEE.Dependent2_TerminationDate + CHAR(9) +
         fnEE.Dependent2_Relationship + CHAR(9) +
         fnEE.Dependent2_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent2_Gender+ CHAR(9) +
         fnEE.Dependent3_Name + CHAR(9) +
         fnEE.Dependent3_DateOfBirth + CHAR(9) +
         fnEE.Dependent3_EffectiveDate + CHAR(9) +
         fnEE.Dependent3_TerminationDate + CHAR(9) +
         fnEE.Dependent3_Relationship + CHAR(9) +
         fnEE.Dependent3_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent3_Gender + CHAR(9) +
         fnEE.Dependent4_Name + CHAR(9) +
         fnEE.Dependent4_DateOfBirth + CHAR(9) +
         fnEE.Dependent4_EffectiveDate + CHAR(9) +
         fnEE.Dependent4_TerminationDate + CHAR(9) +
         fnEE.Dependent4_Relationship + CHAR(9) +
         fnEE.Dependent4_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent4_Gender + CHAR(9) +
         fnEE.Dependent5_Name + CHAR(9) +
         fnEE.Dependent5_DateOfBirth + CHAR(9) +
         fnEE.Dependent5_EffectiveDate + CHAR(9) +
         fnEE.Dependent5_TerminationDate + CHAR(9) +
         fnEE.Dependent5_Relationship + CHAR(9) +
         fnEE.Dependent5_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent5_Gender + CHAR(9) +
         fnEE.Dependent6_Name + CHAR(9) +
         fnEE.Dependent6_DateOfBirth + CHAR(9) +
         fnEE.Dependent6_EffectiveDate + CHAR(9) +
         fnEE.Dependent6_TerminationDate + CHAR(9) +
         fnEE.Dependent6_Relationship + CHAR(9) +
         fnEE.Dependent6_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent6_Gender + CHAR(9) +
         fnEE.Dependent7_Name + CHAR(9) +
         fnEE.Dependent7_DateOfBirth + CHAR(9) +
         fnEE.Dependent7_EffectiveDate + CHAR(9) +
         fnEE.Dependent7_TerminationDate + CHAR(9) +
         fnEE.Dependent7_Relationship + CHAR(9) +
         fnEE.Dependent7_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent7_Gender
    as varchar(MAX)),
    Header = REPLACE(@Header, 'EligibilityExportID' + CHAR(9) + 'EmployeePlanID' + CHAR(9), '')
FROM @EligibilityExportEmployeePlans ee_ep
    INNER JOIN v_EmployeeInformation vEI
        ON vEI.EmployeeID = ee_ep.EmployeeID
    INNER JOIN [fn_EligibilityExport_Export_RowData] 
        ( @EligibilityExportDetailRows, @EligibilityExportEmployeePlans ) fnEE
            ON fnEE.EmployeePlanID = ee_ep.EmployeePlanID
    LEFT OUTER JOIN Division dv
        ON vEI.DivisionID = dv.DivisionID
    LEFT OUTER JOIN Department dp
        ON vEI.DepartmentID = dp.DepartmentID
    LEFT OUTER JOIN Workcenter wc
        ON vEI.WorkcenterID = wc.WorkCenterID
--CoverageEndDate           
    LEFT OUTER JOIN EmployeeCoverageValue eCV
        ON eCV.EmployeeID = vEI.EmployeeID
WHERE @terminationDeadLine <= eCV.CoverageEndDate OR eCV.CoverageEndDate IS NULL
--CoverageEndDate
ORDER BY CoverageEndDate DESC, DivisionNumber, DepartmentName, WorkcenterName, LastName, FirstName

1 个答案:

答案 0 :(得分:0)

TY的帮助:)显然,部门,部门和工作中心已经从数据库中删除了一段时间。这是我最终在代码中做的事情:

    SELECT DISTINCT Selected = CAST(0 as bit), vEI.EmployeeID, vEI.LastName, vEI.FirstName, vEI.MiddleName, 
    vEI.Address1, vEI.City, vEI.State, vEI.ZipCode, vEi.HireDate, vEi.TerminationDate, 
    CoverageEndDate = ISNULL(MAX(eCV.CoverageEndDate), ''),
    DivisionNumber = vEI.DivisionNumber,
    --DivisionName = ISNULL(dv.DivisionName,'None'), 
    --DepartmentName = ISNULL(dp.DepartmentName,'None'), 
    --WorkcenterName = ISNULL(wc.WorkcenterName,'None'),
    ExportRowData = CAST (
         fnEE.EmployeeNumber + CHAR(9) +
         fnEE.LastName  + CHAR(9) +
         fnEE.FirstName  + CHAR(9) +
         fnEE.MiddleName  + CHAR(9) +
         fnEE.Suffix  + CHAR(9) +
         fnEE.SocialSecurityNumber  + CHAR(9) +
         fnEE.DateOfBirth  + CHAR(9) +
         fnEE.EE_Gender  + CHAR(9) +
         fnEE.Address1  + CHAR(9) +
         fnEE.Address2  + CHAR(9) +
         fnEE.City  + CHAR(9) +
         fnEE.State  + CHAR(9) +
         fnEE.ZipCode + CHAR(9) +
         fnEE.HireDate  + CHAR(9) +
         fnEE.Department  + CHAR(9) +
         fnEE.EffectiveDate  + CHAR(9) +
         fnEE.TerminationDate + CHAR(9) +
         fnEE.CoverageCode + CHAR(9) +
         fnEE.CobraEventCode + CHAR(9) +
         fnEE.Dependent2_Name + CHAR(9) +
         fnEE.Dependent2_DateOfBirth + CHAR(9) +
         fnEE.Dependent2_EffectiveDate + CHAR(9) +
         fnEE.Dependent2_TerminationDate + CHAR(9) +
         fnEE.Dependent2_Relationship + CHAR(9) +
         fnEE.Dependent2_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent2_Gender+ CHAR(9) +
         fnEE.Dependent3_Name + CHAR(9) +
         fnEE.Dependent3_DateOfBirth + CHAR(9) +
         fnEE.Dependent3_EffectiveDate + CHAR(9) +
         fnEE.Dependent3_TerminationDate + CHAR(9) +
         fnEE.Dependent3_Relationship + CHAR(9) +
         fnEE.Dependent3_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent3_Gender + CHAR(9) +
         fnEE.Dependent4_Name + CHAR(9) +
         fnEE.Dependent4_DateOfBirth + CHAR(9) +
         fnEE.Dependent4_EffectiveDate + CHAR(9) +
         fnEE.Dependent4_TerminationDate + CHAR(9) +
         fnEE.Dependent4_Relationship + CHAR(9) +
         fnEE.Dependent4_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent4_Gender + CHAR(9) +
         fnEE.Dependent5_Name + CHAR(9) +
         fnEE.Dependent5_DateOfBirth + CHAR(9) +
         fnEE.Dependent5_EffectiveDate + CHAR(9) +
         fnEE.Dependent5_TerminationDate + CHAR(9) +
         fnEE.Dependent5_Relationship + CHAR(9) +
         fnEE.Dependent5_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent5_Gender + CHAR(9) +
         fnEE.Dependent6_Name + CHAR(9) +
         fnEE.Dependent6_DateOfBirth + CHAR(9) +
         fnEE.Dependent6_EffectiveDate + CHAR(9) +
         fnEE.Dependent6_TerminationDate + CHAR(9) +
         fnEE.Dependent6_Relationship + CHAR(9) +
         fnEE.Dependent6_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent6_Gender + CHAR(9) +
         fnEE.Dependent7_Name + CHAR(9) +
         fnEE.Dependent7_DateOfBirth + CHAR(9) +
         fnEE.Dependent7_EffectiveDate + CHAR(9) +
         fnEE.Dependent7_TerminationDate + CHAR(9) +
         fnEE.Dependent7_Relationship + CHAR(9) +
         fnEE.Dependent7_SocialSecurityNumber + CHAR(9) +
         fnEE.Dependent7_Gender
    as varchar(MAX)),
    Header = REPLACE(@Header, 'EligibilityExportID' + CHAR(9) + 'EmployeePlanID' + CHAR(9), '')
FROM @EligibilityExportEmployeePlans ee_ep
    INNER JOIN v_EmployeeInformation vEI
        ON vEI.EmployeeID = ee_ep.EmployeeID
    INNER JOIN [fn_EligibilityExport_Export_RowData] 
        ( @EligibilityExportDetailRows, @EligibilityExportEmployeePlans ) fnEE
            ON fnEE.EmployeePlanID = ee_ep.EmployeePlanID
    --LEFT OUTER JOIN MAIN.Global.dbo.Division dv
    --  ON vEI.DivisionID = dv.DivisionID
    --LEFT OUTER JOIN MAIN.Global.dbo.Department dp
    --  ON vEI.DepartmentID = dp.DepartmentID
    --LEFT OUTER JOIN MAIN.Global.dbo.Workcenter wc
    --  ON vEI.WorkcenterID = wc.WorkCenterID

    LEFT OUTER JOIN EmployeeCoverageValue eCV
        ON eCV.EmployeeID = vEI.EmployeeID
WHERE @terminationDeadLine <= eCV.CoverageEndDate OR eCV.CoverageEndDate IS NULL
GROUP BY vEI.EmployeeID, vEI.LastName, vEI.FirstName, vEI.MiddleName, vEI.Address1, vEI.City, vEI.State, vEI.ZipCode, vEI.HireDate, vEI.TerminationDate, vEI.DivisionNumber,
         fnEE.EmployeeNumber,
         fnEE.LastName ,
         fnEE.FirstName ,
         fnEE.MiddleName ,
         fnEE.Suffix ,
         fnEE.SocialSecurityNumber ,
         fnEE.DateOfBirth ,
         fnEE.EE_Gender ,
         fnEE.Address1 ,
         fnEE.Address2 ,
         fnEE.City ,
         fnEE.State ,
         fnEE.ZipCode,
         fnEE.HireDate ,
         fnEE.Department ,
         fnEE.EffectiveDate ,
         fnEE.TerminationDate,
         fnEE.CoverageCode,
         fnEE.CobraEventCode,
         fnEE.Dependent2_Name,
         fnEE.Dependent2_DateOfBirth,
         fnEE.Dependent2_EffectiveDate,
         fnEE.Dependent2_TerminationDate,
         fnEE.Dependent2_Relationship,
         fnEE.Dependent2_SocialSecurityNumber,
         fnEE.Dependent2_Gender,
         fnEE.Dependent3_Name,
         fnEE.Dependent3_DateOfBirth,
         fnEE.Dependent3_EffectiveDate,
         fnEE.Dependent3_TerminationDate,
         fnEE.Dependent3_Relationship,
         fnEE.Dependent3_SocialSecurityNumber,
         fnEE.Dependent3_Gender,
         fnEE.Dependent4_Name,
         fnEE.Dependent4_DateOfBirth,
         fnEE.Dependent4_EffectiveDate,
         fnEE.Dependent4_TerminationDate,
         fnEE.Dependent4_Relationship,
         fnEE.Dependent4_SocialSecurityNumber,
         fnEE.Dependent4_Gender,
         fnEE.Dependent5_Name,
         fnEE.Dependent5_DateOfBirth,
         fnEE.Dependent5_EffectiveDate,
         fnEE.Dependent5_TerminationDate,
         fnEE.Dependent5_Relationship,
         fnEE.Dependent5_SocialSecurityNumber,
         fnEE.Dependent5_Gender,
         fnEE.Dependent6_Name,
         fnEE.Dependent6_DateOfBirth,
         fnEE.Dependent6_EffectiveDate,
         fnEE.Dependent6_TerminationDate,
         fnEE.Dependent6_Relationship,
         fnEE.Dependent6_SocialSecurityNumber,
         fnee.Dependent6_Gender,
         fnEE.Dependent7_Name,
         fnEE.Dependent7_DateOfBirth ,
         fnEE.Dependent7_EffectiveDate,
         fnEE.Dependent7_TerminationDate ,
         fnEE.Dependent7_Relationship,
         fnEE.Dependent7_SocialSecurityNumber,
         fnEE.Dependent7_Gender
ORDER BY CoverageEndDate DESC, DivisionNumber, LastName, FirstName