问个sum group by 的查询语句
表A
Aid , a , b , c , d
1 , a1, b1, c1, d1
2 , a2, b2, c2, d2
3 , a3, b3, c3, d3
表B
Bid , Aid , e, f
1 , 1 ,10, f1
2 , 1 , 3, f2
3 , 2 ,-2, f3
4 , 2 , 6, f4
5 , 2 , 7, f5
SQL查询后结果是
Aid a b c d sum(e) f
A表所有数据都出现,b表计算e字段合计数
[最优解释]
的确是懒的写所以才告诉你的,你把代码写出来我改把,text那些,这样搞:
select aid,convert(varchar(max),a),convert(varchar(max),b),convert(varchar(max),c),sum(xxx)
from tb
group by aid,convert(varchar(max),a),convert(varchar(max),b),convert(varchar(max),c)
如果你是2000,那么varcahr(max)改成varchar(8000)
[其他解释]
select Aid, a, b, c, d, sum(e), f
from A left Join B on a.Aid = b.Aid
Group by Aid, a, b, c, d, f
为啥还要显示f呢
[其他解释]
group by Aid,cast(a as varchar(max)),。。。。 转换思路了
[其他解释]
两表关联然后按照你的想法来group by咯
[其他解释]
dbo.DepartmentWorkTargetScore ON dbo.DepartmentWorkTarget.DepartmentWorkTargetID = dbo.DepartmentWorkTargetScore.DepartmentWorkTargetID
WHERE (dbo.DepartmentWorkTarget.UnitsID = 1) AND (dbo.DepartmentWorkTarget.YearMarkID = 1)
GROUP BY dbo.DepartmentWorkTarget.DepartmentWorkTargetID, dbo.AssessContent.AssessContentName, CONVERT(varchar(MAX), dbo.DepartmentWorkTarget.IndexContent),
dbo.DepartmentWorkTarget.IndexCategory, dbo.DepartmentWorkTarget.IndexValue, dbo.DepartmentWorkTargetScore.DepartmentWorkTargetScore
可以查询了,但查询后还是分列显示,没有统计合。
[其他解释]
可以了 把最后那个sum的group by去掉就行了。