如何一起显示顺序和分组

时间:2014-05-07 09:21:43

标签: sql-server-2008

 SELECT DISTINCT
        GradeID, Division, vchSubject, Topic, competency, AVG(Column3) as 'Total',
        AVG(Played_Count) as 'KidCount',
        (AVG(Played_Count) / AVG(Column3)) * 100 as 'Percents'
   FROM (
         SELECT UserID, [User Name], GradeID, Division,
                vchSubject,stable.vchCompetency as Topic,
                stable.vchComptencyDesc as competency,
                column3, isnull(column4,0) as 'Played_Count',
                (isnull(column4, 0) / column3 ) * 100 as 'Percents'
           FROM
                (
                --some code
                ) as Mtable4
  GROUP BY Mtable4.GradeID, Division, vchSubject, Topic, competency
  ORDER BY Percents

我是sql查询的新手,我发现难以分组和订购;它给出了错误。任何人都可以帮我解决。

1 个答案:

答案 0 :(得分:0)

试试这样!

 ;with mtable4 as
 (
 SELECT DISTINCT
        GradeID, Division, vchSubject, Topic, competency,AVG(Column3) as 'Total',
        AVG(Played_Count) as 'KidCount',
        (AVG(Played_Count) / AVG(Column3)) * 100 as 'Percent'
   FROM (
         SELECT UserID, [User Name], GradeID, Division,
                vchSubject,stable.vchCompetency as Topic,
                stable.vchComptencyDesc as competency, column3,
                isnull(column4, 0) as 'Played_Count',
                (isnull(column4, 0) / column3) * 100 as 'Percents'
           FROM table
        )

 SELECT *
   FROM mtable4
  GROUP BY Mtable4.GradeID, Division, vchSubject, Topic, competency
  ORDER BY Percents