anumoses
asked on
Oracle query
https://www.experts-exchange.com/questions/28498750/oracle-query-from-union.html
This is already answered. Need additional inputs
I am creating sample table with some data as it shows in query with blood_drives and blood_drives_audit table.
The solved solution includes situation if a drive is cancelled, then it subtracts the projection. Now if the column_name in the audit_table is projection
then it has to take a sum subtracting old_value from new_value and then give me the sum
I have data for rep 6 and 4 in the table. Both have same situations.
The expected result for rep 6
6 SEP 25 New Drive/Projection Added
6 OCT 19 Projection
6 NOV 80 New Drive/Projection Added
This is already answered. Need additional inputs
I am creating sample table with some data as it shows in query with blood_drives and blood_drives_audit table.
The solved solution includes situation if a drive is cancelled, then it subtracts the projection. Now if the column_name in the audit_table is projection
then it has to take a sum subtracting old_value from new_value and then give me the sum
I have data for rep 6 and 4 in the table. Both have same situations.
The expected result for rep 6
6 SEP 25 New Drive/Projection Added
6 OCT 19 Projection
6 NOV 80 New Drive/Projection Added
SELECT audit_key
, area_rep_no
, to_char(drive_date, 'MON') AS Month
, DRIVE_DATE
, SUM(projection) AS projection
, insert_date
, 'New Drive/Projection Added' AS reason
, null as old_value
, null as new_value
FROM tab1
WHERE insert_date BETWEEN TRUNC(SYSDATE, 'Day')-6 and TRUNC(SYSDATE, 'Day')
AND drive_cancelled IS NULL
AND drive_date BETWEEN trunc(sysdate) - (to_number(to_char(sysdate,'DD')) - 1) and
ADD_MONTHS(trunc(sysdate,'YEAR'),12)-1
GROUP BY
area_rep_NO
, drive_date
, insert_date
, audit_key
UNION ALL
SELECT a.audit_key
, b.area_rep_no
, to_char(b.drive_date, 'MON') AS Month
, DRIVE_DATE
, SUM(
CASE
WHEN column_name = 'drive_cancelled' THEN b.projection * -1
ELSE b.projection END) AS projection
, audit_insert_date AS insert_date
, column_name
, old_value
, new_value
FROM tab2 a
INNER JOIN tab1 b
ON a.audit_key = b.audit_key
WHERE column_name IN ('drive_id', 'drive_cancelled', 'projection')
AND audit_insert_date BETWEEN TRUNC(SYSDATE, 'Day')-6 and TRUNC(SYSDATE, 'Day')
GROUP BY a.audit_key
, area_rep_no
, DRIVE_DATE
, to_char(drive_date, 'MON')
, audit_insert_date
, column_name
, old_value
, new_value
ORDER BY
area_rep_no, drive_date
table-script-with-sample-data.txt
ASKER
This is the existing date range:
WHERE column_name IN ('drive_id', 'drive_cancelled', 'projection')
AND audit_insert_date BETWEEN TRUNC(SYSDATE, 'Day')-6 and TRUNC(SYSDATE, 'Day')
Do I need to deduct another 7 days from that calculation for the "old_value"?
WHERE column_name IN ('drive_id', 'drive_cancelled', 'projection')
AND audit_insert_date BETWEEN TRUNC(SYSDATE, 'Day')-6 and TRUNC(SYSDATE, 'Day')
Do I need to deduct another 7 days from that calculation for the "old_value"?
ASKER
No
From this
What else can you tell me that would help here?
I won't be online now until (my) tomorrow.
select
area_rep_no
, Month
, reason
, sum( projection) as projection
, sum(old_value) as old_value
from (
SELECT
b.area_rep_no
, to_char(b.drive_date, 'YYYY-MM') AS Month
, CASE
WHEN column_name = 'drive_cancelled' THEN b.projection * -1
ELSE b.projection END AS projection
, column_name as reason
, null as old_value
FROM tab2 a
INNER JOIN tab1 b
ON a.audit_key = b.audit_key
WHERE column_name IN ('drive_id', 'drive_cancelled', 'projection')
AND audit_insert_date BETWEEN TRUNC(SYSDATE, 'Day')-6 and TRUNC(SYSDATE, 'Day')
UNION ALL
SELECT
b.area_rep_no
, to_char(b.drive_date, 'YYYY-MM') AS Month
, NULL as projection
, column_name as reason
, CASE
WHEN column_name = 'drive_cancelled' THEN b.projection * -1
ELSE b.projection END as old_value
FROM tab2 a
INNER JOIN tab1 b
ON a.audit_key = b.audit_key
WHERE column_name IN ('drive_id', 'drive_cancelled', 'projection')
AND audit_insert_date BETWEEN TRUNC(SYSDATE, 'Day')-13 and TRUNC(SYSDATE, 'Day')-6
)
GROUP BY
area_rep_no
, Month
, reason
ORDER BY
area_rep_no
, Month
, reason
;
I get this:
| AREA_REP_NO | MONTH | REASON | PROJECTION | OLD_VALUE |
|-------------|---------|-----------------|------------|-----------|
| 4 | 2014-08 | drive_cancelled | -19 | (null) |
| 4 | 2014-09 | drive_id | 30 | (null) |
| 4 | 2014-10 | drive_cancelled | -20 | (null) |
| 4 | 2014-10 | drive_id | 31 | (null) |
| 4 | 2014-11 | drive_id | 50 | (null) |
| 6 | 2014-10 | drive_id | (null) | 25 |
| 6 | 2014-10 | projection | 70 | (null) |
Does this make sense to you, is it "the right direction"?What else can you tell me that would help here?
I won't be online now until (my) tomorrow.
>>"no"
not very helpful I'm afraid
If deducting 7 days isn't right, please tell me what is right.
I am going to bed now (its very late for me).
Please understand I have to ask questions, and if unanswered I cannot get you a solution.
not very helpful I'm afraid
If deducting 7 days isn't right, please tell me what is right.
I am going to bed now (its very late for me).
Please understand I have to ask questions, and if unanswered I cannot get you a solution.
ASKER
No this is not the right direction.
more details please, I don't know where "old_value" comes from
I hope to see details when I get back to this tomorrow
I hope to see details when I get back to this tomorrow
ASKER
I was thinking something on these lines. Tell me if I am wrong
SUM(
CASE
WHEN column_name = 'drive_cancelled' THEN b.projection * -1
WHEN column_name = 'projection' THEN new_value-old_value
ELSE null END) AS difference
SUM(
CASE
WHEN column_name = 'drive_cancelled' THEN b.projection * -1
WHEN column_name = 'projection' THEN new_value-old_value
ELSE null END) AS difference
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
I have you DDL and the inserts working - than you that is very helpful.
But for the expected result:
6 SEP 25 New Drive/Projection Added
6 OCT 19 Projection
6 NOV 80 New Drive/Projection Added
You need to explain WHY that expected result is "correct". For example you only display 4 columns. Do you only want those 4 columns?
If I revert the query to the same at the last question, there were 7 columns, and from your sample I get:
Open in new window
If I reduce the columns to 4, this is the result:Open in new window
HOW can you reach 19 for projection in OCT? What are you doing manually to reach that number?{+ EDIT} details behind the calculation of 70 = 25+25+20 as you see here
Open in new window