SQL语句中使用sum等统计函数时,如果有非统计函数的字段,那么这些字段必须放入group by子句中,即需要按group by中的字段进行分组统计。故从语法上来说添加group by即可:
select a1, a2, a3, sum(b1), sum(b2)
from return_order a, return_detail b
where aid = breturnid
group by a1, a2, a3
select sum(pmpodUnitPriceNoTax) as tax_sum from dbopurchaseOrderDetails
输出 tax_sum就可以了
第一个问题:分组了,就不用再用DISTINCT了
第二个问题:WHERE后跟分组前条件,HAVING是分组后条件
第三个问题:你也用总金额为分组,不知道你有什么特定目的
select acode as 订单编号,aamount as 订单总金额,
sum(bamount + bamount_lans) as 明细总金额
from a
left join b on aorder_id = border_id
group by acode,aamount
having atotal_amount - sum(bamount + bamount_lans) <> 0
估计这个效果才是你要的
select acode as 订单编号,sum(aamount) as 订单总金额,
sum(bamount + bamount_lans) as 明细总金额
from a
left join b on aorder_id = border_id
group by acode
having atotal_amount - sum(bamount + bamount_lans) <> 0
SELECT A,Buid,Bmoney as heji FROM A
LEFT JOIN (SELECT uid,sum(money) FROM A) b ON Auid=Buid
解析
SELECT uid,sum(money) FROM Asum()为集合函数 查询多个列时需要使用GROUP BY
正确语法
SELECT uid,sum(money) FROM A GROUP BY uid第二条SQL语句
SELECT go_member,go_member_accountuid,go_member_accountmoney as hejiFROM `go_member` LEFT JOIN
(SELECT uid,sum(money) FROM go_member_account ) b
ON go_memberuid=go_member_accountuid
解析
ON go_memberuid=go_member_accountuid条件错误
LEFT JOIN(SELECT uid,sum(money) FROM go_member_account ) b
子查询 相当于把表go_member_account
复制给B了 这个表go_member_account不存在
ON go_memberuid=buid第一种解决方案:textBox1Text=SDR[0]ToString();
第二种解决方案:
connOpen();
stringsql1="SELECTSUM(AAA)asAAAsumFROMOWHERECCC='"+123TextTrim()+"'";
SqlCommandcomm1=newSqlCommand(sql1,conn);
SqlDataReaderSDR=comm1ExecuteReader();
while(SDRRead())
{
textBox1Text=SDR["AAAsum"]ToString();//这里不会写
}
SDRClose();
还可以SDRGet什么的忘了,不过VS可以点出来,是一个方法,要的是下标,你给就行,建议你用第二种
这个样子?sqlserver?
update a set ahour=chour from a inner join (select id,sum(isnull(hour,0)) hour from b group by id) c on aid=cidsum是聚合函数,有sum,就不能有前面的,如果除了sum外,还要显示什么,就必须列举出来,然后where后面还要加group。
列一:select sum(ab) as a from stmx where
列二:select colum1,colum2,sum(ab) from stmx where group by colum1,colum2
sql中的sum配合casewhen就可以添加条件\x0d\例:\x0d\sum(casewhena>0thenaelse0end)\x0d\将a列所以大于0的值相加。\x0d\sum(casewhenrq>'2015-1-1'andrq
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)