简单的sql语句问题!拜托了
表a
dj(单价) sl(数量) lb(类别) bh(编号)
2 3 a 001
5 2 a 001
1 5 b 001
3 9 c 001
要得到:
bh 001的
lb(类别) 总金哦
a 16
b 5
c 27
怎么写啊
------解决方案--------------------select lb , sum(dj*sl) 总金哦 from tb group by lb
------解决方案--------------------create table tb (dj int , sl int , lb varchar(10), bh varchar(10))
insert into tb values(2,3, 'a ', '001 ')
insert into tb values(5,2, 'a ', '001 ')
insert into tb values(1,5, 'b ', '001 ')
insert into tb values(3,9, 'c ', '001 ')
select lb , sum(dj*sl) 总金额 from tb group by lb
drop table tb
/*
lb 总金额
---------- -----------
a 16
b 5
c 27
(所影响的行数为 3 行)
*/
------解决方案--------------------create table tb (dj int , sl int , lb varchar(10), bh varchar(10))
insert into tb values(2,3, 'a ', '001 ')
insert into tb values(5,2, 'a ', '001 ')
insert into tb values(1,5, 'b ', '001 ')
insert into tb values(3,9, 'c ', '001 ')
select lb , sum(dj*sl) 总金额 from tb where bh = '001 ' group by lb
drop table tb
/*
lb 总金额
---------- -----------
a 16
b 5
c 27
(所影响的行数为 3 行)
*/
------解决方案--------------------select lb,sum(dj) as dj(这个你想把总金额叫啥名就是啥,我这里就用dj) group by lb where bh= '001 '
------解决方案--------------------select lb,sum(dj) as dj(这个你想把总金额叫啥名就是啥,我这里就用dj) from a group by lb where bh= '001 '
------解决方案--------------------select lb, sum(isnull(dj,0)*isnull(sl,0)) as 总金额 from a