JBTALKS.CC

标题: 【已解决】 SQL Group By 问题 [打印本页]

作者: shinn1080    时间: 2012-8-15 01:52 PM
标题: 【已解决】 SQL Group By 问题
本帖最后由 shinn1080 于 2012-8-16 10:11 AM 编辑

各位好
  1. select empPhoto.ImageData,upduser, count(upduser) as totalView   from efview  left join empPhoto on empPhoto.emp_id = efview.upduser where docid = '1' and version ='1.00' group by efview.updUser
复制代码
出现的error如下
Msg 8120, Level 16, State 1, Line 1
Column 'empPhoto.ImageData' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

可是拿掉empPhoto.ImageData就没事了,但是我非常需要这个field。不太清楚group by的用法,参考网上的资料还是没头绪。
请高手指点。
作者: weeming21    时间: 2012-8-15 06:16 PM
你确定 empPhoto 里有 ImageData 这个field?
最好你能把两个Table的field都写出来,不然很难猜测实际情况
作者: AhPang    时间: 2012-8-15 10:16 PM
  1. select empPhoto.ImageData,upduser, count(upduser) as totalView   from efview  left join empPhoto on empPhoto.emp_id = efview.upduser where docid = '1' and version ='1.00' group by efview.updUser, empPhoto.ImageData
复制代码
试试看可以吗?太久没用sql command没办法直接帮你test。
作者: shinn1080    时间: 2012-8-16 10:11 AM
http://zhidao.baidu.com/question/464583113.html?oldq=1
  1. select t.*,p.ImageData  from (
  2. select t.upduser, COUNT(1) as totalView  from efView t
  3. group by t.updUser ) t
  4. left join empPhoto p on t.upduser = p.emp_id
复制代码
解决了^^




欢迎光临 JBTALKS.CC (https://jbtalks.my/) Powered by Discuz! X2.5