日期:2014-05-18  浏览次数:20556 次

问个小问题?请大家帮忙
SET   @SQL   = 'SELECT   a.sup_id,
                      数量=sum(sup_id)
from   chitty     a   left   join   item     b   on   a.chitty_id=b.chitty_id  
order   by       a.sup_id '
PRINT(   @Sql)  
EXECUTE     (@SQL)
为什么提示:
列   'a.sup_id '   在选择列表中无效,因为该列未包含在聚合函数中,并且没有   GROUP   BY   子句


------解决方案--------------------
SET @SQL = 'SELECT a.sup_id,
数量=sum(sup_id)
from chitty a left join item b on a.chitty_id=b.chitty_id
group by a.sup_id
order by a.sup_id '
PRINT( @Sql)
EXECUTE (@SQL)

------解决方案--------------------
去掉,当然是正确的了,只是普通的查询而已,但是你如果有sum的话,那就不一样的,你不用去掉
数量=sum(sup_id),而是去掉a.sup_id,那也是正确的,如果有单独的列,又有聚合函数的话,那就要跟group by 才行
------解决方案--------------------
SET @SQL = 'SELECT a.sup_id,
数量=sum(sup_id)
from chitty a left join item b on a.chitty_id=b.chitty_id
GROUP BY a.sup_id '
order by a.sup_id '
PRINT( @Sql)
EXECUTE (@SQL)

------解决方案--------------------
SET @SQL = 'SELECT a.sup_id,
数量=sum(sup_id)
from chitty a left join item b on a.chitty_id=b.chitty_id
group by a.sup_id
order by a.sup_id '
PRINT( @Sql)
EXECUTE (@SQL)