Oracle - Joining a basic join query with a count/group by having query - oracle

I have two queries. One basic join query and one query that uses a count/having/group by. The query that uses count is using a table also used in the basic join so I figured I could do either add another join or some sort of sub query.
What I want to do is add one or more columns from another table to query 2.
Query 1
SELECT t1.col1,
, t2.col12
FROM Table1
inner join Table2 t2
on t1.ID_NO = t2.ID_NO
Query 2
SELECT t2.col1||t2.col2, count(distinct t2.col3) Totals
FROM Table2 t2 having count(distinct t2.col3) >=15 GROUP BY t2.col1, t2.col2
Name
Account
Totals
t1.col1
t2.col1 & t2.col2
count(distinct t2.col3)

You have not described what output you are expecting from your query but there are many routes you could take to join the tables:
Join to a sub-query:
SELECT t1.col1,
t2.col1 || t2.col2 AS col12,
t2.max_id_no,
t2.totals
FROM Table1 t1
INNER JOIN (
SELECT col1,
col2,
MAX(id_no) AS max_id_no,
COUNT(DISTINCT col3) AS Totals
FROM Table2
GROUP BY col1, col2
HAVING COUNT(DISTINCT col3) >=15
) t2
ON t1.id_no = t2.max_id_no
Or, join and then group:
SELECT t2.col1 || t2.col2 AS col12,
MAX(t1.col1) AS max_t1_col1,
COUNT(DISTINCT t2.col3) AS Totals
FROM Table1 t1
INNER JOIN Table2 t2
ON (t1.ID_NO = t2.ID_NO)
GROUP BY t2.col1, t2.col2
HAVING COUNT(DISTINCT t2.col3) >=15

Related

Oracle Hierarchical queries: Translate START WITH ... CONNECT BY PRIOR into 'Recursive Subquery Factoring'

How would the following START WITH / CONNECT BY hierarchical query look like when translated into a RECURSIVE SUBQUERY FACTORING hierarchical query with WITH clause:
SELECT t1.id
FROM table1 t1, table2 t2
WHERE t1.version_id = t2.id
AND t1.baseline_date = TRIM (TO_DATE ('2015-05-26', 'yyyy-mm-dd'))
AND t2.entry_date = t1.baseline_date
START WITH t1.id IN (SELECT id
FROM table1
WHERE parent_id = 101015)
CONNECT BY PRIOR t1.id = t1.parent_id
ORDER SIBLINGS BY t1.child_index;
I think you want:
WITH rsqfc (id, child_index, baseline_date) AS (
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM table1 t1
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
WHERE t1.parent_id = 101015
UNION ALL
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM rsqfc r
INNER JOIN table1 t1
ON (r.id = t1.parent_id)
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
)
SEARCH DEPTH FIRST BY child_index SET order_id
SELECT id
FROM rsqfc
WHERE baseline_date = DATE '2015-05-26';
However, without sample data it is difficult to be sure.

Oracle: Convert rows into columns for output of joined table queries

SELECT ABC.ID, ABC.URL1, ABC.URL2,
DECODE(ABC.TEXT,'URL3' ,ABC.URL),
DECODE(ABC.TEXT,'URL4',ABC.URL),
DECODE(ABC.TEXT,'URL5',ABC.URL),
DECODE(ABC.TEXT,'URL6',ABC.URL),
DECODE(ABC.TEXT,'URL7',ABC.URL),
DECODE(ABC.TEXT,'URL8',ABC.URL),
DECODE(ABC.TEXT,'URL9',ABC.URL)
FROM (SELECT * FROM (SELECT t1.ID, t2.URL1, t2.URL2,
t4.TEXT AS TEXT, t3.URL AS URL
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id=t2.id2
LEFT JOIN table4 t3 ON t3.id=t2.id2
LEFT JOIN table4 t4 ON t3.id3=t4.id
WHERE t1.id='VALUE'))ABC;
I want the output as below:
and values for those urls in respective columnst1
Below code worked for me:
SELECT DEF.ID, DEF.URL1, DEF.URL2,
MAX(DECODE(ABC.TEXT,'URL3' ,ABC.URL)) URL3,
MAX(DECODE(ABC.TEXT,'URL4',ABC.URL)) URL4,
MAX(DECODE(ABC.TEXT,'URL5',ABC.URL)) URL5,
MAX(DECODE(ABC.TEXT,'URL6',ABC.URL)) URL6,
MAX(DECODE(ABC.TEXT,'URL7',ABC.URL)) URL7,
MAX(DECODE(ABC.TEXT,'URL8',ABC.URL)) URL8,
MAX(DECODE(ABC.TEXT,'URL9',ABC.URL)) URL9
FROM (SELECT * FROM (SELECT t1.ID, t2.URL1, t2.URL2,
t4.TEXT AS TEXT, t3.URL AS URL
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id=t2.id2
LEFT JOIN table4 t3 ON t3.id=t2.id2
LEFT JOIN table4 t4 ON t3.id3=t4.id
WHERE t1.id='VALUE')ABC) DEF
GROUP BY DEF.ID, DEF.URL1, DEF.URL2;

Union and With Clause SubQuery Oracle

I need immediate help in solving my problem before using in production:
Am using below SQL in Oracle11g
With sub_query1 as (select t1.column1, t2.column2, t1.id from table1 t1
inner join table2 t2 on t1.id = t2.id )
SELECT t3.column3 s1.* FROM sub_query1 s1
INNER JOIN table3 t3 ON t3.id=s1.id
union
SELECT t4.column4 s1.* FROM sub_query1 s1
INNER JOIN table4 t4 ON t4.id=s1.id;
This was working when I was using as is, but when add one subquery before SUB_QUERY1 it is not working, it is just returning 0 rows without any error:
With
main_sub_query as (select ta.id from tableA ta)
--second subquery
, sub_query1 as (select t1.column1, t2.column2, t1.id from table1 t1
inner join table2 t2 on t1.id = t2.id
inner join main_sub_query mq ON mq.id=t1.id
)
SELECT t3.column3 s1.* FROM sub_query1 s1
INNER JOIN table3 t3 ON t3.id=s1.id
union
SELECT t4.column4 s1.* FROM sub_query1 s1
INNER JOIN table4 t4 ON t4.id=s1.id;
When I remove the union and later part it is returning data, not sure what is the issue.

Fetching other rows of table when performing inner join over multiple fields (oracle query)

select pmt.col1,table2.col1,table2.col3,table3.col1,table3.col1
from table2 inner join (select distinct
col1,col2 from table1) pmt on
table2.col1=pmt.col1 inner join table3 on
table3.col1=table1.col2 where table2.col2 is null;
Is there any way I can select pmt.col3(which is other column of table1) in this very query only.
Thanks very much
Simply select the column in a the sub query. Use for instance max for limiting the result set to one record:
select pmt.col1,
(select max(col3)
from table1 t1
where t1.col1 = pmt.col1
and t1.col2 = pmt.col2) col3,
table2.col1,
table2.col3,
table3.col1,
table3.col1
from table2
inner join (select distinct col1,col2
from table1) pmt
on table2.col1=pmt.col1
inner join table3
on table3.col1=table1.col2
where table2.col2 is null;

2x COUNT in HAVING clause

I have following problem:
I want to count data in one table, count data in second table and compare countings in having clause, and display only that rows which have the same countings
Something like that:
SELECT bla
FROM T1 t1 JOIN T2 t2
ON t1.id = t2.id
HAVING COUNT(counted data from table1) = COUNT(counted data from table2)
Do you have any idea?
Cheers
Standard SQL:
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) AS t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) AS t2
ON t1.id = t2.id AND t1.counter = t2.counter
Oracle SQL (because Oracle doesn't like AS before table aliases):
SELECT t1.bla, t1.id, t1.counter, t2.counter
FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
FROM t1
GROUP BY t1.bla, t1.id
) t1
JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
FROM t2
GROUP BY t2.id
) t2
ON t1.id = t2.id AND t1.counter = t2.counter
You just have to decide where bla comes from; I nominated t1. I'm assuming that for any given value of t1.id, there is a single value of t1.bla. If there isn't, then you need to explain much more clearly what you're counting and where the various columns are, and what the keys of the tables are.
Update: Apologies for not noticing the Oracle tag and giving invalid Oracle syntax.
WITH jezyki as
(SELECT pseudo_wampira, COUNT(*) AS counter
FROM Jezyki_obce_w
GROUP BY pseudo_wampira
)
,sprawnosc as
(SELECT pseudo_wampira, sprawnosc, COUNT(*) AS counter
FROM Sprawnosci_w
GROUP BY pseudo_wampira, sprawnosc
)
SELECT jezyki.pseudo_wampira, sprawnosc.counter
FROM jezyki,sprawnosc
WHERE jezyki.pseudo_wampira = sprawnosc.pseudo_wampira
AND jezyki.counter = sprawnosc.counter

Resources