Update or Select into ORACLE - sql
I am using the following statement;
SELECT RESV_ID, BOOKING_CUS_ID, ACC_ID,
(SELECT F.FLI_PRICE FROM FLIGHT F WHERE F.FLI_ID = R.IN_FLIGHT_ID) AS DEPART_FLIGHT_PRICE,
(SELECT F1.FLI_PRICE FROM FLIGHT F1 WHERE F1.FLI_ID = R.OUT_FLIGHT_ID) AS RETURN_FLIGHT_PRICE,
(SELECT AC.ACC_PRICEPN FROM ACCOMMODATION AC WHERE AC.ACC_ID = R.ACC_ID) AS ACCOMMODATION_PRICE
FROM HOLIDAY_RESERVATION R;
to yield the following results;
RESV_ID BOOKING_CUS_ID ACC_ID DEPART_FLIGHT_PRICE RETURN_FLIGHT_PRICE ACCOMMODATION_PRICE
---------- -------------- ---------- ------------------- ------------------- -------------------
1 1 2 520 450 350
2 3 4 250 150 150
3 5 6 290 300 450
4 7 7 399 450 650
5 9 365 345
6 11 558 460
7 13 250 250
8 15 550 550
9 17 25 250
10 19 19 450
10 rows selected.
Question:
How do I sum up the price fields, SOME PRICES ARE NOT AVAILABLE because a reservation was either made for accommodation only or flight only, hence both values will not be present always and this is where the issue lies
DEPART_FLIGHT_PRICE RETURN_FLIGHT_PRICE ACCOMMODATION_PRICE
Furthermore:
I wish to insert or update the SUM of those three values into a SUBTOTAL in the reservation table, perhaps by using select into or update, I have spent a whole day trying to do this but my skills are limited. any help will be greatly appreciated.
Flight table
FLI_ID FLI_CO FLI_AIRCRA DEPT_AIRPORT ARRV_AIRPORT DEPT_TIME ARRV_TIME FLI_PRICE
1 BD425 Boeing 707 1 12 18-MAR-12 02.24.00 AM 18-MAR-12 06.24.00 AM 520
2 LX345 Beriev 30 6 7 20-MAR-12 03.30.00 PM 20-MAR-12 04.20.00 PM 250
3 NZ4445 Boeing 720 9 14 25-MAR-12 09.00.00 AM 25-MAR-12 05.00.00 PM 290
4 TP351 Boeing 767 10 15 25-MAR-12 11.25.00 AM 25-MAR-12 03.35.00 PM 399
5 BA472 Boeing 720 5 14 26-MAR-12 01.05.00 PM 26-MAR-12 04.15.00 PM 365
Accommodation
ACC_ID ACC_TYPE_CODE ACC_DESC ACC_PRICEPN ACC_ROOMS RESORT_ID ACC_ADDR CITY_ID
1 1 Three bedroom bungalow near theme park 500 3 1
2 1 Two bedroom bungalow next to disney house 350 2 1
3 1 One bedroom bungalow with lake view 250 2 2
4 2 One bedroom chalet near the lake 150 1 2
5 2 Four bedroom chalet near the tree house 600 4 3
Reservation
RESV_ID EMP_ID BOOKING_CUS_ID RESV_DATE HOLIDAY_S HOLIDAY_E IN_FLIGHT_ID OUT_FLIGHT_ID IN_FLIGHT_SEATS_NO OUT_FLIGHT_SEATS_NO ACC_ID SUBTOTAL
1 338 1 16-FEB-12 18-MAR-12 20-APR-12 1 11 2 2 2
2 335 3 10-JAN-12 20-MAR-12 22-APR-12 2 12 2 2 4
3 338 5 05-MAR-12 25-MAR-12 26-APR-12 3 13 2 2 6
4 328 7 02-JAN-12 25-MAR-12 25-APR-12 4 14 2 2 7
5 311 9 20-JAN-12 26-MAR-12 21-APR-12 5 15 2 2
6 317 11 07-JAN-12 27-MAR-12 22-APR-12 6 16 2 2
7 344 13 29-FEB-12 15-MAR-12 12-APR-12 7 17 2 2
8 326 15 11-JAN-12 18-MAR-12 12-APR-14 8 18 2 2
9 329 17 16-JAN-12 19-MAR-12 17-APR-12 25
10 323 19 18-FEB-12 20-MAR-12 21-APR-12 19
Okay I managed to yield the results that i wanted
SELECT HR.RESV_ID, F_IN.FLI_ID, F_IN.FLI_PRICE, F_OUT.FLI_ID, F_OUT.FLI_PRICE, AC.ACC_ID, AC.ACC_PRICEPN, NVL(F_IN.FLI_PRICE,0)+NVL(F_OUT.FLI_PRICE,0)+NVL(AC.ACC_PRICEPN,0) AS TOTAL
FROM HOLIDAY_RESERVATION HR
LEFT JOIN FLIGHT F_IN ON HR.IN_FLIGHT_ID = F_IN.FLI_ID
LEFT JOIN FLIGHT F_OUT ON HR.OUT_FLIGHT_ID = F_OUT.FLI_ID
LEFT JOIN ACCOMMODATION AC ON HR.ACC_ID = AC.ACC_ID
ORDER BY HR.RESV_ID;
YIELDS
RESV_ID FLI_ID FLI_PRICE FLI_ID FLI_PRICE ACC_ID ACC_PRICEPN TOTAL
---------- ---------- ---------- ---------- ---------- ---------- ----------- ----------
1 1 500 11 555 2 350 1405
2 2 150 12 253 4 150 553
3 3 300 13 345 6 450 1095
4 4 450 14 343 7 650 1443
5 5 345 15 242 587
6 6 460 16 460 920
7 7 250 17 250 500
8 8 550 18 550 1100
9 25 250 250
10 19 450 450
And the following statement is to update the reservation table.
Thanks to Leigh Riffel from DBA stackxchange for the following code
UPDATE HOLIDAY_RESERVATION R SET SUBTOTAL =
NVL((SELECT F.FLI_PRICE FROM FLIGHT F WHERE F.FLI_ID = R.IN_FLIGHT_ID), 0) +
NVL((SELECT F.FLI_PRICE FROM FLIGHT F WHERE F.FLI_ID = R.OUT_FLIGHT_ID), 0) +
NVL((SELECT AC.ACC_PRICEPN FROM ACCOMMODATION AC WHERE AC.ACC_ID = R.ACC_ID), 0);
Now the subtotal is populated with the values obtained from the sum performed above >>
RESV_ID EMP_ID BOOKING_CUS_ID RESV_DATE HOLIDAY_S HOLIDAY_E IN_FLIGHT_ID OUT_FLIGHT_ID IN_FLIGHT_SEATS_NO OUT_FLIGHT_SEATS_NO ACC_ID SUBTOTAL
---------- ---------- -------------- --------- --------- --------- ------------ ------------- ------------------ ------------------- ---------- ----------
1 338 1 16-FEB-12 18-MAR-12 20-APR-12 1 11 2 2 2 1405
2 335 3 10-JAN-12 20-MAR-12 22-APR-12 2 12 2 2 4 553
3 338 5 05-MAR-12 25-MAR-12 26-APR-12 3 13 2 2 6 1095
4 328 7 02-JAN-12 25-MAR-12 25-APR-12 4 14 2 2 7 1443
5 311 9 20-JAN-12 26-MAR-12 21-APR-12 5 15 2 2 587
6 317 11 07-JAN-12 27-MAR-12 22-APR-12 6 16 2 2 920
7 344 13 29-FEB-12 15-MAR-12 12-APR-12 7 17 2 2 500
8 326 15 11-JAN-12 18-MAR-12 12-APR-14 8 18 2 2 1100
9 329 17 16-JAN-12 19-MAR-12 17-APR-12 25 250
10 323 19 18-FEB-12 20-MAR-12 21-APR-12 19 450
Subsequently the code was added to a trigger (which was the original intention)
CREATE OR REPLACE TRIGGER HR_SUBTOTAL
BEFORE INSERT OR UPDATE ON HOLIDAY_RESERVATION
FOR EACH ROW
BEGIN
SELECT
NVL((SELECT F.Fli_Price FROM Flight F WHERE F.Fli_ID = :new.In_Flight_ID), 0) +
NVL((SELECT F.Fli_Price FROM Flight F WHERE F.Fli_ID = :new.Out_Flight_ID), 0) +
NVL((SELECT AC.Acc_PricePn FROM Accomodation AC WHERE AC.Acc_ID = :new.Acc_ID), 0)
INTO :new.Subtotal
FROM dual;
END;
/
For the SUM, assuming you want to treat NULL values as 0, you'd just need to do an NVL on the numbers
NVL( DEPART_FLIGHT_PRICE, 0 ) +
NVL( RETURN_FLIGHT_PRICE, 0 ) +
NVL( ACCOMMODATION_PRICE, 0 )
As for the UPDATE, it sounds like you just need a correlated UPDATE statement.
UPDATE reservation r
SET subtotal = (SELECT (SELECT NVL( DEPART_FLIGHT_PRICE, 0 ) +
NVL( RETURN_FLIGHT_PRICE, 0 ) +
NVL( ACCOMMODATION_PRICE, 0 )
FROM (SELECT RESV_ID,
BOOKING_CUS_ID,
ACC_ID,
(SELECT F.FLI_PRICE
FROM FLIGHT F
WHERE F.FLI_ID = R.IN_FLIGHT_ID) AS DEPART_FLIGHT_PRICE,
(SELECT F1.FLI_PRICE
FROM FLIGHT F1
WHERE F1.FLI_ID = R.OUT_FLIGHT_ID) AS RETURN_FLIGHT_PRICE,
(SELECT AC.ACC_PRICEPN
FROM ACCOMMODATION AC
WHERE AC.ACC_ID = R.ACC_ID) AS ACCOMMODATION_PRICE
FROM dual));
You are asking:
How do I sum up the price fields, as you can see some of them can have nulls.
DEPART_FLIGHT_PRICE RETURN_FLIGHT_PRICE ACCOMMODATION_PRICE
Just enclose them in NVL function as follows:
NVL(DEPART_FLIGHT_PRICE, 0)
and then sum them up.
For the second part, what you need is a MERGE statement. A good example is at http://www.oracle-developer.net/display.php?id=203
Related
Group repeating pattern in pandas Dataframe
so i have a Dataframe that has a repeating Number Series that i want to group like this: Number Pattern Value Desired Group Value.1 1 723 1 Max of Group 2 400 1 Max of Group 8 235 1 Max of Group 5 387 2 Max of Group 7 911 2 Max of Group 3 365 3 Max of Group 4 270 3 Max of Group 5 194 3 Max of Group 7 452 3 Max of Group 100 716 4 Max of Group 104 69 4 Max of Group 2 846 5 Max of Group 3 474 5 Max of Group 4 524 5 Max of Group So essentially the number pattern is always monotonly increasing. Any Ideas?
You can compare Number Pattern by 1 with cumulative sum by Series.cumsum and then is used GroupBy.transform with max: df['Desired Group'] = df['Number Pattern'].eq(1).cumsum() df['Value.1'] = df.groupby('Desired Group')['Value'].transform('max') print (df) Number Pattern Value Desired Group Value.1 0 1 723 1 723 1 2 400 1 723 2 3 235 1 723 3 1 387 2 911 4 2 911 2 911 5 1 365 3 452 6 2 270 3 452 7 3 194 3 452 8 4 452 3 452 9 1 716 4 716 10 2 69 4 716 11 1 846 5 846 12 2 474 5 846 13 3 524 5 846 For monotically increasing use: df['Desired Group'] = (~df['Number Pattern'].diff().gt(0)).cumsum()
Splitting Column Headers and Duplicating Row Values in Pandas Dataframe
In the example df below, I'm trying to find a way to split the column headers ('1;2','4','5;6') based on the ';' that exists and duplicate the row values in these split columns. (My actual df comes from an imported csv file so generally I have around 50-80 column headers that need spliting) Below is my code below with output import pandas as pd import numpy as np # data = np.array([['Market','Product Code','1;2','4','5;6'], ['Total Customers',123,1,500,400], ['Total Customers',123,2,400,320], ['Major Customer 1',123,1,100,220], ['Major Customer 1',123,2,230,230], ['Major Customer 2',123,1,130,30], ['Major Customer 2',123,2,20,10], ['Total Customers',456,1,500,400], ['Total Customers',456,2,400,320], ['Major Customer 1',456,1,100,220], ['Major Customer 1',456,2,230,230], ['Major Customer 2',456,1,130,30], ['Major Customer 2',456,2,20,10]]) df =pd.DataFrame(data) df.columns = df.iloc[0] df = df.reindex(df.index.drop(0)) print (df) 0 Market Product Code 1;2 4 5;6 1 Total Customers 123 1 500 400 2 Total Customers 123 2 400 320 3 Major Customer 1 123 1 100 220 4 Major Customer 1 123 2 230 230 5 Major Customer 2 123 1 130 30 6 Major Customer 2 123 2 20 10 7 Total Customers 456 1 500 400 8 Total Customers 456 2 400 320 9 Major Customer 1 456 1 100 220 10 Major Customer 1 456 2 230 230 11 Major Customer 2 456 1 130 30 12 Major Customer 2 456 2 20 10 Below is my desired output 0 Market Product Code 1 2 4 5 6 1 Total Customers 123 1 1 500 400 400 2 Total Customers 123 2 2 400 320 320 3 Major Customer 1 123 1 1 100 220 220 4 Major Customer 1 123 2 2 230 230 230 5 Major Customer 2 123 1 1 130 30 30 6 Major Customer 2 123 2 2 20 10 10 7 Total Customers 456 1 1 500 400 400 8 Total Customers 456 2 2 400 320 320 9 Major Customer 1 456 1 1 100 220 220 10 Major Customer 1 456 2 2 230 230 230 11 Major Customer 2 456 1 1 130 30 30 12 Major Customer 2 456 2 2 20 10 10 Ideally I would like to perform such a task at the 'read_csv' level. Any thoughts?
Try reindex with repeat s=df.columns.str.split(';') df=df.reindex(columns=df.columns.repeat(s.str.len())) df.columns=sum(s.tolist(),[]) df Out[247]: Market Product Code 1 2 4 5 6 1 Total Customers 123 1 1 500 400 400 2 Total Customers 123 2 2 400 320 320 3 Major Customer 1 123 1 1 100 220 220 4 Major Customer 1 123 2 2 230 230 230 5 Major Customer 2 123 1 1 130 30 30 6 Major Customer 2 123 2 2 20 10 10 7 Total Customers 456 1 1 500 400 400 8 Total Customers 456 2 2 400 320 320 9 Major Customer 1 456 1 1 100 220 220 10 Major Customer 1 456 2 2 230 230 230 11 Major Customer 2 456 1 1 130 30 30 12 Major Customer 2 456 2 2 20 10 10
You can split the columns with ';' and then reconstruct a df: pd.DataFrame({c:df[t] for t in df.columns for c in t.split(';')}) Out[157]: 1 2 4 5 6 Market Product Code 1 1 1 500 400 400 Total Customers 123 2 2 2 400 320 320 Total Customers 123 3 1 1 100 220 220 Major Customer 1 123 4 2 2 230 230 230 Major Customer 1 123 5 1 1 130 30 30 Major Customer 2 123 6 2 2 20 10 10 Major Customer 2 123 7 1 1 500 400 400 Total Customers 456 8 2 2 400 320 320 Total Customers 456 9 1 1 100 220 220 Major Customer 1 456 10 2 2 230 230 230 Major Customer 1 456 11 1 1 130 30 30 Major Customer 2 456 12 2 2 20 10 10 Major Customer 2 456 Or if you would like to reserve column order: pd.concat([df[t].to_frame(c) for t in df.columns for c in t.split(';')],1) Out[167]: Market Product Code 1 2 4 5 6 1 Total Customers 123 1 1 500 400 400 2 Total Customers 123 2 2 400 320 320 3 Major Customer 1 123 1 1 100 220 220 4 Major Customer 1 123 2 2 230 230 230 5 Major Customer 2 123 1 1 130 30 30 6 Major Customer 2 123 2 2 20 10 10 7 Total Customers 456 1 1 500 400 400 8 Total Customers 456 2 2 400 320 320 9 Major Customer 1 456 1 1 100 220 220 10 Major Customer 1 456 2 2 230 230 230 11 Major Customer 2 456 1 1 130 30 30 12 Major Customer 2 456 2 2 20 10 10
SUM column values based on two rows in the same tables in SQL
I have one table like below in my SQL server. Trans_id br_code bill_no amount 1 22 111 10 2 22 111 20 3 22 111 30 4 22 111 40 5 22 111 10 6 23 112 20 7 23 112 20 8 23 112 20 9 23 112 30 and I want desired output like below table s.no br_code bill_no amount 1 22 111 110 2 23 112 90
try this: select br_code, bill_no, sum(amount) from TABLE group by br_code, bill_no
how to update duplicate rows in a column to a new values
I will explain my problem briefly have duplicate rino like below (actually this rino is the serial number in front end) chqid rino branchid ----- ---- -------- 876 6 2 14 6 2 18 10 2 828 10 2 829 11 2 19 11 2 830 12 2 20 12 2 78 40 2 1092 40 2 1094 41 2 79 41 2 413 43 2 1103 43 2 82 44 2 1104 44 2 1105 45 2 83 45 2 91 46 2 1106 46 2 here in my case I don't want to delete these duplicate rino instead of that I planned to update the rino having max date(column not specified in the above sample actually a date column is there) to the next rino number what exactly I meant is : if I sort out the above result according to the max(date) I will get chqid rino branchid ----- ---- -------- 876 6 2 828 10 2 19 11 2 830 12 2 1092 40 2 79 41 2 413 43 2 82 44 2 83 45 2 1106 46 2 (NOTE : total number of duplicate rows are 10 in branchid=2) the last entered rino in the table for branchid=2 is 245 So I just want to update the 10 rows(Of column rino) with numbers starting from 246 to 255( just added 245+10 like this select lastno+ generate_series(1,10) nos from tab where cola=4 and branchid = 2 and vrid=20;) Expected Output: chqid rino branchid ----- ---- -------- 876 246 2 828 247 2 19 248 2 830 249 2 1092 250 2 79 251 2 413 252 2 82 253 2 83 254 2 1106 255 2 using postgresql
Finally I found a solution, am using dynamic-sql to solve my issue do $$ declare arow record; begin for arow in select chqid,rino,branchid from ( select chqid,rino::int ,vrid,branchid ,row_number()over (partition by rino::int ) rn from tab where vrid =20 and branchid = 2)t where rn >1 loop execute format(' update tab set rino=(select max(rino::int)+1 from gtab19 where acyrid=4 and branchid = 2 and vrid=20) where chqid=%s ',arow.chqid); end loop; end; $$;
SQL query self join
I am working on a query for a report in Oracle 10g. I need to generate a short list of each course along with the number of times they were offered in the past year (including ones that weren't actually offered). I created one query SELECT coursenumber, count(datestart) AS Offered FROM class WHERE datestart BETWEEN (sysdate-365) AND sysdate GROUP BY coursenumber; Which produces COURSENUMBER OFFERED ---- ---------- ST03 2 PD01 1 AY03 2 TB01 4 This query is all correct. However ideally I want it to list those along with COURSENUMBER HY and CS in the left column as well with 0 or null as the OFFERED value. I have a feeling this involves a join of sorts, but so far what I have tried doesn't produce the classes with nothing offered. The table normally looks like REFERENCE_NO DATESTART TIME TIME EID ROOMID COURSENUMBER ------------ --------- ---- ---- ---------- ---------- ---- 256 03-MAR-11 0930 1100 2 2 PD01 257 03-MAY-11 0930 1100 12 7 PD01 258 18-MAY-11 1230 0100 12 7 PD01 259 24-OCT-11 1930 2015 6 2 CS01 260 17-JUN-11 1130 1300 6 4 CS01 261 25-MAY-11 1900 2000 13 6 HY01 262 25-MAY-11 1900 2000 13 6 HY01 263 04-APR-11 0930 1100 13 5 ST03 264 13-SEP-11 1930 2100 6 4 ST03 265 05-NOV-11 1930 2100 6 5 ST03 266 04-FEB-11 1430 1600 6 5 ST03 267 02-JAN-11 0630 0700 13 1 TB01 268 01-FEB-11 0630 0700 13 1 TB01 269 01-MAR-11 0630 0700 13 1 TB01 270 01-APR-11 0630 0700 13 1 TB01 271 01-MAY-11 0630 0700 13 1 TB01 272 14-MAR-11 0830 0915 4 3 AY03 273 19-APR-11 0930 1015 4 3 AY03 274 17-JUN-11 0830 0915 14 3 AY03 275 14-AUG-09 0930 1015 14 3 AY03 276 03-MAY-09 0830 0915 14 3 AY03
SELECT coursenumber, COUNT(CASE WHEN datestart BETWEEN (sysdate-365) AND sysdate THEN 1 END) AS Offered FROM class GROUP BY coursenumber; So, as you can see, this particular problem doesn't need a join.
I think something like this should work for you, by just doing it as a subquery. SELECT distinct c.coursenumber, (SELECT COUNT(*) FROM class WHERE class.coursenumber = c.coursenumber AND datestart BETWEEN (sysdate-365) AND sysdate ) AS Offered FROM class c
I like jschoen's answer better for this particular case (when you want one and only one row and column out of the subquery for each row of the main query), but just to demonstrate another way to do it: select t1.coursenumber, nvl(t2.cnt,0) from class t1 left outer join ( select coursenumber, count(*) cnt from class where datestart between (sysdate-365) AND sysdate group by coursenumber ) t2 on t1.coursenumber = t2.coursenumber