SELECT on two other queries in Oracle - sql

So, lets say I want to do something like:
SELECT Query1.a,
Query2.b
FROM (
SELECT q as a
FROM somewhere
),
(
SELECT g as b
FROM elsewhere
)
where Query 1 is
(
SELECT q as a
FROM somewhere
)
and Query2 is
(
SELECT g as b
FROM elsewhere
)
So, i want to select from two other select statements.
Query 1 produces a table
a
value1
Query 2 produces a table
b
value 2
And Query 3 (the outer select statement) produces
a b
value 1 value 2
So, essentially, two result tables are combined as columns and not as rows.
Thank you, if you have any hints.

You basically have your solution. You are only missing the names of your queries, so do like this:
SELECT Query1.a,
Query2.b
FROM (
SELECT q as a
FROM somewhere
) Query1,
(
SELECT g as b
FROM elsewhere
) Query2

It's not clear how you need to connect different rows from tables but it can be something like this:
select query1.a,
query2.b
FROM
(select q as a, ROW_NUMBER() OVER (ORDER BY q) as RN from a) Query1
FULL JOIN
(select q as b, ROW_NUMBER() OVER (ORDER BY q) as RN from b) Query2
ON Query1.RN=Query2.RN
SQLFiddle example

Your syntax is a bit off the SQL charts, but in essence ritgh:
It is possible to do a subquery:
select A.field from (select field from a_table) A;
It is essential that you name your query, if you want to use it in the select or where clauses.
And even possible to combine them like regular tables:
select A.field, B.other_field from (select field from table1) A, (select other_field from table2) B;
It is also possible to do al kind of where, grouping and sorting stuff on it, but not needed in your case.

I assume this is what you're looking for:
SELECT query1.a, query2.b
FROM
(SELECT q as a FROM somewhere) query1,
(SELECT g as b FROM elsewhere) query2
Here is a SQLFiddle to test the query

Related

JOINING 2 QUERIES

I have a query which gives me results based in my where condition (a varchar e.g. 20200227Abc).
I run a union of those query with same query but changing the Where condition to (20200126xyz).
I want if the data is present for both where conditions only then give me the result of first query where (20200227abc).
Please advise.
If I understand your question correctly, you are looking to get the results for Condition 1 only when it satisfies condition 2 as well. You could use exists in this case. Using a sample:
;with cte as
(select 1 as id,'20200227Abc' as val union
select 1 as id,'20200126xyz' as val union
select 2 as id,'20200227Abc' as val)
select *
from cte a
where val='20200227Abc' and
exists (select 1 from cte b where b.val='20200126xyz' and a.id=b.id)
This should give you the desired result.
I think you want not exists:
select t.*
from t
where t.val = '20200227Abc' or
(t.val = '20200126xyz' and
not exists (select 1 from t where t.val = '20200227Abc')
);

SQL Server query for all columns with group by and having

I'm wondering is there a way to query all columns with group by and having in SQL Server? For example, I have 6 columns, a, b,…,f, and this is something I want to get:
Select *
From table
Group by table.b, table.c
Having max(table.d)=table.d
This works in sybase, since I'm trying to migrate stuff from sybase to SQL Server, I'm not sure what I can do in new environment. Thanks.
Why do you want to group by every column when you don't use any aggragate-functions in your select? Just use the following code to get all columns of the table:
select * from table
Group by only gets used when you have aggragete-functions (e.g. max(), avg(), count(), ...) in your select.
Having limits the aggrageted columns and where the normal columns of the table.
You can use MIN, MAX, AVG, and COUNT functions with the OVER clause to provide aggregated values for each column (to imitate the group by clause for each column) and Common table expression CTE to filter out the results (to imitate the having clause) as:
;With CTE as
(
SELECT
MIN(a) OVER (PARTITION BY a) AS MinCol_a
, MAX(b) OVER (PARTITION BY b) AS MaxCol_b
, AVG(c) OVER (PARTITION BY c) AS AvgCol_c
, COUNT(e) OVER (PARTITION BY d) AS Counte_PerCol_d
FROM Tbl_Test
)
select MinCol_a,MaxCol_b ,AvgCol_c,Counte_PerCol_d
from CTE
Join --here you can join the table Test results with other tables
where --any filter condition similar to Having clause
If what you want is to get the rows with maximum d for each combination of b and c then use NOT EXISTS:
select t.* from tablename t
where not exists (
select 1 from tablename
where b = t.b and c = t.c and d > t.d
)
or with rank() window function:
select t.a, t.b, t.c, t.d, t.e, t.f
from (
select *,
rank() over (partition by b, c order by d desc) rn
from tablename
) t
where t.rn = 1
Without using having you can get the result which you want. Try below
Select table.b, table.c, max(table.d)
From table
Group by table.b, table.c

Selecting random value for every row

Suppose I have 2 tables called 'FOR_TEST_1' with column A, B, C and 'FOR_TEST_2' with column D, E, F.
I would like to generate column A paired with a random value from column D.
Here is a snippet of the tables.
So far, this is the statement that I have been doing and it return the same value of D for every row in A.
Currently I am using toad for oracle, but I tried using the same logic in MySQL and it works fine.
You're expecting Oracle to execute the subquery once per row (which is what MySQL does). However, it seems you have run into a side-effect of an Oracle optimization. There's no correlation between the main query and the scalar subquery so Oracle decides to un-nest the subquery, execute it once and join the result to the main query.
To get the results you want you have a couple of options. One is to turn off the unnesting with the NO_UNNEST hint.
select t1.a
, ( select d from ( select /*+ NO_UNNEST */ d from for_test_two
order by dbms_random.value ) where rownum = 1) d
from for_test_one t1
/
Alternatively you could rewrite you query to use an inline view rather than a scalar subquery.
select t1.a
, t2.d
from ( select a, rownum as rn
from for_test_one) t1
join ( select d, rownum as rn
from ( select d from for_test_two
order by dbms_random.value() ) ) t2
on t1.rn = t2.rn
order by t1.rn
/
Warning: The NO_UNNEST solution doesn't work on SQL Fiddle demo (find it here). Not sure why, the syntax looks correct. So try it on your environment, or just use the second approach, which definitely works.
Try this:
SELECT A, (SELECT D (
SELECT D, ROWNUM ROWPTR FROM FOR_TEST_2)
WHERE ROWPTR = (SELECT ROUND(DBMS_RANDOM.VALUE(1, (SELECT COUNT(D) FROM FOR_TEST_2) + 1 )) from DUAL)) D
FROM FOR_TEST_1

Keep Track of already summed tuples sql

If we have a table with values for a and b, is there a way to only add up the b's if its not a duplicate a? For example
a b
1 2
2 3
2 3
so we would get only 5 (instead of 8)
A sort of
select sum(b if unique a),
from table
where ...
The following query selects the lowest value of b for each group a
select min(b) min_b
from mytable
group by a
You can then sum those values by selecting the sum from a derived table
select sum(min_b) from (
select min(b) min_b
from mytable
group by a
) t
http://sqlfiddle.com/#!9/d82c5/1
You haven't specified your RDBMS, but if you are using a database which supporting window functions like SQL Server, you can query the unique rows first by using WITH clause and ROW_NUMBER() function and then get the SUM out of that.
;WITH C AS(
SELECT a, b,
ROW_NUMBER() OVER (PARTITION BY a ORDER BY a) AS Rn
FROM Table1
)
SELECT SUM(b) FROM C
WHERE Rn = 1
SQL Fiddle

Aggregate two columns and rows into one

I have the following table structure
start|end
09:00|11:00
13:00|14:00
I know
SELECT ARRAY_AGG(start), ARRAY_AGG(end)
Will result in
start|end
[09:00,13:00]|[11:00,14:00]
But how can i get the following result?
result
[09:00,11:00,13:00,14:00]
BTW, I'm using Postgres
You could do array concatenation (if order is not important):
SELECT ARRAY_AGG(start) || ARRAY_AGG(end) FROM TABLE1
If order is important you could use Gordon's approach but:
add aggregate order array_agg(d order by d ASC)
use unnest instead of union all, because Gordon's solution (union all) performs two sequence scan. If table is big it could be better for performance to use:
SELECT array_agg(d ORDER BY d ASC) FROM(
SELECT unnest(ARRAY[start] || ARRAY[end]) as d from table1
) sub
which performs only one sequence scan on table (and will be faster).
One method is to unpivot them and then aggregate:
select array_agg(d)
from (select start as d from t
union all
select end as d from t
) t;
A similar method uses a cross join:
select array_agg(case when n.n = 1 then t.start else t.end end)
from t cross join
(select 1 as n union all select 2) n;
I assume the start and end are character type
select ARRAY_AGG(col)
from(select string_agg(strt::text||','||en::text,',') col
from b
)t