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

因为该列没有包含在聚合函数或 GROUP BY 子句中,强加group by数据不对

select 
max(isnull(t.AC0534,'')) as [图片显示],
a.AD0506 as [产品品牌],
a.AC0011 as [产品编号],
max(isnull(a.AC0012,'')) as [厂家货号],
max(isnull(a.EC0104,'')) as [产品名称],
max(cast(isnull(a.AM0006,0) as decimal(12,2))) as [市场价],
max(cast(isnull(a.AM0007,0) as decimal(12,2))) as [零售价],
max(cast(isnull(t.AM0010,0) as decimal(12,2))) as [销售价],
max(isnull(a.AC0013,'')) as [产品简介]

from agt_trad..WFPUSER_A0301 a 
join agt_trad..WFPUSER_A0304 t on a.AC0011=t.AC0011 and a.AD0506=t.AD0506
join agt_trad..WFPCODE_AD502 c on a.AD0506=c.objname and a.objid <>0 and a.AB0001=0 and isnull(a.AD0520,'0')='0'
join agt_trad..WFPUSER_A0301 b on a.parentid=b.objid
left join agt_trad..WFPUSER_A0301 b2 on b.parentid=b2.objid where 
  cast(isnull(t.AM0010,0) as decimal(12,2)) between 1 and 10 group by a.AD0506,a.AC0011
order by t.AM0010

这样写报错, ORDER BY 子句中的列 "agt_trad..WFPUSER_A0304.AM0010" 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。 
如果group by a.AD0506,a.AC0011.t.AM0010 这样强加的话数据不准, 
怎么解决


------解决方案--------------------
SQL code
select  
max(isnull(t.AC0534,'')) as [图片显示],
a.AD0506 as [产品品牌],
a.AC0011 as [产品编号],
max(isnull(a.AC0012,'')) as [厂家货号],
max(isnull(a.EC0104,'')) as [产品名称],
max(cast(isnull(a.AM0006,0) as decimal(12,2))) as [市场价],
max(cast(isnull(a.AM0007,0) as decimal(12,2))) as [零售价],
max(cast(isnull(t.AM0010,0) as decimal(12,2))) as [销售价],
max(isnull(a.AC0013,'')) as [产品简介]

from agt_trad..WFPUSER_A0301 a  
join agt_trad..WFPUSER_A0304 t on a.AC0011=t.AC0011 and a.AD0506=t.AD0506
join agt_trad..WFPCODE_AD502 c on a.AD0506=c.objname and a.objid <>0 and a.AB0001=0 and isnull(a.AD0520,'0')='0'
join agt_trad..WFPUSER_A0301 b on a.parentid=b.objid
left join agt_trad..WFPUSER_A0301 b2 on b.parentid=b2.objid where  
  cast(isnull(t.AM0010,0) as decimal(12,2)) between 1 and 10 
group by a.AD0506,a.AC0011

------解决方案--------------------
SQL code
select  
max(isnull(t.AC0534,'')) as [图片显示],
max(a.AD0506) as [产品品牌], ---要么这里聚合
max(a.AC0011) as [产品编号],
max(isnull(a.AC0012,'')) as [厂家货号],
max(isnull(a.EC0104,'')) as [产品名称],
max(cast(isnull(a.AM0006,0) as decimal(12,2))) as [市场价],
max(cast(isnull(a.AM0007,0) as decimal(12,2))) as [零售价],
max(cast(isnull(t.AM0010,0) as decimal(12,2))) as [销售价],
max(isnull(a.AC0013,'')) as [产品简介]

from agt_trad..WFPUSER_A0301 a  
join agt_trad..WFPUSER_A0304 t on a.AC0011=t.AC0011 and a.AD0506=t.AD0506
join agt_trad..WFPCODE_AD502 c on a.AD0506=c.objname and a.objid <>0 and a.AB0001=0 and isnull(a.AD0520,'0')='0'
join agt_trad..WFPUSER_A0301 b on a.parentid=b.objid
left join agt_trad..WFPUSER_A0301 b2 on b.parentid=b2.objid where  
  cast(isnull(t.AM0010,0) as decimal(12,2)) between 1 and 10 group by a.AD0506,a.AC0011
order by t.AM0010

------解决方案--------------------
SQL code
select  
max(isnull(t.AC0534,'')) as [图片显示],
a.AD0506 as [产品品牌],
a.AC0011 as [产品编号],
max(isnull(a.AC0012,'')) as [厂家货号],
max(isnull(a.EC0104,'')) as [产品名称],
max(cast(isnull(a.AM0006,0) as decimal(12,2))) as [市场价],
max(cast(isnull(a.AM0007,0) as decimal(12,2))) as [零售价],
max(cast(isnull(t.AM0010,0) as decimal(12,2))) as [销售价],
max(isnull(a.AC0013,'')) as [产品简介]

from agt_trad..WFPUSER_A0301 a  
join agt_trad..WFPUSER_A0304 t on a.AC0011=t.AC0011 and a.AD0506=t.AD0506
join agt_trad..WFPCODE_AD502 c on a.AD0506=c.objname and a.objid <>0 and a.AB0001=0 and isnull(a.AD0520,'0')='0'
join agt_trad..WFPUSER_A0301 b on a.parentid=b.objid
left join agt_trad..WFPUSER_A0301 b2 on b.parentid=b2.objid 
where cast(isnull(t.AM0010,0) as decimal(12,2)) between 1 and 10 
group by a.AD0506,a.AC0011
order by cast(isnull(t.AM0010,0) as decimal(12,2))

------解决方案--------