从一组行中选择仅返回一行?

时间:2019-11-08 19:29:26

标签: sql-server tsql

我正在使用AdventureWork2012数据库练习SQL,并且创建了一个员工信息表。这是我的查询和结果。

FROM   (SELECT DISTINCT p.ID, 
       p.p_name,
       COUNT(*) OVER (PARTITION BY p.p_name, p.ID) AS Cnt
       FROM prescription AS p
       ORDER BY Cnt DESC) as Tabela
INNER JOIN(
        SELECT Tabela2.ID, MAX(Tabela2.Cnt)
        FROM (SELECT DISTINCT p.ID, 
              p.p_name,
              COUNT(*) OVER (PARTITION BY p.p_name, p.ID) AS Cnt
              FROM prescription AS p
              ORDER BY Cnt DESC) as Tabela2
        GROUP BY Tabela2.ID
)
SELECT 
    [e].[BusinessEntityID]
    , [e].[JobTitle]
    , CONCAT (p.FirstName, ' ', p.LastName)             AS Full_Name
    , dep.Name                                          AS Department

    , DATENAME(dw, edh.StartDate) + ', ' +
      DATENAME(month, edh.StartDate) + ' ' +
      DATENAME(day, edh.StartDate) + ', ' +                 
      DATENAME(year, edh.StartDate)                     AS 'Start_Date'

    , CASE 
        WHEN edh.EndDate IS NULL
        THEN CONVERT(VARCHAR, GETDATE(), 101)       
        ELSE edh.EndDate
      END                                                                       AS End_Date

    , IIF(LEFT(pp.PhoneNumber, 3) = '1 (', ' ', LEFT(pp.PhoneNumber, 3))        AS Area_Code
    , eph.Rate

    , CASE CONVERT(CHAR(10), eph.PayFrequency)
        WHEN 2 THEN 'Biweekly'
        ELSE 'Weekly'
      END                                               AS PayFrequency

    , IIF(e.SalariedFlag = 1, 'Y', 'N')                 AS 'SalaryEmployee'

    , CASE 
        WHEN e.SalariedFlag = 1
        THEN ROUND((eph.Rate*40)*52, 2)
        ELSE NULL
      END                                               AS YearlySalary
--INTO EmpInfo

FROM [HumanResources].[Employee]    AS e
INNER JOIN  [HumanResources].[EmployeeDepartmentHistory] AS edh
    ON edh.BusinessEntityID = e.BusinessEntityID
INNER JOIN [Person].[Person] AS p 
    ON p.BusinessEntityId = e.BusinessEntityID
INNER JOIN [Person].[PersonPhone] AS pp
    ON pp.BusinessEntityID = e.BusinessEntityID
INNER JOIN [HumanResources].[Department] AS dep
    ON dep.DepartmentID = edh.DepartmentID
INNER JOIN [HumanResources].[EmployeePayHistory] AS eph
    ON eph.BusinessEntityID = e.BusinessEntityID


ORDER BY e.BusinessEntityID;

如您所见,罗伯·沃尔特斯(Rob Walters)被列出6次,因为他获得了3次加薪,并且是两个部门的一部分。有什么办法我只能返回他所在的两个部门的Rob Walter的最新/最高时薪吗?

1 个答案:

答案 0 :(得分:2)

类似的事情应该很接近。我删除了日期格式,因为它确实属于前端。另外,您也不想使用字符串文字作为列别名。它令人困惑,并且已在sql server 2016中弃用。https://docs.microsoft.com/en-us/sql/database-engine/deprecated-database-engine-features-in-sql-server-2016?view=sql-server-ver15

select *
from 
(
    SELECT 
        [e].[BusinessEntityID]
        , [e].[JobTitle]
        , CONCAT (p.FirstName, ' ', p.LastName) AS Full_Name
        , dep.Name AS Department
        , Start_Date = isnull(edh.StartDate, getdate())
        , End_Date = isnull(edh.EndDate, getdate())
        , IIF(LEFT(pp.PhoneNumber, 3) = '1 (', ' ', LEFT(pp.PhoneNumber, 3)) AS Area_Code
        , eph.Rate
        , CASE CONVERT(CHAR(10), eph.PayFrequency)
            WHEN 2 THEN 'Biweekly'
            ELSE 'Weekly'
          END AS PayFrequency
        , IIF(e.SalariedFlag = 1, 'Y', 'N')                 AS SalaryEmployee
        , CASE 
            WHEN e.SalariedFlag = 1
            THEN ROUND((eph.Rate*40)*52, 2)
            ELSE NULL
          END                                               AS YearlySalary
        , RowNum = ROW_NUMBER()over(partition by e.BusinessEntityID order by edh.StartDate desc) --or whatever column makes sense here
    --INTO EmpInfo
    FROM [HumanResources].[Employee]    AS e
    INNER JOIN  [HumanResources].[EmployeeDepartmentHistory] AS edh
        ON edh.BusinessEntityID = e.BusinessEntityID
    INNER JOIN [Person].[Person] AS p 
        ON p.BusinessEntityId = e.BusinessEntityID
    INNER JOIN [Person].[PersonPhone] AS pp
        ON pp.BusinessEntityID = e.BusinessEntityID
    INNER JOIN [HumanResources].[Department] AS dep
        ON dep.DepartmentID = edh.DepartmentID
    INNER JOIN [HumanResources].[EmployeePayHistory] AS eph
        ON eph.BusinessEntityID = e.BusinessEntityID
) x
where x.RowNum = 1
ORDER BY x.BusinessEntityID;