首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

问个小疑点?请大家帮忙

2012-01-20 
问个小问题?请大家帮忙SET@SQL SELECTa.sup_id,数量sum(sup_id)fromchittyaleftjoinitembona.chitty_id

问个小问题?请大家帮忙
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)

热点排行