Tables structure are below :
Table1 (ID int, value1 int,...)
ID Value1
---- --------
1 10
2 20
5 12
Table2 (ID int, value2 int,...)
ID Value2
---- --------
1 13
3 24
4 11
Table3 (ID int, value3 int,...)
ID Value3
---- --------
4 150
5 100
My expected output is below.
ID Value1 Value2 Value3
---- -------- -------- --------
1 10 13 NULL
2 20 NULL NULL
3 NULL 24 NULL
4 NULL 11 150
5 12 NULL 100
It should be noted that above tables is huge and I want to have best performance.
My query suggestion is below :
Select ID,
SUM(Value1) AS Value1,
SUM(Value2) AS Value2,
SUM(Value3) AS Value3
From (
Select ID, Value1 , NULL as value2, NULL as value 3
From Table1
Union ALL
Select ID, NULL , value2, NULL
From Table2
Union ALL
Select ID, NULL, NULL, value 3
From Table3
)Z
Group By Z.ID
Assuming you only have one value per id, this should do the trick:
SELECT aux.ID, t1.Value1, t2.Value2, t3.Value3
FROM
(SELECT ID FROM Table1
UNION
select ID FROM Table2
UNION
SELECT ID FROM Table3) aux
LEFT OUTER JOIN Table1 t1 ON aux.ID = t1.ID
LEFT OUTER JOIN Table2 t2 ON aux.ID = t2.ID
LEFT OUTER JOIN Table3 t3 ON aux.ID = t3.ID
If you've more than one value:
SELECT aux.ID, SUM(t1.Value1) as 'Value1', SUM(t2.Value2) as 'Value2', SUM(t3.Value3) as 'Value3'
FROM
(SELECT ID FROM Table1
UNION
select ID FROM Table2
UNION
SELECT ID FROM Table3) aux
LEFT OUTER JOIN Table1 t1 ON aux.ID = t1.ID
LEFT OUTER JOIN Table2 t2 ON aux.ID = t2.ID
LEFT OUTER JOIN Table3 t3 ON aux.ID = t3.ID
GROUP BY aux.ID
I intially wrote the same answer as aF. did above. So, removed it, and used a different approach.
Here,
1st query get all from table1
2nd query gets all from table2 skipping
those already present in table1 3rd query gets all remaining skipping those in above two query.
SELECT T1.ID, T1.VALUE1, T2.VALUE2, T3.VALUE3 --all T1
FROM TABLE1 T1
LEFT JOIN TABLE2 ON T1.ID=T2.ID
LEFT JOIN TABLE3 ON T1.ID=T3.ID
UNION
SELECT T2.ID, T1.VALUE1, T2.VALUE2, T3.VALUE3 --all T2 where T1 is NULL
FROM TABLE1 T2
LEFT JOIN TABLE1 ON T2.ID=T1.ID
LEFT JOIN TABLE3 ON T2.ID=T3.ID
WHERE T1.ID IS NULL
UNION
SELECT T3.ID, T1.VALUE1, T2.VALUE2, T3.VALUE3 --all T3 where T1 is NULL AND T2 IS NUL
FROM TABLE1 T3
LEFT JOIN TABLE1 ON T3.ID=T1.ID
LEFT JOIN TABLE2 ON T3.ID=T2.ID
WHERE T1.ID IS NULL
AND T2.ID IS NULL
Related
Let's say I have Two tables T1 and T2 with ID as the column. For example
T1:ID
1
2
3
4
5
T2:ID
4
5
6
7
8
How can get a column where my result is 1 2 3 6 7 8 I want to exclude the common IDs from both the table and display the result in one column.
Hmmm . . . one method is union all:
select t1.id
from table1 t1
where not exists (select 1 from table2 t2 where t2.id = t1.id)
union all
select t1.id
from table2 t2
where not exists (select 1 from table1 t1 where t2.id = t1.id);
use minus
select id from
(
select id from t1
union
select id from t2
) a
minus select id from (
select id from t1 join t2 on t1.id=t2.id
) b
If your DBMS support FULL OUTER JOIN then i would do :
select coalesce(t1.id, t2.id)
from t1 full outer join
t2
on t2.id = t1.id
where (t1.id is null or t2.id is null);
I have the following table structures:
Table1
--------------
Table1Id
Field1
Field2
Table2
------------
Table2Id
Table1Id
Field1
Field2
Table3
-----------
Table3Id
Table1Id
Field1
Field2
I need to be able to select all fields in Table1, count of records in Table2, and count of records in Table3 Where count of records in Table2 > count of records in Table3
Here is an example of expected output with the given data:
Table1 Data
-------------
1 Record1Field1 Record1Feild2
2 Record2Field1 Record2Feild2
3 Record3Field1 Record3Feild2
4 Record4Field1 Record4Feild2
Table2 Data
------------
1 1 Record1Field1 Record1Feild2
2 1 Record2Field1 Record2Feild2
3 2 Record3Field1 Record3Feild2
4 2 Record4Field1 Record4Feild2
5 2 Record5Field1 Record5Feild2
6 4 Record6Field1 Record6Feild2
7 4 Record6Field1 Record6Feild2
8 4 Record6Field1 Record6Feild2
Table3 Data
------------
1 2 Record1Field1 Record1Feild2
2 2 Record2Field1 Record2Feild2
3 3 Record3Field1 Record3Feild2
4 3 Record4Field1 Record4Feild2
5 3 Record5Field1 Record5Feild2
6 4 Record6Field1 Record6Feild2
Desired Results
Table1Id Field1 Field2 Table2Count Table3Count
1 Record1Field1 Record1Field2 2 0
2 Record2Field1 Recird2Field2 3 2
4 Record4Field1 Recird4Field2 3 1
Notice record 3 in Table 1 is not shown because the record count in Table2 is less than the record count in Table3. I was able to make this work using a very ugly query similar to the one below but feel there is a much better way to do this using joins.
SELECT
t1.Table1Id,
t1.Field1,
t1.Field2
(Select Count(Table2Id) From Table2 t2 Where t2.Table1Id = t1.Table1Id) as Table2Count,
(Select Count(Table3Id) From Table3 t3 Where t3.Table1Id = t1.Table1Id) as Table3Count,
From
Table1 t1
Where
(Select Count(Table2Id) From Table2 t2 Where t2.Table1Id = t1.Table1Id) > (Select Count(Table3Id) From Table3 t3 Where t3.Table1Id = t1.Table1Id)
Hard to test it without working examples but something along these lines should be a good starting point.
SELECT
t1.Table1Id,
t1.Field1,
t1.Field2,
COUNT(DISTINCT t2.Table2Id),
COUNT(DISTINCT t3.Table3Id)
From Table1 t1
LEFT OUTER JOIN Table2 t2 ON t1.Table1Id = t2.Table1Id
LEFT OUTER JOIN Table3 t3 ON t1.Table1Id = t3.Table1Id
GROUP BY t1.Table1Id
HAVING COUNT(DISTINCT t2.Table2Id) > COUNT(DISTINCT t3.Table3Id)
You could get all the value in t1 and the data form t2 e t3 for your comparision using a couple of join on grouped values
SELECT
t1.Table1Id
,t1.Field1
,t1.Field2
, tt2.count_t2
, tt3.count_t3
from table1 t1
join (
select Table1Id, count(*) count_t2
From Table2
group by Table1Id
) tt2 on tt2.Table1Id = t1.Table1Id
join (
select Table1Id, count(*) count_t3
From Table3
group by Table1Id
) tt3 on tt3.Table1Id = t1.Table1Id
where tt2.count_t2 < tt3.count_t3 <
I have the following tables where a few of the columns are included here:
Table 1:
Id RefId PhoneNumber
1 11 919191
2 11 888888
3 11 919191
Table 2:
Id RefId City UniqueId
1 11 Mumbai 111
2 11 Pune 222
3 11 Nashik 333
I want a few columns from Table1 and Table2. Common in both table is RefId. Table2 has UniqueId which is primary key of Table2. If I do an inner join based on RefId I will get 9 records, but I want 3. How do I get that?
Here is my query:
SELECT T1.PhoneNumber,T2.City,T2.UniqueId,T2.RefId
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.RefId = T2.RefId
If the ID is used to join as well then
SELECT t1.Id, t1.RefId, t1.PhoneNumber, t2.City, t2.UniqueId
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Id = t2.Id
AND t1.RefId = t2.RefId
If the Ids are randomly generated them you probably shouldn't join on them.
If the Ids were random but all of the PhoneNumbers were for the same place then you could just pick the Minimum value PhoneNumber (or Max)
SELECT t2.RefId, t1.PhoneNumber, t2.City, t2.UniqueId
FROM Table2 t2
INNER JOIN (
SELECT RefId, MIN(PhoneNumber) AS PhoneNumber
FROM Table1
GROUP BY RefId
) t1 ON t1.RefId = t2.RefId
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?
Please, help me with join results of commands (MS SQL):
SELECT name,value FROM table1 WHERE idfoo1 IN(SELECT _id FROM table3 where id = 1);
SELECT value FROM table2 WHERE idfoo2 IN(SELECT _id_2 FROM table3 where id = 1) AND name='fooname';
And I get:
name value
John 2
Bill 32
Alex 11
value
434
234
144
But I need join results.
name value value
John 2 434
Bill 32 234
Alex 11 144
So, id == id, _id != _id_2,
Use this query:
SELECT t1.name,
t1.value,
t2.value
FROM table1 t1
INNER JOIN table3 t3 ON t1.idfoo1 = t3._id
INNER JOIN table2 t2 ON t2.idfoo2 = t3._id_2
WHERE t3.id=1 AND t2.name = 'fooname'
Select a.name,a.value,c.value FROM table1 as a inner join table3 as b
on a.idfoo1=b.id and b.id=1 inner join table3 as c
on c.idfoo2=b._id_2 and b.id=1 and c.name='fooname'
i guess this is what you need-
SELECT t1.name, t1.value, t2.value
FROM table1 t1, table2 t2, table3 t3
WHERE
t1.idfoo1 = t3._id
AND t2.idfoo2 = t3._id_2
AND t3.id = 1
AND t2.name='fooname';