Query evaluation with "INTERSECT" function - sql

I've a query:
QUERY1{statements...}
INTERSECT
QUERY2{statements...}
I need to evaluate these 2 queries according to giving database data, my question is:
do I have to evaluate each query separately and then combine the 2 results together? i.g: cost(Query1) + cost(Query2) = Total query's cost ? .. or there is another way to solve this?

Yes, you have to add the 2 queries' cost.
In Toad Oracle you can evaluate the global intersect query:

Related

Can you use results from 2 sql queries and perform calculations

I have a requirement to calculate a number value by using the results of two sql queries. I know this is possible in python etc but if I want to use the results of sql queries in say, apache superset, how can I achieve this?
For ex:
query1: select * from consumer;
result: 190 rows
query2: select * from producer;
result: 230 rows
value I need, called output : 190/230.
Is this possible using sub queries? How can I extract the output alone?

Oracle SQL query Optimization with multiple filters and create table

My select query is running for 15 hours and still running so I cancelled it. How can I optimize my query?
CREATE TABLE W_PURCH_RCPT_FS_tmp
AS
SELECT RCV.TRANSACTION_ID,
PO.RELEASE_DATE,
NVL (ASL.PROCESSING_LEAD_TIME, MTL.FULL_LEAD_TIME)
AS PROCESSING_LEAD_TIME
FROM obia_ui.PO_ASL_ATTRIBUTES#betsy.world#dsn_310 ASL,
obia_ui.MTL_SYSTEM_ITEMS_B#betsy.world#dsn_310 MTL,
obia_ui.RCV_TRANSACTIONS#betsy.world#dsn_310 RCV,
obia_ui.PO_RELEASES_ALL#betsy.world#dsn_310 PO,
obia_ui.PO_LINES_ALL#betsy.world#dsn_310 LINES,
obia_ui.PO_LINE_LOCATIONS_ALL#betsy.world#dsn_310 LOC,
obia_ui.PO_HEADERS_ALL#betsy.world#dsn_310 HEAD
WHERE RCV.CREATION_DATE > '30-JUN-2016'
AND MTL.INVENTORY_ITEM_ID = LINES.ITEM_ID
AND MTL.ORGANIZATION_ID = LOC.SHIP_TO_ORGANIZATION_ID
AND LINES.PO_LINE_ID = LOC.PO_LINE_ID
AND ASL.USING_ORGANIZATION_ID = LOC.SHIP_TO_ORGANIZATION_ID
AND ASL.ITEM_ID = LINES.ITEM_ID
AND ASL.VENDOR_ID = HEAD.VENDOR_ID
AND ASL.VENDOR_SITE_ID = HEAD.VENDOR_SITE_ID
AND RCV.TRANSACTION_ID = PO.PO_RELEASE_ID
;
I expect that query to finish shorter.
First of all you have to master explain plan concept to optimize a query in Oracle. After understanding explain plans I would suggest to go for table by table. Starting from the first table check how it performs on an explain plan. By this way you can figure out the indexes that need to be created. Sometimes there's no way to create a useful index. In that case splitting the data into smaller chunks (tables) may help. Also Oracle has a parallel query running capabilities. You can take advantage of this feature too.

SQL - HAVING (execution vs structure)

I'm a beginner, studying on my own... please help me to clarify something about a query: I am working with a soccer database and trying to answer this question: list all seasons with an avg goal per Match rate of over 1, in Matchs that didn’t end with a draw;
The right query for it is:
select season,round((sum(home_team_goal+away_team_goal) *1.0) /count(id),3) as ratio
from match
where home_team_goal != away_team_goal
group by season
having ratio > 1
I don't understand 2 things about this query:
Why do I *1.0? why is it necessary?
I know that the execution in SQL is by this order:
from
where
group
having
select
So how does this query include: having ratio>1 if the "ratio" is only defined in the "select" which is executed AFTER the HAVING?
Am I confused?
Thanks in advance for the help!
The multiplication is added as a typecast to convert INT to FLOAT because by default sum of ints is int and the division looses decimal places after dividing 2 ints.
HAVING. You can consider HAVING as WHERE but applied to the query results. Imagine the query is executed first without HAVING and then the HAVING condition is applied to result rows leaving only suitable ones.
In you case you first select grouped data and calculate aggregated results and then skip unnecessary results of aggregation.
the *1.0 is used for its ".0" part so that it tells the system to treat the expression as a decimal, and thus not make an integer division which would cut-off the decimal part (eg 1 instead of 1.33).
About the second part: select being at the end just means that the last thing
to be done is showing the data. Hoewever, assigning an alias to a calculated field is being done, you could say, at first priority. Still, I am a bit doubtful; I am almost certain field aliases cannot be used in the where/group by/having in, say, sql server.
There is no order of execution of a SQL query. SQL is a descriptive language not a procedural language. A SQL query describes the result set that the query is producing. The SQL engine can execute it however it likes. In fact, most SQL engines compile the query into a directed acyclic graph, which looks nothing like the original query.
What you are referring to might be better phrased as the "order of interpretation". This is more simply described by simple rules. Column aliases can be used in the ORDER BY clause in any database. They cannot be used in the FROM, WHERE, or GROUP BY clauses. Some databases -- such as SQLite -- allow them to be referenced in the HAVING clause.
As for the * 1.0, it is because some databases -- such as SQLite -- do integer arithmetic. However, the logic that you want is probably more simply expressed as:
round((avg(home_team_goal + away_team_goal * 1.0), 3)

Difference between using 'in' and statements combined by 'or'

what is the difference between this two queries
select * from ggk where gid in (100,200,300,400,500);
select * from ggk where gid=100 or gid=200 or gid=300 or gid=400 or gid=500;
I know that the first one is easy to write and it is eventually converted to the second query. But that will decrease the performance if there are more parameters.
Thanks in advance

BAPI_MDDATASET_CREATE_OBJECT MDX Query

I would like to make a BAPI call from Java to the BAPI "BAPI_MDDATASET_CREATE_OBJECT".
Therefore I want to use the following MDX Query:
SELECT
[0DISTR_CHAN].Members ON COLUMNS,
[0MATERIAL].Members ON ROWS
FROM [$/PKG/AB_C01]
Because the Parameter "COMMAND_TEXT" is to short I made 4 entries like this:
Now I run the BAPI and get a DATASETID in return. I use this id with the next BAPI "BAPI_MDDATASET_CHECK_SYNTAX"
But now I get an exception:
English Version:
Why is this so?
This MDX query seems to be OK, because in transcation "MDXTEST" this query is working.
Any suggestions?
I had the same problem only with c# and sap connector.
I solved it. Need to use all commands in one context:
RfcSessionManager.BeginContext(destination);
Create_Object();
CheckSyntax();
etc.
RfcSessionManager.EndContext(this.destination);
The following will return a large table that is n * m where n are the number of columns and m is the number of rows:
SELECT
[0DISTR_CHAN].Members ON COLUMNS,
[0MATERIAL].Members ON ROWS
FROM [$/PKG/AB_C01]
n = number of members in this dimension [0DISTR_CHAN] + 1
m = number of members in this dimension [0MATERIAL]
This is not an answer but maybe will help to discover if the mdx is the problem. If you simplify the above to just a single member on rows, and a single member on columns do you still get an error?
SELECT
{[0DISTR_CHAN].[0DISTR_CHAN].[someMemberY]} ON COLUMNS,
{[0MATERIAL].[0MATERIAL].[someMemberX]} ON ROWS
FROM [$/PKG/AB_C01];