Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calculating mathematical difference between two rows

Rohit MittelDec 20 2023 — edited Dec 20 2023

Hi,

I have the following two queries to get data and they return only one row of data. The data returned is numerical and total of seven columns are present. I want to calculate the mathematical difference between the two rows of data.

Query 1

select * from(select DISTINCT
trim(to_char(DATE,'DAY')) as DAY,
(AMOUNT1 + AMOUNT2) as NON_CASH
FROM BANK_TABLE
where site_code = '12'
and submit_flag = 'N')
pivot(
 SUM(NON_CASH)
 for DAY in ('MONDAY' day1,'TUESDAY' day2,'WEDNESDAY' day3,'THURSDAY' day4,'FRIDAY' day5,'SATURDAY' day6, 'SUNDAY' day7)
)

Query 2

select * from(
select
trim(to_char(date,'DAY')) as DAY,
TOTAL_TAKINGS
FROM CASH_TABLE
where site_code = '12'
and submit_flag = 'N')
pivot(
 SUM(total_takings)
 for DAY in ('MONDAY' day1,'TUESDAY' day2,'WEDNESDAY' day3,'THURSDAY' day4,'FRIDAY' day5,'SATURDAY' day6, 'SUNDAY' day7)
)

I tried using the following query for each column. As the query becomes too long and complex to get data for each column I would like to know if there is a better find the mathematical difference for the data.

select (select day1 - (select day1 from (select * from(select DISTINCT
trim(to_char(BANKING_DATE,'DAY')) as BANKING_DAY,
(CHEQUES_AMOUNT + CREDIT_CARD_SLIPS_AMOUNT) as NON_CASH
FROM BANK_TABLE
where site_code = :APP_USER
and submit_flag = 'N')
pivot(
 SUM(NON_CASH)
 for BANKING_DAY in ('MONDAY' day1,'TUESDAY' day2,'WEDNESDAY' day3,'THURSDAY' day4,'FRIDAY' day5,'SATURDAY' day6, 'SUNDAY' day7)
) )
)
from
(select * from(
select
trim(to_char(report_date,'DAY')) as REPORT_DAY,
TOTAL_REGISTER_TAKINGS
FROM CASH_TABLE
where site_code = :APP_USER
and submit_flag = 'N')
pivot(
 SUM(total_register_takings)
 for REPORT_DAY in ('MONDAY' day1,'TUESDAY' day2,'WEDNESDAY' day3,'THURSDAY' day4,'FRIDAY' day5,'SATURDAY' day6, 'SUNDAY' day7)
))) day1
from dual

Thanks

Rohit

This post has been answered by Stax on Dec 20 2023
Jump to Answer
Comments
Post Details
Added on Dec 20 2023
5 comments
521 views