JOIN 3 QUERIES as a single table/view - sql

How can I JOIN these 3 queries in to a single view
SELECT A,B,C FROM TABLE1
SELECT D,E,F FROM TABLE 2 WHERE G = 'TOM'
SELECT H,I,J FROM TABLE 2 WHERE G = 'HARRY'
OUTPUT TABLE/VIEW:
A,B,C,D,E,F,H,I,J

You can use cross join:
SELECT *
FROM (SELECT A,B,C FROM TABLE1) t1 CROSS JOIN
(SELECT D,E,F FROM TABLE 2 WHERE G = 'TOM') tom CROSS JOIN
(SELECT H,I,J FROM TABLE 2 WHERE G = 'HARRY') harry

This is what worked for me.
SELECT A,B,C from T1
LEFT JOIN
(SELECT D,E,F FROM T2 WHERE G ='TOM') ON T1.A = T2.D
LEFT JOIN
(SELECT H,I,J FROM T2 WHERE G = 'HARRY') as T3 ON T1.A = T3.H

Related

how to union 3 table and group same id amount?

how to solve 3 table and group same id?
t1
----------
Id a
1 100
1 600
2 800
t2
----------
Id b
1 600
2 700
3 400
t3
----------
Id c
2 400
3 800
4 100
i want result like this:
Id a b c
------------------------------
1 700 600
2 800 700 400
3 400 800
4 100
Same id group by
do the fact you have id in several table youn should get, eg: using union ,all the id you need for join
select t.id, t1.a, t2.b, t3.c
from (
select id
from t1
union
select id
from t2
union
select id
from t3 ) AS t
left join t1 on t.id = t1.id
left join t2 on t.Id = t2.Id
left join t3 on t.Id = t3.Id
and if you need sum for a,b,c
select t.id, sum(t1.a), sum(t2.b), sum(t3.c)
from (
select id
from t1
union
select id
from t2
union
select id
from t3 ) AS t
left join t1 on t.id = t1.id
left join t2 on t.Id = t2.Id
left join t3 on t.Id = t3.Id
group by t.id
To ensure that you are taking all possible values use full outer join. Though this will not work in mySQL. If that is the case then look at this answer
select coalesce(t1.id,t2.id,t3.id) as id, sum(t1.a) as a, sum(t2.b) as b,sum(t3.c) as c
from t1
outer join t2
on t1.id = t2.id
outer join t3
on t1.id = t3.id
or t2.id = t3.id
group by id
Might be misunderstanding you, but looks like you just need to join the table more than doing a Union Operation on them. Below statement will only return records where all three tables have at least one record with the same id.
SQL would be:
SELECT TBL1.ID,
TBL1.A,
TBL2.B,
TBL3.C
FROM A TBL1 (NOLOCK)
INNER JOIN B TBL2 (NOLOCK) ON TBL1.ID = TBL2.ID
INNER JOIN C TBL3 (NOLOCK) ON TBL1.ID = TBL3.ID
Two questions:
1. Which SQL engine to you use?
2. and do you need to return values where one table does not have the id?

MS Access - alternative to performing a "full join" for columns with same name

I have this problem using access: I am using the RIGHT + LEFT outer joins to overcome the fact that ACCESS does not support the FULL JOIN.
SELECT *
FROM T1 RIGHT OUTER JOIN T2
ON T1.xxx = T2.xxx
UNION
SELECT *
FROM T1 LEFT OUTER JOIN T2
ON T1.xxx = T2.xxx
on these tables:
T1:
ID1 | xxx | fieldA
a 1 X
b 2 Y
c 3 Z
T2:
ID2 | xxx | fieldB
d 2 K
e 3 J
f 4 H
AS a result I obtain a table with this structure
T1.xxx | T2.xxx | fieldA | fieldB | ID1 | ID2
1 X a
2 2 Y K b d
3 3 Z J c e
4 H f
xxx is not primary key but has the same name and numerical type (integer)
I saw from many other places that this should work by collapsing the two tables! Here it does not (the elements on the same rows, when non blank, are of course the same)
MY EXPECTATION
FINAL TABLE:
xxx | ID1 | ID2 |fieldA | fieldB
1 a X
2 b d Y K
3 c e Z J
4 f H
It seems that there are different set of column values for both of these 2 tables, you could be having t1.xxx and t2.xxx which have the same values, but other columns dont, the union in this case wouldn't combine these 2 records
Try something like
SELECT T1.xxx
FROM T1 RIGHT OUTER JOIN T2
ON T1.xxx = T2.xxx
UNION
SELECT T2.xxx
FROM T1 LEFT OUTER JOIN T2
ON T1.xxx = T2.xxx
Something like this should give you all the xxx values from table 1 and table 2, ignoring duplicate values for xxx.
The most likely explanation for the behavior you observe is that the rows being returned that are not identical, they are not exact duplicates.
The UNION operator will remove duplicate rows, but it doesn't do anything to "collapse tables" other than that.
To get the specified result set (in the updated question), here's one SQL pattern that would return the result. (I don't know if Access supports this, but this would work in MySQL, SQL Server, Oracle, etc.)
SELECT i.xxx
, v1.ID AS ID1
, v2.ID AS ID2
, v1.fieldA AS fieldA
, v2.fieldB AS fieldB
FROM (
SELECT t1.xxx AS xxx
FROM T1 t1
UNION
SELECT t2.xxx
FROM T2 t2
) i
LEFT
JOIN T1 v1
ON v1.xxx = i.xxx
LEFT
JOIN T2 v2
ON v2.xxx = i.xxx
(NOTE: if xxx is not guaranteed to be unique in each table, then this query could generate duplicate rows.)
MY SOLUTION (absolutely not elegant):
SELECT [QUERY1].XX, ID1, ID2, fieldA, fieldB
FROM (T2 RIGHT JOIN [QUERY1] ON T2.xxx = [QUERY1].XX) LEFT JOIN T1 ON [QUERY1].XX = T1.XX;
where QUERY1 is the following:
(SELECT CInt(NZ(T1.xxx,T2.xxx)) as XX
FROM T1 RIGHT OUTER JOIN T2
ON T1.xxx = T2.xxx
UNION
SELECT CInt(NZ(T1.xxx,T2.xxx)) as XX
FROM T1 LEFT OUTER JOIN T2
ON T1.xxx = T2.xxx)
UNION (SELECT CInt(NZ(T2.xxx,T1.xxx)) as xx1
FROM T1 RIGHT OUTER JOIN T2
ON T1.xxx = T2.xxx
UNION
SELECT CInt(NZ(T2.xxx,T1.xxx)) as xx1
FROM T1 LEFT OUTER JOIN T2
ON T1.xxx = T2.xxx);
notice that xx1 is not used, and not even shown in the table
TRY THIS ONE: I have tried and get both result sets.
DECLARE #T1 TABLE
(
ID1 VARCHAR(2) ,XXX INT, FIELDA VARCHAR(2)
)
DECLARE #T2 TABLE
(
ID2 VARCHAR(2) ,XXX INT, FIELDB VARCHAR(2)
)
INSERT INTO #T1 VALUES
('a', 1, 'X'),
('b', 2, 'Y'),
('c', 3, 'Z')
INSERT INTO #T2 VALUES
('d', 2, 'k'),
('e', 3, 'j'),
('f', 4, 'h')
SELECT
ISNULL(CONVERT(VARCHAR(1),T1.xxx),' ') AS [T1.xxx] ,
ISNULL(CONVERT(VARCHAR(1),T2.xxx),'') AS [T2.xxx] ,
ISNULL(fieldA,'') AS [fieldA],
ISNULL(fieldB,'') AS [fieldB] ,
ISNULL(ID1,'') AS [ID1] ,
ISNULL(ID2,'') AS [ID2]
FROM
(
SELECT XXX as XXX1 FROM #T1
UNION
SELECT XXX as XXX1 FROM #T2
)T LEFT OUTER JOIN
#T1 T1 ON T.XXX1 = T1.XXX
LEFT OUTER JOIN
#T2 T2 ON T.XXX1 = T2.XXX
----------------------RESULT------------------
T1.xxx T2.xxx ID1 ID2 fieldA fieldB
1 a X
2 2 b d Y k
3 3 c e Z j
4 f h
SELECT
ISNULL(CONVERT(VARCHAR(1),T.xxx1),' ') AS [xxx] ,
ISNULL(ID1,'') AS [ID1] ,
ISNULL(ID2,'') AS [ID2],
ISNULL(fieldA,'') AS [fieldA],
ISNULL(fieldB,'') AS [fieldB]
FROM
(
SELECT XXX as XXX1 FROM #T1
UNION
SELECT XXX as XXX1 FROM #T2
)T LEFT OUTER JOIN
#T1 T1 ON T.XXX1 = T1.XXX
LEFT OUTER JOIN
#T2 T2 ON T.XXX1 = T2.XXX
----------------------RESULT------------------
xxx ID1 ID2 fieldA fieldB
1 a X
2 b d Y k
3 c e Z j
4 f h

Fetch records from two tables in sql server

I have two table in sql server like this .
table 1
userid value
a 1
b 1
c 1
d 1
table 2
userid value
e 0
f 0
g 0
a 0
b 0
I want to output like this from above two tables
usrid value
a 0
b 0
c 1
d 1
e 0
f 0
g 0
if any records exists in table 1, records must fetches the data from table 2, other wise table 1. if userid not exists in table 1 and fetches records from table 2 only.
Try this:
select ISNULL(t1.usrid,t2.usrid) as usrid
,ISNULL(t1.value,t2.value) as value
from table1 t1
outer join table2 t2 on t1.usrid = t2.usrid
Try this
SELECT * FROM dbo.Table_2 AS t WHERE t.uid IN (SELECT t2.uid FROM dbo.Table_1 AS t2)
UNION
SELECT * FROM dbo.Table_1 AS t WHERE t.uid NOT IN (SELECT t2.uid FROM dbo.Table_2 AS t2)
UNION
SELECT * FROM dbo.Table_2 AS t WHERE t.uid NOT IN (SELECT t2.uid FROM dbo.Table_1 AS t2)
Try this
SELECT *
FROM table1
WHERE userid NOT IN (select userid from table2)
UNION
SELECT *
FROM table2
Try this:
SELECT *
FROM table1
WHERE table1.userid NOT IN (select userid from table2)
UNION
SELECT *
FROM table2

How to trace out values from three different tables sql JOIN?

I have 3 table like below
table_1
securityno name price
1 a11 12.12
2 z11 44.4
table_2
name identifier Mainprice
a11_bond NO34 11
z22_bond NO98 22
table_3
securityno name identifier
1 a11 NO34
2 z11 NO98
I want to check whether table_1 is having correct price or not as per table_2
I just want to display output table_1 data and Mainprice column from table_2
securityno name price Mainprice
1 a11 12.12 11
2 z11 44.4 22
I was trying like
select * from table_1 left join table_2 on what about table_3?
failed to use 3 tables .
please help.
Try:
SELECT
t1.*,
t2.Mainprice
FROM table_1 AS t1
LEFT JOIN table_3 AS t3
ON t1.securityno = t3.securityno AND t1.name = t3.name
INNER JOIN table_2 AS t2
ON t2.identifier = t3.identifier
Simple use INNER JOIN
SELECT T1.*,
T2.mainprice
FROM TABLE1 T1
INNER JOIN Table3 T3 ON T3.securityno = T1.securityno
INNER JOIN Table2 T2 ON T2.identifier = T3.identifier
DEMO

taking difference of tables

I have following tables;
A B A B
_____ _____
1 t 7 a
2 r 5 d
3 e 3 e
4 f
5 d
6 s
7 a
And, output should be ;
A B
_____
1 t
2 r
4 f
6 s
In other words I want difference of these two tables . I want region A in this figure.
How can I do that ?
Try this:
SELECT t1.*
FROM t1
LEFT JOIN t2 USING (A, B)
WHERE t2.A IS NULL
without using JOIN
SELECT A, B
FROM tableA
WHERE A NOT IN
(SELECT Distinct A FROM tableB)
This should do the trick:
SELECT
A,
B
FROM Table_1
WHERE NOT EXISTS
(
SELECT Table_2.A AS Test
FROM Table_2
INNER JOIN Table_1 AS T ON Table_2.A = Table_1.A
)
SELECT A, B
FROM Table1
EXCEPT
SELECT A, B
FROM Table2;