【问题标题】:Use group by clause使用 group by 子句
【发布时间】:2011-09-25 07:45:26
【问题描述】:

查询:

  SELECT sd.ident,sd.suniq, testc, subtestc, ts.testscore, 
         metadept, ts.takendt, 
         MAX(takendt) testdate
    FROM studemo sd, stutests ts, testdef td, udefstu ud
   WHERE ts.suniq =sd.suniq
     AND td.testuniq = ts.testuniq
     AND ts.suniq = ud.suniq
     AND td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
GROUP BY sd.suniq
ORDER BY suniq

得到以下错误:

消息 8120,第 16 级,状态 1,第 2 行 列“studemo.ident”在 选择列表,因为它不是 包含在任一聚合中 函数或 GROUP BY 子句。

我的目标是获得最新的考试成绩。在我尝试添加更多表格以包含更多学生信息之前,我是成功的。

【问题讨论】:

    标签: sql sql-server group-by


    【解决方案1】:

    您需要将那些不在聚合函数中的列添加到您的GROUP BY 中,以使您的 SQL 语句有效。

    GROUP BY sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt

    【讨论】:

      【解决方案2】:

      您需要将不使用聚合函数的剩余字段添加到您的Group By

          select sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt, max(takendt)testdate
      from studemo sd, stutests ts, testdef td, udefstu ud
      where ts.suniq =sd.suniq
      and td.testuniq = ts.testuniq
      and ts.suniq = ud.suniq
      and td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
      group by sd.suniq, sd.ident, testc, subtestc, ts.testscore, metadept, ts.takendt
      order by suniq
      

      【讨论】:

        【解决方案3】:
        select sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt, max(takendt)testdate
        from studemo sd, stutests ts, testdef td, udefstu ud
        where ts.suniq =sd.suniq
        and td.testuniq = ts.testuniq
        and ts.suniq = ud.suniq
        and td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
        group by sd.ident,sd.suniq, testc, subtestc, ts.testscore, metadept, ts.takendt
        order by suniq
        

        评论:上面的代码就是@Neil的意思

        【讨论】:

          【解决方案4】:

          我怀疑您想显示每个学生的 maxtakedt,即使他们已经参加了多项测试,例如如果他们参加了两次测试,那么您要显示两个测试日期,并在每一行中显示最大日期。如果是这种情况,那么按所有其他列分组将无济于事。也许试试这个,假设 SQL Server 2005 或更高版本:

                  WITH md AS
                  (
                      SELECT ts.suniq, maxdate = MAX(ts.takendt)
                          FROM dbo.stutests AS ts
                      INNER JOIN
                          dbo.testdef AS td
                          ON td.testuniq = ts.testuniq
                      WHERE
                          td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
                      GROUP BY ts.suniq
                  )
                  SELECT
                      sd.ident,
                      sd.suniq,
                      testc, -- which table does this come from? why no prefix?
                      subtestc, -- which table does this come from? why no prefix?
                      ts.testscore,
                      metadept, -- which table does this come from? why no prefix?
                      ts.takendt,
                      md.maxdate
                  FROM
                      dbo.studemo AS sd
                  INNER JOIN
                      dbo.stutests AS ts
                      ON sd.suniq = ts.suniq
                  INNER JOIN
                      dbo.testdef AS td
                      ON td.testuniq = ts.testuniq
                  INNER JOIN
                      dbo.udefstu AS ud
                      ON ts.suniq = ud.suniq
                  INNER JOIN md
                      ON md.suniq = sd.suniq
                  WHERE
                      td.testuniq IN ('2000089', '2000090', '2000091', '2000092')
                  ORDER BY
                      sd.suniq; -- you forgot a prefix here too
                      -- could cause problems if you change the query later
          

          【讨论】:

          • 我根据以下建议更改了我的查询:您需要将那些不在聚合函数中的列添加到您的 GROUP BY 中,以使您的 SQL 语句有效。按 sd.ident、sd.suniq、testc、subtestc、ts.testscore、metadept、ts.takendt 分组
          • 好吧,我怀疑结果不会很理想(因为所有这些列值的每个排列都会有一行),但谁知道...
          • 我的 testuniq 表示所参加的测试级别。一旦通过级别,学生将参加下一个级别的测试。因此,当我运行查询时,当我只想查看最后一次测试时,我会看到所有级别的结果。有没有办法可以修改查询来做到这一点???
          • 您需要提供一些示例数据和所需的结果。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-12-14
          • 1970-01-01
          • 1970-01-01
          • 2012-04-20
          • 2019-10-03
          • 2018-03-23
          • 1970-01-01
          相关资源
          最近更新 更多