Hi I have Table1 and Table2 in sqlserver. I want the result like in Table3 as can be seen in the image below.
This query gave me the answer finally:
SELECT COALESECE(t1.Label, t2.label) AS label,
t1.Value AS Table1_Value,
t2.Value AS Table2_Value
FROM Table_1 t1
FULL OUTER JOIN Table_2 t2
ON t1.Label = t2.Label
Try the below Query
SELECT COALESCE(T1.LABEL,T2.LABEL) AS LABEL,T1.VALUE,T2.VALUE
FROM TAB1 T1 FULL OUTER JOIN TAB2 T2
ON T1.LABEL=T2.LABEL
Using This query:
SELECT t1.Label,
t1.Value AS Table1_Value,
t2.Value AS Table2_Value
FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.Label = t2.Label
Related
I am relatively new to SQL and I have the following question. I have the following code:
Select * from table1
LEFT JOIN table2 ON table1.name = table2.name and table1.id = table2.id
LEFT JOIN (SELECT id FROM table2 GROUP BY id) newtable ON table1.id = newtable.id
As both left joins uses data from the same table, is it possible to combine the two joins into one? How would the filters work in this case?
If your goal just to join table2 based on distinct values, then you can use WHERE and GROUP BY:
Select
*
from table1 t1
LEFT JOIN table2 t2
ON t1.name = t2.name and t1.id = t2.id
WHERE t1 id in (SELECT s2.id FROM table2 s2 GROUP BY s2.id)
I have two tables t1 and t2.
t1 having 10k records and t2 having 2k records. The 2k records of t2 is present in t1.
I wanted the 8k different records from t1 which is not present in t2.
I'm doing this as below:
select id, second_telphon from t1
except
select id, second_telphon from t2
However, I'm still getting all the 10k records. Is "except" keyword not working?
how can I achieve this?
you can perform a Join to get the unique data from the tables .
like the tables t1 & t2 both you cna perform left or right join .
example:
SELECT T1.*
FROM T1
WHERE NOT EXISTS(SELECT NULL
FROM T2
WHERE T1.ID = T2.ID
AND T1.Date = T2.Date
AND T1.Hour = T2.Hour)
OR .
SELECT T1.*
FROM T1
LEFT JOIN T2
ON T1.ID = T2.ID
AND T1.Date = T2.Date
AND T1.Hour = T2.Hour
WHERE T2.ID IS NULL
Try this:
SELECT *
FROM T1
WHERE NOT EXISTS(SELECT id,second_telphon FROM t2)
If ID is a unique value, Try this also:
SELECT *
FROM T1
WHERE ID NOT IN(SELECT ID FROM t2)
You could try a union, followed by an aggregation to restrict to those records in the first table which were not duplicated by the second table:
SELECT id, second_telphon
FROM
(
SELECT id, second_telphon FROM t1
UNION ALL
SELECT id, second_telphon FROM t2
) t
GROUP BY id, second_telphon
HAVING COUNT(*) = 1;
If a record, being defined as an id, second_telphon pair, has a record count of only one after the union, it implies that this record was unique to the first table.
Just do left join
select t1.id,t1.second_telphon from t1
left join t2 on
t1.id = t2.id
and t1.second_telphon =t2.second_telphon
where t2.id is null
I have two JOIN queries which give an output column of Id. How do I find all the Id values returned by query 1 but not returned by query 2?
select Id from Table1 join Table2;
select Id from Table2 join Table3;
SELECT id FROM dbo.Table1 INNER JOIN dbo.Table2 ON ...
EXCEPT
SELECT id FROM dbo.Table2 INNER JOIN dbo.Table3 ON ...;
try this one:
select Id from Table1 join Table2
union
select Id from Table2 join Table3
There is this SQL Statement
SELECT t1.Name
,Count(t2.SubID) Totals -- I don't know how to do it.
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
Thanks.
Guessing... change the JOIN and add GROUP BY
SELECT t1.Name
,Count(t2.SubID) AS Totals
FROM Table t1
LEFT OUTER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name
The LEFT OUTER JOIN allows you to find count zero rows per t1.Name
SELECT t1.Name, Count(t2.SubID) Totals
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.SubID
GROUP BY t1.Name
Table 1 2 columns: ID, Name
Table 2 2 columns: ID, Name
What is a query to show names from Table 1 that are not in table 2? So filtering out all the names in table 1 that are in table 2 gives the result query. Filtering is on ID not name.
Select * from table1
left join table2 on table1.id = table2.id
where table2.id is null
This should perform better than the left join...is null version. See here and here for comparisons.
select t1.id, t1.name
from table1 t1
where not exists(select null from table2 t2 where t2.id = t1.id)
Use this query
select
t1.*
from table1 t1
left outer join table2 t2
on t1.id=t2.id
where t2.id is null
this works by joining everything in t1 to whatever exists in t2. the where clause filters out all of the records that don't exist in t2.
SELECT Table1.ID, Table1.Name, Table2.ID
FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID
WHERE Table2.ID IS NULL
I think that should do it.
Try like this:
select t1.*
from table1 as t1
where t1.id not in
(select distinct t2.id from table2 as t2);