如何在一个表中显示两个记录?

时间:2015-03-04 08:15:38

标签: asp.net sql-server

我有一个如下程序:

CREATE PROCEDURE studentpvt2
AS
BEGIN

SELECT 
     TOP 1 StudentName, 
     EC1,
     EC2,
     EC3,
     EC4,
     EC5,
     TotalMarks=EC1+EC2+EC3+EC4+EC5 
 FROM Student pivot
(
SUM (Marks) FOR subject IN ([EC1],[EC2],[EC3],[EC4],[EC5],[TotalMarks])
)
AS pivotTable ORDER  BY TotalMarks DESC

SELECT 
     TOP 1 StudentName,
     EC1,
     EC2,
     EC3,
     EC4,
     EC5,
     TotalMarks=EC1+EC2+EC3+EC4+EC5 
 FROM Student pivot
(
SUM (Marks) FOR subject IN ([EC1],[EC2],[EC3],[EC4],[EC5],[TotalMarks])
)
AS pivotTable ORDER  BY TotalMarks ASC;

END

执行此操作时输出如下:

studentName EC1 EC2 EC3 EC4 EC5 TotalMarks
  Smita     76  45  67  56  76  320   

studentName EC1 EC2 EC3 EC4 EC5 TotalMarks
  Rajesh    34  56  12  45  23  170

我希望他们在一张表中显示两条记录,所以请帮助我如何获得该输出。这是我的第一个问题,如果有任何不妥,请原谅。

原始表是:

StudentID StudentName Subject Marks 1 Savita EC1 50 1 Savita EC2 55 1 Savita EC3 45 1 Savita EC4 34 1 Savita EC5 23 2 Rajesh EC1 34 2 Rajesh EC2 56 2 Rajesh EC3 12 2 Rajesh EC4 45 2 Rajesh EC5 23 3 Smita EC1 76 3 Smita EC2 45 3 Smita EC3 67 3 Smita EC4 56 3 Smita EC5 76 4 Rahul EC1 66 4 Rahul EC2 34 4 Rahul EC3 22 4 Rahul EC4 18 4 Rahul EC5 33

3 个答案:

答案 0 :(得分:1)

使用UNIONUNION ALL。您还应该将ORDER BY更改为WHERE子句以防止出错。见下文:

CREATE PROCEDURE studentpvt2
AS
BEGIN

SELECT 
     TOP 1 StudentName, 
     EC1,
     EC2,
     EC3,
     EC4,
     EC5,
     TotalMarks=EC1+EC2+EC3+EC4+EC5 
 FROM Student pivot
(
SUM (Marks) FOR subject IN ([EC1],[EC2],[EC3],[EC4],[EC5],[TotalMarks])
)
AS pivotTable WHERE TotalMarks = (
                 SELECT TOP 1 Sum(Marks) s_m
                 FROM   Student 
                 GROUP  BY StudentName
                 ORDER  BY s_m DESC)
UNION ALL
SELECT 
     TOP 1 StudentName,
     EC1,
     EC2,
     EC3,
     EC4,
     EC5,
     TotalMarks=EC1+EC2+EC3+EC4+EC5 
 FROM Student pivot
(
SUM (Marks) FOR subject IN ([EC1],[EC2],[EC3],[EC4],[EC5],[TotalMarks])
)
AS pivotTable WHERE TotalMarks = (
                 SELECT TOP 1 Sum(Marks) s_m
                 FROM   Student 
                 GROUP  BY StudentName
                 ORDER  BY s_m ASC);

END

答案 1 :(得分:1)

CREATE PROCEDURE studentpvt2
AS
BEGIN

SELECT * FROM (SELECT 
     TOP 1 StudentName, 
     EC1,
     EC2,
     EC3,
     EC4,
     EC5,
     TotalMarks=EC1+EC2+EC3+EC4+EC5 
 FROM Student pivot
(
SUM (Marks) FOR subject IN ([EC1],[EC2],[EC3],[EC4],[EC5],[TotalMarks])
)
AS pivotTable order by [TotalMarks])A


UNION ALL

SELECT * FROM (SELECT 
     TOP 1 StudentName, 
     EC1,
     EC2,
     EC3,
     EC4,
     EC5,
     TotalMarks=EC1+EC2+EC3+EC4+EC5 
 FROM Student pivot(
SUM (Marks) FOR subject IN ([EC1],[EC2],[EC3],[EC4],[EC5],[TotalMarks])
)
AS pivotTable order by [TotalMarks] DESC)B

END

答案 2 :(得分:1)

不是在两个Union查询之间使用pivot,而是使用单个数据透视并过滤旋转后的最大值和最小值

您无法在order byUnion

中间使用Union All
SELECT *
FROM   (SELECT StudentName,
               EC1,
               EC2,
               EC3,
               EC4,
               EC5,
               TotalMarks=EC1 + EC2 + EC3 + EC4 + EC5
        FROM   Yourtable
               PIVOT ( Sum(Marks)
                     FOR subject IN([EC1],
                                    [EC2],
                                    [EC3],
                                    [EC4],
                                    [EC5],
                                    [TotalMarks]) ) AS pivotTable) a
WHERE  TotalMarks = (SELECT TOP 1 Sum(Marks) s_m
                     FROM   Yourtable
                     GROUP  BY StudentName
                     ORDER  BY s_m DESC)
        OR TotalMarks = (SELECT TOP 1 Sum(Marks) s_m
                         FROM   Yourtable
                         GROUP  BY StudentName
                         ORDER  BY s_m ASC) 

或使用stacked CTE

;WITH min_cte
     AS (SELECT TOP 1 StudentName,
                      EC1,EC2,EC3,EC4,EC5,
                      TotalMarks=EC1 + EC2 + EC3 + EC4 + EC5
         FROM   Yourtable
                PIVOT ( Sum(Marks)
                      FOR subject IN([EC1],
                                     [EC2],
                                     [EC3],
                                     [EC4],
                                     [EC5],
                                     [TotalMarks]) ) AS pivotTable
         ORDER  BY TotalMarks ASC),
     max_cte
     AS (SELECT TOP 1 StudentName,
                      EC1,EC2,EC3,EC4,EC5,
                      TotalMarks=EC1 + EC2 + EC3 + EC4 + EC5
         FROM   Yourtable
                PIVOT ( Sum(Marks)
                      FOR subject IN([EC1],
                                     [EC2],
                                     [EC3],
                                     [EC4],
                                     [EC5],
                                     [TotalMarks]) ) AS pivotTable
         ORDER  BY TotalMarks DESC) SELECT *
FROM   min_cte
UNION ALL
SELECT *
FROM   max_cte