How to run two ORACLE SQL Queries together in TOAD? - sql

I want to run two Oracle SQL queries together inside TOAD.
first query:
SELECT * FROM OT_SO_HEAD WHERE SOH_ANNOTATION = 'ECSO10012791'
and second:
SELECT * FROM OT_SO_ITEM WHERE SOI_SOH_SYS_ID = '30977853'
Please guide

You can use UNION or UNION ALL.
The UNION operator is used to combine the result-set of two or more SELECT statements.
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.

right click in script window and select EXECUTE -> Execute via Toad Script Runner and reuse Toad

Related

DB2 SELECT EXCEPT with WHERE clause

I'm trying to compare two tables in a DB2 database in z/OS using SPUFI to submit SQL queries.
I'm doing this by using EXCEPT to see the difference between two SELECT queries.
I need to filter the SELECT statement from the first query with a WHERE clause.
SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1'
EXCEPT
SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2
I got results back, but it also returned an error -199 Is this because the WHERE clause is not present in the second SELECT statement?
ERROR: ILLEGAL USE OF KEYWORD EXCEPT.
TOKEN <ERR_STMT> <WNG_STMT> GET SQL
SAVEPOINT HOLD FREE ASSOCIATE WAS EXPECTED
Try introducing parentheses e.g.
( SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1' )
EXCEPT
( SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2 )

Combine results of two table in SQL

i have two tables one contain computer_science_book and Mechanical_engineering_book i want the result which contail all books of computer_science_book and Mechanical_engineering_book .
give me suggetions.
You can use union all for that:
select * from computer_science_book
union all
select * from Mechanical_engineering_book
You can use the UNION operator:
select columns from computer_science_books
UNION
select columns from mechanical_eng_books
Note that the structure of the results from computer_science_books needs to match the structure of results from mechanical_eng_books. The reason is that, as asked for, there is only one result table. There is also the UNION ALL operator. The difference is that UNION removes duplicates whereas UNION ALL returns all combined rows.

Sybase - Subquery in FROM clause

I'm using Sybase ASE 12.5.0.3 and I'm unable to do subqueries like:
select * from (select '1' union select '2' ) X
I've been looking around and as far as I know it should be possible after Sybase ASE 12, am I doing something wrong, or is it not possible with this version???
Edit - Even after changing the query to:
select * from (select '1' as col1 union select '2' as col1 ) X
So even giving alias to the columns, it fails anyways...
Without seeing an error message, it appears that you need to give column aliases in your sub-query:
select *
from
(
select '1' as yournewCol
union
select '2' as yournewCol
) X
You need to give your columns name. Try this:
Sybase ASE does not support subqueries in the FROM clause:
Subqueries can be nested inside the where or having clause of an outer select, insert, update, or delete statement, inside another subquery, or in a select list. Alternatively, you can write many statements that contain subqueries as joins; Adaptive Server processes such statements as joins.

Enter multiple rows explicitly in single select statement

I am testing a query and want to use SELECT to get some initial data like so:
SELECT 1,2,3
UNION
SELECT 2,3,4
Is there a syntax to fold these two selects into one or do I have to use a UNION statement for each row?
You can use values clause
select t.* from (values(1,2,3), (2,3,4)) as t(col1,col2,col3)
If you want to display the result as one result set then you have to use union/union all

Support UNION function in BigQuery SQL

BigQuery does not seem to have support for UNION yet:
https://developers.google.com/bigquery/docs/query-reference
(I don't mean unioning tables together for the source. It has that.)
Is it coming soon?
If you want UNION so that you can combine query results, you can use subselects
in BigQuery:
SELECT foo, bar
FROM
(SELECT integer(id) AS foo, string(title) AS bar
FROM publicdata:samples.wikipedia limit 10),
(SELECT integer(year) AS foo, string(state) AS bar
FROM publicdata:samples.natality limit 10);
This is almost exactly equivalent to the SQL
SELECT id AS foo, title AS bar
FROM publicdata:samples.wikipedia limit 10
UNION ALL
SELECT year AS foo, state AS bar
FROM publicdata:samples.natality limit 10;
(note that if want SQL UNION and not UNION ALL this won't work)
Alternately, you could run two queries and append the result.
BigQuery recently added support for Standard SQL, including the UNION operation.
When submitting a query through the web UI, just make sure to uncheck "Use Legacy SQL" under the SQL Version rubric:
You can always do:
SELECT * FROM (query 1), (query 2);
It does the same thing as :
SELECT * from query1 UNION select * from query 2;
Note that, if you're using standard SQL, the comma operator now means JOIN - you have to use the UNION syntax if you want a union:
In legacy SQL, the comma operator , has the non-standard meaning of UNION ALL when applied to tables. In standard SQL, the comma operator has the standard meaning of JOIN.
For example:
#standardSQL
SELECT
column_name,
count(*)
from
(SELECT * FROM me.table1 UNION ALL SELECT * FROM me.table2)
group by 1
This helped me out very much for doing a UNION INTERSECT with big query's StandardSQL.
#standardSQL
WITH
a AS (
SELECT
*
FROM
table_a),
b AS (
SELECT
*
FROM
table_b)
SELECT
*
FROM
a INTERSECT DISTINCT
SELECT
*
FROM
b
I STOLE/MODIFIED THIS EXAMPLE FROM: https://gist.github.com/yancya/bf38d1b60edf972140492e3efd0955d0
Unions are indeed supported. An excerpt from the link that you posted:
Note: Unlike many other SQL-based systems, BigQuery uses the comma syntax to indicate table unions, not joins. This means you can run a query over several tables with compatible schemas as follows:
// Find suspicious activity over several days
SELECT FORMAT_UTC_USEC(event.timestamp_in_usec) AS time, request_url
FROM [applogs.events_20120501], [applogs.events_20120502], [applogs.events_20120503]
WHERE event.username = 'root' AND NOT event.source_ip.is_internal;