sql group by order by和top in a same query

时间:2017-04-11 08:38:28

标签: sql-server

我有三个表CompanyDesignationEmployee

我需要一个查询来从公司分组的所有名称中选择前2名员工尝试过并附上我的截图,请帮我查询

CREATE TABLE [dbo].[Company](
    [Comp ID] [int] NULL,
    [Company Name] [nvarchar](50) NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Designation](
    [Design ID] [int] NULL,
    [Comp ID] [int] NULL,
    [Designation] [nvarchar](50) NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Employee](
    [Emp ID] [int] NULL,
    [Comp ID] [int] NULL,
    [Design ID] [int] NULL,
    [Employee Name] [nvarchar](50) NULL
) ON [PRIMARY]

GO
INSERT [dbo].[Company] ([Comp ID], [Company Name]) VALUES (1, N'Company 1')
GO
INSERT [dbo].[Company] ([Comp ID], [Company Name]) VALUES (2, N'Company 2')
GO
INSERT [dbo].[Designation] ([Design ID], [Comp ID], [Designation]) VALUES (1, 1, N'CEO')
GO
INSERT [dbo].[Designation] ([Design ID], [Comp ID], [Designation]) VALUES (2, 1, N'HR')
GO
INSERT [dbo].[Designation] ([Design ID], [Comp ID], [Designation]) VALUES (3, 2, N'CEO')
GO
INSERT [dbo].[Designation] ([Design ID], [Comp ID], [Designation]) VALUES (3, 2, N'Developer')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (1, 1, 1, N'Test emp1')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (2, 1, 1, N'Test emp2')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (3, 1, 2, N'Test emp3')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (4, 1, 2, N'Test emp4')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (5, 1, 1, N'Test emp5')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (6, 1, 2, N'Test emp6')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (7, 2, 1, N'Test emp7')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (8, 2, 1, N'Test emp8')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (9, 2, 2, N'Test emp9')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (10, 2, 2, N'Test emp10')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (11, 2, 1, N'Test emp11')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (12, 2, 1, N'Test emp12')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (13, 1, 1, N'Test emp13')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (14, 1, 2, N'Test emp14')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (15, 2, 1, N'Test emp15')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (16, 2, 2, N'Test emp16')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (17, 1, 1, N'Test emp17')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (18, 1, 2, N'Test emp18')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (19, 2, 1, N'Test emp19')
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name]) VALUES (20, 2, 2, N'Test emp20')
GO

我试过的查询是,

select 
emp.[Emp ID],
comp.[Company Name],
design.[Designation],
emp.[comp id],
emp.[Employee Name]
from employee as emp
join designation as design on design.[Design ID]=emp.[Design ID]
join company as comp on comp.[Comp ID]=emp.[Comp ID]

order by emp.[Comp ID] desc, emp.[Design ID] desc, emp.[Emp ID] desc

2 个答案:

答案 0 :(得分:1)

使用CTE可以达到预期效果

;WITH CTE AS (
    select emp.[Emp ID], 
           comp.[Company Name], 
           design.[Designation], 
           emp.[comp id], 
           emp.[Employee Name],
           ROW_NUMBER() OVER(PARTITION BY emp.[comp id],design.[Designation] ORDER BY design.[Design ID],emp.[Emp ID])   RN
   from employee as emp 
   join designation as design on design.[Design ID]=emp.[Design ID] 
   join company as comp on comp.[Comp ID]=emp.[Comp ID] 
)
SELECT * 
FROM CTE 
WHERE RN <= 4

答案 1 :(得分:0)

我建议你在子查询中使用ROW_NUMBER()来选择每个公司和名称返回的前2行。

SELECT
    [Company Name],
    [Designation],
    [comp id],
    [Employee Name]
FROM
(
    SELECT emp.[Emp ID],
        comp.[Company Name],
        design.[Designation],
        emp.[comp id],
        emp.[Employee Name],
        ROW_NUMBER() OVER (PARTITION BY emp.[Comp ID], emp.[Design ID] ORDER BY emp.[Comp ID] desc, emp.[Design ID] desc, emp.[Emp ID] desc) RowNum
    from employee as emp
        join designation as design on design.[Design ID]=emp.[Design ID]
        join company as comp on comp.[Comp ID]=emp.[Comp ID]
) a
WHERE RowNum <=2