1

質問があります。

select ProductID,  name, (unitPrice*quantity) As 'Stock Equity'
from tproduct
group by productID with rollup
;

最後の結果を繰り返す代わりに、最後の合計の計算である行のセットを返すことを望みますか?

なぜ、これを克服する方法を教えてもらえますか?

これは現時点でのクエリ結果です。

ProductID    Name           Stock Equity
1            cannon 600 D   3360
2            cannon 550 D   1000
3            cannon 500 D   750
4            cannon 5D      5000
5            cannon 650 D   9000
6            Nikon D5100    1000
7            Nikon D3200    420
8            Nikon D7000    2700
9            Nikon D800     6030
10           Nikon D90      4770
null         Nikon D90      4770
4

2 に答える 2

1

使用できますUNION ALL

select ProductID,  name, (unitPrice*quantity) As 'Stock Equity'
from tproduct
union all
select 'total', 'total', sum(unitPrice*quantity)
from tproduct

デモで SQL Fiddle を参照してください

または、次のようなものを使用できます。

select case when ProductID is null then 'total' else ProductId end Productid,  
  case when ProductID is null then 'total' else name end name, 
  sum(unitPrice*quantity) As 'Stock Equity'
from tproduct
group by ProductID with rollup

デモで SQL Fiddle を参照してください

于 2012-11-26T21:55:20.937 に答える
0
select ProductID,  
       name, 
       (unitPrice*quantity) As 'Stock Equity',
       (select sum(unitPrice*quantity) from tproduct) As total
from tproduct
group by productID
于 2012-11-26T21:53:00.643 に答える