oracle sql关于事务数据聚合的问题

时间:2019-05-09 20:28:18

标签: sql oracle

我正在尝试在te数据(dollar_value_us,QUANTITY)中添加所有内容,但不添加运输总额,因为每个交易编号都有多个项目,但是客户只支付了一次运输费用。我正在使用以下数据:

https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=01693db7ce05b062804cedeb3b3a7e73

以下是我在实际数据库中使用的查询:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/main_background"
   tools:context=".SlideActivity"
   android:layout_centerInParent="true">

   <android.support.v4.view.ViewPager
       android:id="@+id/slideViewPager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_centerInParent="true"></android.support.v4.view.ViewPager>

   <LinearLayout
       android:id="@+id/dotsLayout"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/slideViewPager"
       android:layout_alignParentStart="true"
       android:layout_centerInParent="true"
       android:orientation="horizontal"
       android:padding="20dp"
       android:layout_gravity="center" />
</RelativeLayout>

美国的最终输出应具有35的总运输量

1 个答案:

答案 0 :(得分:1)

尝试一下:

select a.quarter_date, a.country, a.total_shipping, b.total_dollar_value, b.total_quantity
from 
 (select quarter_date, country, sum(shipping_total) as total_shipping
from 
    (select distinct quarter_date, country, shipping_total
    from transaction_detail_mv)c
  group by quarter_date,country
)a
join -- Below is part of your query
(select QUARTER_DATE ,COUNTRY,sum(DOLLAR_VALUE_US) as total_dollar_value, sum(QUANTITY) as total_quantity
from transaction_detail_mv
group by QUARTER_DATE,COUNTRY)b
on a.quarter_date = b.quarter_date
and a.country = b.country

测试结果:

DB<>Fiddle