Join SQL query results inside stored procedure - sql

I need to join result sets inside a stored procedure, I tried this query but it is incorrect.
SELECT * FROM (SELECT Field1, Field2 FROM Table1 WHERE Field4='val1') A
INNER JOIN
SELECT * FROM (SELECT Field1, Field3 FROM Table1 WHERE Field4='val2') b
ON A.Field1 = B. Fileld1
How to join resulting tables inside a stored procedure?

try this:
you don't have to use SELECT * FROM twice..
SELECT * FROM
(SELECT Field1, Field2 FROM Table1 WHERE Field4='val1') A
INNER JOIN
(SELECT Field1, Field3 FROM Table1 WHERE Field4='val2') b
ON A.Field1 = B. Fileld1

You are doing some this like this (Just for an example):
SELECT * FROM Table1 A
INNER JOIN
SELECT * FROM Table2 B
ON A.FIELD1 = B.FIELD2
Which is wrong. Because you don't need to write SELECT * FROM twice. Correct one is:
SELECT * FROM Table1 A
INNER JOIN
Table2 B
ON A.FIELD1 = B.FIELD2
So your query should be(Including Create procedure):
CREATE PROCEDURE testJoining
AS
SELECT * FROM
(SELECT FIELD1, Field2 FROM Table1 WHERE Field4='VAL1') A
INNER JOIN
(SELECT FIELD1, Field3 FROM Table1 WHERE Field4='VAL2') B
ON A.FIELD1 = B.FIELD1
GO
See this fiddle.
Other way to select data from the same table is:
SELECT A.Field1,A.Field2,B.Field3 FROM Table1 A
INNER JOIN
Table1 B
ON A.FIELD1 = B.FIELD1
WHERE A.Field4='VAL1'
AND B.Field4='VAL2'
See this fiddle

Maybe this would help
With cte_sample AS ( SELECT Field1, Field2 FROM Table1
WHERE Field4='val1' )
SELECT Field1, Field3 FROM Table1 AS a
INNER JOIN cte_sample ON a.Field1 = cte_sample.Field1 AND
WHERE a.Field4='val2'

Related

mongodb equivalent to sql WITH clause?

I want to query something with SQL's WITH clause, query:
with t1 as(
select field1, field2, calculatedField
from table1
),
t2 as (
select field1, field2, calculatedField
from table 2
)
select a.*, b.*
from t1 as a
left join t2 as b on a.field1 = b.field1
how can I achieve this in mongodb? i can't find an operator in mongodb for WITH

SQL - Use different Select Statements based on column value

For instance,
Select field1
From table1
when table1.field1 = 'S'
then (select field1,2,3,4,5,6,.....
form table1,2,3,4,5,6,....(with joins))
when table1.field1 = 'O'
then (select field1,2,3,4,5,6,.....
from table1,2,3,4,5,6,.....(with join))
I think I got what you need. One possible solution is to create a view with hardcoded where clauses on it. This is the idea:
CREATE VIEW ConditionalSelect AS
SELECT ...fields...
FROM ...tables...
JOIN ...joins...
WHERE table1.field1 = 'S'
AND ....
UNION
SELECT ...fields...
FROM ...tables...
JOIN ...joins...
WHERE table1.field1 = 'O'
AND ....
Then you can do this:
SELECT *
FROM ConditionalSelect
WHERE field1 = 'S'
NOTE: Both SELECT MUST have the same columns, columns types and column names, either the VIEW won't compile.
You could just do something like this:
DECLARE #Field1 VARCHAR(10) =
(
SELECT Field1
FROM table1
);
IF(#Field1 = 'S')
BEGIN
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t2.col1 = t1.col1
END
ELSE IF (#Field1 = 'O')
BEGIN
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t2.col1 = t1.col1
END
Saves you from creating a view

How to use a field name as an argument for a nested select statement

I'm trying to do something wonky, but I cant think of any other way to do it:
SELECT
my_table.field1 as field1,
(EXISTS (SELECT 1 FROM another_table WHERE id = field1)) as does_exist
FROM my_table
This obviously fails because field1 doesn't exist at the time the result set is created. Does anyone know how to accomplish this?
You can use left join:
select distinct on (t1.field1) t1.field1, t2.id is not null as does_exist
from my_table t1
left join another_table t2 on t2.id = t1.field1
however your query should work as well:
SELECT
my_table.field1 as field1,
(EXISTS (SELECT 1 FROM another_table WHERE id = my_table.field1)) as does_exist
FROM my_table
SELECT
my_table.field1 as field1,
case another_table.ID
when null then 0
else 1
end does_exist
FROM my_table
left outer join another_table on another_table.ID = my_table.field1
Here 1 is Exists, and 0 not exists

replicate table 1 and combine a value from table2

I have table1 like this:
and other column in table2 having two unique value 3 and 4.
How do I write a query to make a table like
Actually My table is more complicated than the one above:
I tried :
"Select DISTINCT table1.x, table1.y ,table2.z from table1 Where table1.a = "something'
and table1.b is Not Null,
CROSS JOIN table2 Where table2.z='something' OR table2.z='something2' "
it does not work...I am using a modeling package which use the sql language and I am not sure it is fully compatible with sql.
You are looking to find the Cartesian product of two tables. It can be short as:
select * from table1, table2;
Or explicitly using a cross join as:
select table1.*, table2.*
from table1 cross join table2;
To filter each table individually, you can use sub-queries:
select distinct table1.x, table1.y, table2.z
from (select * from table1
where table1.a = 'something' and table1.b is not NULL
) as table1
cross join (select * from table2
where table2.z = 'something' or table2.z = 'something2'
) as table2;
Or use combine the logic under one query:
select distinct table1.x, table1.y, table2.z
from table1, table2
where table1.a = 'something' and table1.b is not NULL
and (table2.z = 'something' or table2.z = 'something2')

query to select from a table which is a value from another table

Is it possible to write a query as below? The 'column4' in inner query is a table name, and I want to use it in outer query
select x.column1, x.column2, x.column3, d.column5
from (select
a.column1, a.column2, a.column3, b.column4
from
table1 a inner join table2 b on a.priKeyCol = b.prikeyCol
)x
inner join column4 d on x.column2 = d.priColKey
What you can do is to create a view that sits on all the tables you could possibly join.
create view UnifiedTables
as
select *,'Table1' as TableName from table1
union all
select *,'Table2' as TableName from table2
union all
select *,'Table3' as TableName from table3
And then you can join this UnifiedTables in your main query.
select t1.c1 t1c1, t1.c2 t1c2, t1.c3 t1c3,
ut.c1 utc1, ut.c2 utc2, ut.c3 utc3
from table1 t1
join UnifiedTables ut on t1.c4 = ut.TableName
But this is a hack which probably won't solve your problem at a larger scale.