表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咯
------其他解决方案--------------------
group by Aid 报错啊
------其他解决方案--------------------
group by Aid,a,b,c,d
------其他解决方案--------------------
你干嘛group by aid呢?你要显式多少,就group by多少,非sum/max/min/avg/count那些都groupby
------其他解决方案--------------------
a,b,c,d中有b是text字段 不能group
------其他解决方案--------------------
你就费了写个SQL语句出来嘛
------其他解决方案--------------------
SELECT dbo.DepartmentWorkTarget.DepartmentWorkTargetID, dbo.AssessContent.AssessContentName, CONVERT(varchar(MAX), dbo.DepartmentWorkTarget.IndexContent)
AS Expr2, dbo.DepartmentWorkTarget.IndexCategory, dbo.DepartmentWorkTarget.IndexValue, SUM(dbo.DepartmentWorkTargetScore.DepartmentWorkTargetScore)
AS Expr1
FROM dbo.DepartmentWorkTarget INNER JOIN
dbo.AssessContent ON dbo.DepartmentWorkTarget.AssessContentID = dbo.AssessContent.AssessContentID LEFT OUTER JOIN