improving query or logic in a oracle stored procedure? - stored-procedures

I have 3 tables as follows:
CUSTOMER
CIF SUMOFALLACCOUNTBALACE
A1 6000
A2 2000
A3 7000
ACCOUNT
ACCOUNTNUMBER ACCOUNTBALANCE
B1 1000
B2 2000
B3 3000
B4 4000
B5 5000
RELATION
CIF ACCOUNTNUMBER
A1 B1
A1 B5
A2 B2
A3 B3
A3 B4
I need to update SUMOFALLACCOUNTBALACE in CUSTOMER table of all the available ACCOUNTNUMBERs of respective CIFs using RELATION table.
The sample query I built for 1 by 1 CIF is :-
UPDATE CUSTOMER
SET SUMOFALLACCOUNTBALACE =
(SELECT SUM(ACCOUNTBALANCE)
FROM ACCOUNT
WHERE ACCOUNTNUMBER IN
(SELECT ACCOUNTNUMBER
FROM RELATION
WHERE CIF IN
(SELECT CIF
FROM CUSTOMER
WHERE CIF = 'A1')))
WHERE CIF = 'A1'
which is giving me right results but it is taking time when large data, also I need to pass CIF 1 by 1 in oracle procedure using loop.
Can anybody please suggest a better way to do this?
Thanks in Advance,
Gopal Arora

You could execute a single statement which updates all SUMOFACCOUNTBALANCEs in one go:
UPDATE CUSTOMER C
SET SUMOFALLACCOUNTBALACE =
(SELECT SUM(A.ACCOUNTBALANCE)
FROM RELATION R
INNER JOIN ACCOUNT A
ON (A.ACCOUNTNUMBER = R.ACCOUNTNUMBER)
WHERE R.CIF = C.CIF)
However, in general, you should not store the results of computations due to the danger of the computation result not being kept up to date when the elements forming that computation are changed. It would be better if you did not have a CUSTOMER.SUMOFALLACCOUNTBALACE column and instead you calculated it from the base data when required. In this way you can be assured that its value is correct at all times.
SELECT C.CIF
,SUM(A.ACCOUNTBALANCE) SUMOFALLACCOUNTBALACE
FROM CUSTOMER C
LEFT OUTER JOIN RELATION R
ON (R.CIF = C.CIF)
LEFT OUTER JOIN ACCOUNT A
ON (A.ACCOUNTNUMBER = R.ACCOUNTNUMBER)

Related

vlookup several criteria and a range

Here's my table:
Exchange No.
Name
Tier
30d Volume (higher than)
Maker
Taker
Specials
1
FTX
1
$0
0.0200%
0.0700%
FTX
2
$2,000,000
0.0150%
0.0600%
FTX
3
$5,000,000
0.0100%
0.0550%
FTX
4
$10,000,000
0.0050%
0.0500%
2
Binance
Regular User
$0
0.0120%
0.0500%
Binance
VIP 1
$15,000,000
0.0120%
0.0500%
Binance
VIP 2
$50,000,000
-0.0100%
0.0500%
I want to retrieve the correct fees in another table as follows:
Volume (past 30d):
volume variable, ie $10,000
FTX
Binance
Column #
2
3
4
IN:
Maker
correct fee
correct fee
OUT-stop:
Maker
correct fee
correct fee
OUT-profit:
Maker
correct fee
correct fee
OUT-manually:
Maker
correct fee
correct fee
B3 cell (second table) should take the fee in cell E2 (of the first table).
Why?
Cause:
C1 (of second table) says "FTX" as per column B row 2:5 (of first
table)
Volume (in B1 of second table) is higher than D2 but lower than D3 (of first
table)
B3 (of second table) says "Maker" which is column F (of first
table)
So I tried to do a vlookup but only my criteria No. 3 would work with vlookup.
Other criteria are a range higher than (No. 1) and two different columns as "index" (in vlookup formula) which are, by the way, the searched text... (No. 2)
Someone has an idea to take into account those special criteria in vlookup, or similar, please?
try:
=INDEX(VLOOKUP(M2, QUERY({'Exchange Fees'!B2:B&":", 'Exchange Fees'!D2:D,
FILTER('Exchange Fees'!A2:G, 'Exchange Fees'!A1:G1=M3)},
"select Col2,Col3 where Col1 = '"&N2&"'", ), 2, 1))

Filter and sort results, ignoring rows with no data, with 2, 3 or 4 conditions

I have a large database from Y to BF.
Columns Y to AB are the categories of all the rest.
I want to apply a filter function that will only filter results with rows with data (ignore all other columns/rows blank).
Conditions for Filter are in B2 ; D2 ; F2 ; H2 .
I also want to be able to Sort results according to O2 and in a Ascending or Descending order according to Q2.
In A6 I have a formula that only shows titles with corresponding data, BUT it doesn't respect possible 4 conditions, only one, the first in B2
=FILTER( AD2:AJ2,INDEX(AD2:AJ,MATCH( B2,Y3:Y,0 ),)<>"" )
In A7 i have a formula (not working) that only fills filtered results according with B2 + D2 + F2 + H2 BUT doesn't show only rows with data, it shows all columns
=IFERROR(SORT(FILTER(AD3:BF,
IF(B2="",ROW(Y3:Y),Y3:Y=B2)*
IF(D2="",ROW(Z3:Z),Z3:Z=D2)*
IF(F2="",ROW(AA3:AA),AA3:AA=F2)*
IF(H2="",ROW(AB3:AB),AB3:AB=H2)),
IF(O2="",1,MATCH(O2,A6:R6,0)),
IF(Q2="Asc",1,0)),"Sem Navios")
My difficulty is that my current formulas respect the 4 conditions (even in case some one is blank) BUT does not filter columns only with data, it spills all columns and that is very confusing.
Can someone help me getting to filter data according to 4 conditions (even in case some one is blank), being able to sort and only shows rows with data?
Thanks.
Example sheet here https://docs.google.com/spreadsheets/d/11B-4rjP9_8brvF0A3yVJlIoiyXWqxM91qWtKRSD8tLI/edit?usp=sharing
Feel free to edit.
I have a "desired results" tab, with no formulas, just showing what I pretend.

Counting unique values when other criteria must also be met

I'm having difficultly with Google Sheets counting unique rows, where other criteria in the formula must also be met.
I've created a simple Google Sheet to highlight the problem I need solving.
https://docs.google.com/spreadsheets/d/1P0sHIUN12Wpbr6vKJAwsPluj-T2H-NrPpQ1_WWV7lvI/edit?usp=sharing
I need the formula in cell C5 to only count unique values in row G, where the criteria is met in rows F & H (the SP is “DG” and the likelihood is “Booked”). I cannot work out how to make this only count unique values. The formula I currently have only counts any cells that are not blank in row G, it does not look for unique values but needs to.
The working formula should give the following answers:
C3 = <> C5 should equal 3
C3 = 1 C5 should equal 1
C3 = 2 C5 should equal 2
C3 = 3 C5 should equal 1
Please can someone help?
EDIT
The formulas I've already tried won't make sense with the example in the link but to show what I have attempted
=COUNTIF(UNIQUE(QUERY(AA:AM,"Select AC where AA = 'DG' and AL = 'Booked' and AM = '" & I2 & "'")),"<>")
It returned a 1 regardless of how many it should have returned.
=COUNTUNIQUE(QUERY(F2:I21,"Select G where F='DG' and H='Booked' "&IF(ISBLANK(C3),,"and I="&C3&"")))
QUERY to get G column subject to conditions
COUNTUNIQUE to count unique G Rows

SUMIFs by only calculating unique values in another column

I currently have a table with five columns:
A = Campaign
B = Person
C = Opportunity Name
D = Total Cost of Campaign
E = Date
I'm trying to use =sumifs to calculate the amount in column D only if: (1) it matches the exact value in cell H2 to column A, (2) the date range, in column E, is greater than the value in cell I2, and (3) it removes duplicates based on value in column C (so you can't sum if value appears more than once).
Please refer to my data table (also attached) as reference:
Campaign Person Opportunity Name Total Cost of Campaign Date
A Bob Airbnb 5000 3/2/2017
B Jim Sony 10000 3/2/2017
B Jane Coca-Cola 10000 3/2/2017
C Jim Sony 200 3/2/2017
B Daniel Sony 10000 3/2/2017
B April Coca-Cola 10000 3/5/2017
How could I do this?
Thanks in advance for the help!
Try:
=SUM(
UNIQUE(
QUERY(A:E,"select C, D where A = '"&H2&"' and E > date '"&TEXT(H3,"YYYY-MM-DD")&"'")))
Query will cut first two conditions. Unique will remove duplicates:

Select Random item from list in Google Sheet

Im working on a google sheet document to randomly pick a protein and a side dish for weeknight meals based on a selection. The idea is you select your meat (chicken, beef, pork, etc) and the max amount of calories for the meal and it will select a protein and side dish randomly from a list. Not sure how to go about writing either the equation or a script to do this though. Heres what im working with so far:
This is the main page where you select what you want
This is the protein sheet that will have all the possible selections, so based on the image above would want to randomly pick a chicken dish with 800 or less calories
And finally this is the side, for this I would want to choose a side that is less than the remaining calories, so if the protein had say 500 calories would want a side with 300 or less:
On the Proteins sheet add E3 =randbetween(1,100) and copy the formula down. On the Side/Vegetables add C3 =randbetween(1,100) and copy down. On the Dinner Generator sheet add this query to A8:
=query(Sheet2!A3:E,"select A,B where D contains '"& A3 &"' and B <= "& B3 &" order by E desc Limit 1")
and this to A9:
=query(Sheet3!A3:C,"Select A,B where B <= ("& B3 &" - "& B8 &") order by C desc Limit 1")
And this to A10:
=B8+B9
I named the sheets Sheet1, Sheet2, and Sheet3. Change the sheet names in the queries as needed. I also used desc in the queries so blanks would be at the bottom allowing the lists to be expanded without having to change the queries. I suggest you change fish entries in column D to Fish instead of Tuna, Salmon, etc. Below is a shared example spreadsheet. Make a copy to test.
https://docs.google.com/spreadsheets/d/17SWtKfzDFDV8OeffR7s5PcmmwRMzu6UKzWK26CX7KDU/edit?usp=sharing

Resources