所以说我在Oracle SQL中有两个表(实际上不是数据但是为了方便起见我应该强调我的问题)
Table1仅包含Order_id和Order_quantity,Table2仅包含Order_id和Order_price
然后我加入他们如下
SUM(variable_name) From Table_Name
基本上我想要两个额外的列,一个作为两个表中列的乘积,另一个作为我的联合表中该列的总和(因此每个条目都是相同的)。但是你需要
Select T1.Order_id,
T1.Order_quantity,
T2.Order_price,
T1.Order_quantity*T2.Order_price As "Order_amount",
Sum(Order_amount) from New_Table As "Total_Sales"
from (Table1 T1
inner join Table2 T2
on T1.Order_id = T2.Order_id) As New_Table
我可以为新表分配变量名称,然后参考。我尝试了以下但我得到的SQL命令没有正确结束错误
setContentView(R.layout.activity_main);
Mapbox.getInstance(this, "access_token");
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
mapboxMap.setStyleJson("http://192.168.1.65:8080/styles/dark-matter.json");
mapboxMap.setCameraPosition(new CameraPosition.Builder()
.target(new LatLng(47.3769, 8.5417))
.zoom(25)
.build());
}
});
感谢您的帮助,道歉,因为我目前对SQL非常天真,
答案 0 :(得分:2)
我想你只想要一个窗口功能:
select T1.Order_id, T1.Order_quantity, T2.Order_price,
T1.Order_quantity*T2.Order_price As order_amount,
sum(T1.Order_quantity*T2.Order_price) over () As Total_Sales
from Table1 T1 inner join
Table2 T2
on T1.Order_id = T2.Order_id
您无法在order_amount
中重复使用别名select
。您需要重复表达式 - 或使用子查询或CTE来定义它。
答案 1 :(得分:0)
如果您的 DBMS 没有窗口功能支持,那么您可以使用subquery
代替
select order_id, Order_quantity,
(select t1.Order_quantity * t2.Order_price
from table2 t2
where t2.Order_id = t1.Order_id) as Order_amount,
(select sum(t1.Order_quantity * t2.Order_price)
from table2 t2
where t2.Order_id = t1.Order_id) as Total_Sales
from table1 t1;