[英]oracle sql Monthly financial report : Provide a report that shows total income of the vehicle owners for every single month
Monthly financial report :月度财务报告:
Provide a report that shows total income of the vehicle owners for every single month (limit it to one year – 12 months - and print only if there is activity in a given month)提供一份报告,显示车主每个月的总收入(限制为一年 - 12 个月 - 并且仅在给定月份有活动时才打印)
find the trips table details below在下面找到行程表的详细信息
TRIP_ID C_id V_OWNERID DISTANCE_ID TRIP_DATE soruce town luggage NO_OF_PASSENGER amount
---------------------------------------------------------------------------------
1001 1009 117 29 9 11-FEB-20 11.31.54.496000000 AM Baltimore Charlotte 1 2 TRUE 1510
1002 1010 114 23 4 10-JAN-19 05.31.54.130000000 PM Philadelp Pittsburgh 2 1 FALSE 1530
1003 1002 116 31 8 27-OCT-19 01.39.47.130000000 AM Dallas Los Angel 2 3 TRUE 9270
1004 1005 118 25 10 27-OCT-19 01.01.04.130000000 AM Richmond Virginia 1 4 FALSE 905
1005 1006 113 24 2 28-OCT-19 01.01.04.130000000 AM Westchester Gettysburg 0 5 TRUE 1340
1006 1009 115 29 7 15-MAY-19 01.01.04.130000000 AM Baltimore Charlotte 1 1 FALSE 475
1021 1009 117 29 9 12-FEB-20 11.31.54.496000000 AM Baltimore Charlotte 1 2 TRUE 1510
1022 1009 117 29 9 13-FEB-20 11.31.54.496000000 AM Baltimore Charlotte 1 2 TRUE 1510
1023 1009 117 29 9 14-FEB-20 11.31.54.496000000 AM Baltimore Charlotte 1 2 TRUE 1510
1024 1009 117 19 9 22-FEB-20 11.31.54.496000000 AM Baltimore Frederick 1 2 TRUE 190
1025 1009 117 19 9 23-FEB-20 11.31.54.496000000 AM Baltimore Frederick 1 2 TRUE 190
1042 1009 117 61 9 19-JAN-20 11.31.54.496000000 AM Baltimore Delaware 1 2 TRUE 0
1043 1009 117 61 9 11-JAN-20 11.31.54.496000000 AM Baltimore Delaware 1 2 TRUE 0
1044 1009 117 61 9 16-JAN-20 11.31.54.496000000 AM Baltimore Delaware 1 2 TRUE 0
1045 1009 117 61 9 26-JAN-20 11.31.54.496000000 AM Baltimore Delaware 1 2 TRUE 0
1046 1009 117 62 9 15-AUG-20 11.31.54.496000000 AM Baltimore Michigan 1 2 TRUE 0
1047 1009 117 61 9 31-AUG-20 11.31.54.496000000 AM Baltimore Delaware 1 2 TRUE 0
desc trips;
TRIP_ID NOT NULL NUMBER
CUSTOMER_ID NUMBER
V_OWNERID NUMBER
DISTANCE_ID NUMBER
VEHICLE_ID NUMBER
TRIP_DATE TIMESTAMP(6)
SOURCE_TOWN VARCHAR2(20)
DESTINATION_TOWN VARCHAR2(20)
LUGGAGE NUMBER
NO_OF_PASSENGER NUMBER
HOLIDAY VARCHAR2(5)
TOTAL_PAYMENT FLOAT(126)
vehicle owner table车主表
VEHICLE_OWNERID OWNER_NAME
-------------------------------------------------
110 JOHN
111 EMMA
112 SURESH
113 JOSHUA
114 Prakash
115 Prasad
116 Jayawardena
117 Suchitra
desc VEHICLE_OWNERID ;
VEHICLE_OWNERID NOT NULL NUMBER
OWNER_NAME VARCHAR2(20)
vehicles tables车辆表
VEHICLE_ID VEHICLE_OWNERID MAKE
------------------------------------------------------
1 110 Honda
2 111 Toyota
3 112 Tesla
4 113 Ford
5 114 BMW
6 115 AUDI
7 116 Fiat
VEHICLE_ID NOT NULL NUMBER
VEHICLE_OWNERID NUMBER
MAKE VARCHAR2(15)
I think you need the sum of amount per owner per month for the last 12 months including the current month.我认为您需要过去 12 个月(包括当月)每个所有者每月的金额总和。
You can use an aggregate function as follows:您可以按如下方式使用聚合函数:
SELECT
V.OWNER_NAME,
TRUNC(T.TRIP_DATE, 'MONTH'),
SUM(AMOUNT) AS TOTAL_BY_MONTH
FROM
TRIPS T
JOIN VEHICLE_OWNER V ON T.V_OWNERID = V.VEHICLE_OWNERID
WHERE
T.TRIP_DATE >= ADD_MONTHS(ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), 1), - 12)
GROUP BY
V.OWNER_NAME,
TRUNC(T.TRIP_DATE, 'MONTH')
ORDER BY 1,2
Cheers!!干杯!!
(select 'JAN'as month, t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-1-1'
and trip_date <= date '2020-1-31'
group by t.vehicle_ownerid)
union all
(select 'FEB'as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-2-1'
and trip_date <= date '2020-2-29'
group by t.vehicle_ownerid)
union all
(select 'MAR' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-3-1'
and trip_date <= date '2020-3-31'
group by t.vehicle_ownerid)
union all
(select 'APR' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-4-1'
and trip_date <= date '2020-4-30'
group by t.vehicle_ownerid)
union all
(select 'MAY' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-5-1'
and trip_date <= date '2020-5-31'
group by t.vehicle_ownerid)
union all
(select 'JUN' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-6-1'
and trip_date <= date '2020-6-30'
group by t.vehicle_ownerid)
union all
(select 'JUL' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-7-1'
and trip_date <= date '2020-7-31'
group by t.vehicle_ownerid)
union all
(select 'AUG' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-8-1'
and trip_date <= date '2020-8-31'
group by t.vehicle_ownerid)
union all
(select 'SEP' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-9-1'
and trip_date <= date '2020-9-30'
group by t.vehicle_ownerid)
union all
(select 'OCT' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-10-1'
and trip_date <= date '2020-10-31'
group by t.vehicle_ownerid)
union all
(select 'NOV' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-11-1'
and trip_date <= date '2020-11-30'
group by t.vehicle_ownerid)
union all
(select 'DEC' as month,t.vehicle_ownerid,(sum(t.total_payment))as total_income from trips t,vehicles v ,vehicle_owner vo
where
v.VEHICLE_ID =t.VEHICLE_ID`enter code here`
and vo.vehicle_ownerid =v.vehicle_ownerid and trip_date >= date '2020-12-1'
and trip_date <= date '2020-12-30'
group by t.vehicle_ownerid);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.