Refer to other SQL SELECT statements aliased as tables in FROM clause - sql

I have a very large query that follows the format below:
select ...
from ( select field1,
field2
from some_table ) table1,
( select field1,
field3
from other_table ) table2
where .....
Is is possible for me to refer to one of the tables "defined" in the from clause, lets say table1, in one of the other table definitions in the from clause?
For example:
select ....
from ( select field1,
field2
from some_table ) table1,
( select table1.field1,
field3
from other_table,
table1 ) table2
where .....
Disclaimer: What I am trying to do is not as simple as the example above. The example is simply to illustrate the idea.

WITH
table1 AS
(
SELECT field1, field2
FROM some_table
),
table2 AS
(
SELECT field1, field2
FROM other_table, table1
)
SELECT *
FROM table2

If you are using SQL 2005, you can use Common Table Expressions for doing what you are trying; Quassnoi gives us an example, in Oracle I don't know how to achieve it though

Related

How to create a SELECT query FROM "TABLE1 AND TABLE2"

I have a PostgreSQL database, with only SELECT permissions. In this DB there are two tables with the same structure (the same columns).
I need to write several query in each table and join the results.
There is a way for writing a query like this one?
SELECT
field1,
field2,
field3
FROM
table1
AND
table2
WHERE
condition;
Select from 2 tables. Query = table1 OR table1 + table2 have no answer and it is not my question.
UNION ALL
SELECT field1, field2, field3
FROM table1
WHERE condition
UNION ALL
SELECT field1, field2, field3
FROM table2
WHERE condition;
Or to simplify your WHERE condition
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;
You can use Union:
SELECT
field1,
field2,
field3
FROM
table1
UNION
SELECT
field1,
field2,
field3
FROM
table2
SELECT * FROM
( SELECT field1, field2, field3
FROM table1
UNION ALL
SELECT field1, field2, field3
FROM table2
)
WHERE condition;

How to use LIKE statement with multiple values from another field?

I want to select all records where field contains values from another field. How do I do that? Here is a code that I am trying.
select field1 , field2, field3
from table1
where field1 like '%'+(select distinct field4 from table2)+'%'
Thanks in advance.
Just do your like as a join condition:
select field1 , field2, field3
from table1
join (select distinct field4 from table2) x
on field1 like '%'+field4+'%'
Using the original structure of your query, you can do:
select field1, field2, field3
from table1 t1
where exists (select 1
from table2
where t1.field1 like '%' + field4 + '%'
);
The advantage of this method is that it will not duplicate records in table1. For instance, if there are two rows in table2 with the values 'a' and 'b' respectively and one row in table1 with the value 'ab', then this method will only return the row from table1 once.

select distinct value in sql with 35 columns

I have table that contains 35 columns, how would I select only the distinct records from that table, this is what my query looks like:
`SELECT field1, field2, field3 etc... from table1 group by field1, field2, field3 etc...`
This gets me the unique results that I want but I have 35 columns, its too long to group all 35 rows - is there any efficient way of doing this:
by doing this, I get repeated results:
SELECT distinct * from table1
DISTINCT will be also faster:
Your query should looks like:
SELECT distinct field1, field2, field3 etc...
from table1
Use distinct once.
This will affect all columns.
You don't need GROUP BY if you use DISTINCT. But you have to list all the fields that you want to show, and for sure not the primary key.
SELECT DISTINCT field1, field2, field3 etc...
FROM table1

ORACLE Select and group by excluding one field

I have a very simple query (on Oracle 11g) to select 3 fields:
select field1, field2, field3, count(*) from table
where...
group by field1, field2, field3
having count(*) > 10;
Now, what I need, is exclude "field3" from the "group by" since I only need field 1 and 2 to be grouped, but I also need field3 in the output.
As far I know, all the fields in the select must be reported also in "group by", so how can I handle that?
Thanks
Lucas
select t.field1, t.field2, t.field3, tc.Count
from table t
inner join (
select field1, field2, count(*) as Count
from table
where...
group by field1, field2
having count(*) > 10
) tc on t.field1 = tc.field1 and t.field2 = tc.field2
Use the analytical version of the "count" function:
select * from (
select field1, field2, field3, count(*) over(partition by field1, field2) mycounter
from table )
--simulate the having clause
where mycounter > 10;
If you don't group by field3 anymore, there can suddenly be different field3 per group. You must decide which one to show, e.g. the maximum:
select field1, field2, max(field3), count(*) from table
where...
group by field1, field2
having count(*) > 10;
The only way I know how to handle that is to first isolate the Field1 and Field2 data and create a new table, then link it back to the original table adding in Field3.
Select Table2.Field1, Table2.Field2, Table1.Field3
From
(Select Field1, max(Field2) as Field2
From Table1) Table2
Where Table2.Field1 = Table1.Field1
And Table2.Field2 = Table1.Field2
Group By
Table2.Field1, Table2.Field2, Table1.Field3

query multiple tables and combine results into one return table for stored procedure?

I have several different tables, but they all have 2 columns that are the same name. I want to write a stored procedure that searches one column in all of the tables and returns the result. Ideas? I'm fairly nub when it comes to SQL.
The operation you are looking for is UNION or UNION ALL.
SELECT * FROM (
SELECT col1, col2 FROM table1
UNION ALL
SELECT col1, col2 FROM table2
UNION ALL
SELECT col1, col2 FROM table3
) all_tables
WHERE all_tables.col1 = 'something'
If you use UNION rather than UNION ALL, the database will eliminate duplicate rows that may be in multiple tables. If you know there won't be any duplicates, use UNION ALL since it is generally much faster.
Select myColumn FROM Table1
UNION Select myColumn FROM Table2
UNION Select myColumn FROM Table3
..etc
-- Note all column names have to be the same and have to be in each table for this to work
You wouldn't even need a stored procedure...just use a union query.
select field1, field2 from table1 where table1.field1=criteria
union
select field1, field2 from table2 where table2.field1=criteria
union
select field1, field2 from table3 where table3.field1=criteria
etc...