sql查询用于排序多个表和PIVOT

时间:2017-04-13 10:12:37

标签: sql sql-server-2008

sql pivot with dynamic columns我在这里附上了我对表和数据的查询,

     /****** Object:  Table [dbo].[Company]    Script Date: 13/04/2017 3:37:15 PM ******/
CREATE TABLE [dbo].[Company](
    [Comp ID] [int] NULL,
    [Company Name] [nvarchar](50) NULL
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Designation]    Script Date: 13/04/2017 3:37:15 PM ******/
CREATE TABLE [dbo].[Designation](
    [Design ID] [int] NULL,
    [Comp ID] [int] NULL,
    [Designation] [nvarchar](50) NULL,
    [Design Code] [int] NULL
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Employee]    Script Date: 13/04/2017 3:37:15 PM ******/
CREATE TABLE [dbo].[Employee](
    [Emp ID] [int] NULL,
    [Comp ID] [int] NULL,
    [Design ID] [int] NULL,
    [Employee Name] [nvarchar](50) NULL,
    [Employee Code] [int] 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], [Design Code]) VALUES (1, 1, N'CEO', 2)
GO
INSERT [dbo].[Designation] ([Design ID], [Comp ID], [Designation], [Design Code]) VALUES (2, 1, N'HR', 1)
GO
INSERT [dbo].[Designation] ([Design ID], [Comp ID], [Designation], [Design Code]) VALUES (3, 2, N'CEO', 1)
GO
INSERT [dbo].[Designation] ([Design ID], [Comp ID], [Designation], [Design Code]) VALUES (3, 2, N'Developer', 2)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (1, 1, 1, N'Test emp1', 101)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (2, 1, 1, N'Test emp2', 102)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (3, 1, 2, N'Test emp3', 103)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (4, 1, 2, N'Test emp4', 104)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (5, 1, 1, N'Test emp5', 105)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (6, 1, 2, N'Test emp6', 106)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (7, 2, 1, N'Test emp7', 107)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (8, 2, 1, N'Test emp8', 108)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (9, 2, 2, N'Test emp9', 109)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (10, 2, 2, N'Test emp10', 110)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (11, 2, 1, N'Test emp11', 111)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (12, 2, 1, N'Test emp12', 112)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (13, 1, 1, N'Test emp13', 113)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (14, 1, 2, N'Test emp14', 114)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (15, 2, 1, N'Test emp15', 115)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (16, 2, 2, N'Test emp16', 116)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (17, 1, 1, N'Test emp17', 117)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (18, 1, 2, N'Test emp18', 118)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (19, 2, 1, N'Test emp19', 119)
GO
INSERT [dbo].[Employee] ([Emp ID], [Comp ID], [Design ID], [Employee Name], [Employee Code]) VALUES (20, 2, 2, N'Test emp20', 120)
GO

我有3个表作为公司,指定,员工

我需要一个查询,为所有公司订购最新指定(设计代码)的最新4名员工(按员工代码排序),然后按照设计代码的降序,然后按员工代码的降序选择,并为每家公司选择前4名记录 结果需要像

那样透视

例如:

CompanyID    Emp1     Emp2     Emp3     Emp4
----------
1            118      114      106      103

我尝试了类似的东西,

select       
  emp.[Comp ID],
  emp.[Design ID],
  emp.[Emp ID],
  emp.[Employee Code],
  design.[Design Code],
  emp.[Employee Code],
  comp.[Company Name],
  design.Designation,
  emp.[Employee Name]
  from Employee emp    
  join Designation design on design.[Design ID]=emp.[Design ID]
  join Company comp on comp.[Comp ID]=emp.[Comp ID]
  order by comp.[Comp ID] desc, design.[Design Code] desc, emp.[Employee Code] desc

我需要前4名员工为emp1,emp2,emp3和emp4转换为每个公司,有可能2个可能属于一个名称,3个来自其他和相同的员工代码可以重复使用不同的公司代码,它需要按公司代码排序,然后按员工代码排序

2 个答案:

答案 0 :(得分:1)

希望这就是你要找的东西,

SELECT [comp id], 
       emp1, 
       emp2, 
       emp3, 
       emp4 
FROM   (SELECT [comp id], 
               [employee code], 
               'Emp' + Cast(rn AS CHAR(10)) AS Empname 
        FROM   (SELECT A.[comp id], 
                       A.[employee code], 
                       Row_number() 
                         OVER ( 
                           partition BY A.[comp id] 
                           ORDER BY B.[comp id], C.[design code] 
                         DESC, A.[employee code] DESC) 
                               AS rn 
                FROM   dbo.employee A 
                       INNER JOIN dbo.company B 
                               ON A.[comp id] = B.[comp id] 
                       INNER JOIN dbo.designation C 
                               ON C.[design code] = A.[design id] 
                                  AND C.[comp id] = B.[comp id])A 
        WHERE  rn <= 4)B 
       PIVOT ( Max([employee code]) 
             FOR empname IN (emp1, 
                             emp2, 
                             emp3, 
                             emp4) ) pv 

结果如下所示,

comp id    emp1 emp2 emp3 emp4
1          118  114  106  104
2          120  116  110  109

注意:根据我的理解

答案 1 :(得分:0)

试试这样:

function SearchGrid(txtSearchSAP, grdSapDetails) {

            if ($("[id *=" + txtSearchSAP + " ]").val() != "") {
            var count = 0;
                $("[id *=" + grdSapDetails + " ]").children
                ('tbody').children('tr').each(function () {
                    $(this).show();
                });
                $("[id *=" + grdSapDetails + " ]").children
                ('tbody').children('tr').each(function () {
                    var match = false;
                    $(this).children('td').each(function () {
                        if ($(this).text().toUpperCase().indexOf($("[id *=" +
                    txtSearchSAP + " ]").val().toUpperCase()) > -1) {
                            match = true;
                            count++;
                            return false;
                        }
                    });
                    if (match) {

                        $(this).show();
                        $(this).children('th').show();
                    }
                    else {

                        $(this).hide();
                        $(this).children('th').show();

                    }
                });


                $("[id *=" + grdSapDetails + " ]").children('tbody').
                        children('tr').each(function (index) {

                            if (index == 0)
                            {
                               $(this).show();
                            }



                        });

                        if(count==0)
                        {
                        alert("No Matching Records");
                        }
            }
            else {
                $("[id *=" + grdSapDetails + " ]").children('tbody').
                        children('tr').each(function () {
                            $(this).show();
                            count=0;
                        });
                }
        }

        $(document).on("keyup", function () {                        
            SearchGrid('txtSearchSAP', 'grdSapDetails');
        });