[英]Oracle DB - Single query non-linear time grouping report (1 day, 1 week, 1 month, 3 months, total)
[英]oracle sql Monthly financial report : Provide a report that shows total income of the vehicle owners for every single month
月度財務報告:
提供一份報告,顯示車主每個月的總收入(限制為一年 - 12 個月 - 並且僅在給定月份有活動時才打印)
在下面找到行程表的詳細信息
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_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)
車輛表
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)
我認為您需要過去 12 個月(包括當月)每個所有者每月的金額總和。
您可以按如下方式使用聚合函數:
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
干杯!!
(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.