Table 1 Table 2
Number | Code Code | Description
1234 A A Something
1235 B C Something else
1246 C D Something other
1247 A
1248 B
1249 A
I would like to find the distinct Code values and get a return like this:
1 | 2
-------
A A
B
C C
D
I can't figure out how to write a SQL query that would return me the above results. Anyone have any experience with a query like this or similar?
In proper RDBMS:
SELECT
T1.Code, T2.Code
FROM
(SELECT DISTINCT Code FROM Table1) T1
FULL OUTER JOIN
(SELECT DISTINCT Code FROM Table2) T2
ON T1.Code = T2.Code
In MySQL... the UNION removes duplicates
SELECT
T1.Code, T2.Code
FROM
Table1 T1
LEFT OUTER JOIN
Table2 T2 ON T1.Code = T2.Code
UNION
SELECT
T1.Code, T2.Code
FROM
Table1 T1
RIGHT OUTER JOIN
Table2 T2 ON T1.Code = T2.Code
In Standard SQL, using relational operators and avoiding nulls:
SELECT Code AS col_1, Code AS col_2
FROM Table_1
INTERSECT
SELECT Code AS col_1, Code AS col_2
FROM Table_2
UNION
SELECT Code AS col_1, 'missing' AS col_2
FROM Table_1
EXCEPT
SELECT Code AS col_1, 'missing' AS col_2
FROM Table_2
UNION
SELECT 'missing' AS col_1, Code AS col_2
FROM Table_2
EXCEPT
SELECT 'missing' AS col_1, Code AS col_2
FROM Table_1;
Again in Standard SQL, this time using constructs that MySQL actually supports:
SELECT Code AS col_1, Code AS col_2
FROM Table_1
WHERE EXISTS (
SELECT *
FROM Table_2
WHERE Table_2.Code = Table_1.Code
)
UNION
SELECT Code AS col_1, 'missing' AS col_2
FROM Table_1
WHERE NOT EXISTS (
SELECT *
FROM Table_2
WHERE Table_2.Code = Table_1.Code
)
UNION
SELECT 'missing' AS col_1, Code AS col_2
FROM Table_2
WHERE NOT EXISTS (
SELECT *
FROM Table_1
WHERE Table_1.Code = Table_2.Code
);
What you're looking for is a full outer join:
select a.code as code_1,b.code as code_2
from(
select code
from table1
group by 1
)a
full outer join(
select code
from table2
group by 1
)b
using(code)
order by 1;
This actually looks like a UNION of two outer joins. Try this:
SELECT t1.Code, t2.Code
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 ON t1.Code
UNION
SELECT t1.Code, t2.Code
FROM Table1 AS t1
RIGHT JOIN Table2 AS t2 ON t1.Code
ORDER BY 1, 2
The UNION operation will only keep distinct values.
The trick would be to get the distinct values from both tables, something like this:
SELECT a.Code, b.code
FROM
( --Get the DISTICT Codes from all sets
SELECT Distinct Code from Table1
UNION SELECT Distinct Code from Table2
) x Left JOIN
Table1 a ON x.code = a.Code LEFT JOIN
Table2 b ON x.code = b.Code
SELECT
ct.ct_id,
ct.pd_id,
ct.ct_qty,
pd.product_name,
pd.price,
src.service_id,
src.service_name,
src.service_charge,
src.service_quantity
FROM
cart ct,
product pd,
service src
WHERE ct_session_id = '$sid'
LIMIT 1
Related
I have two table which I would like to union. I need to keep only the duplicates from one of the two tables. I tried to find it, but could not find it anywhere. Hope somebody can help.
For example:
Table_1:
ID
Product
Amount
1
A
10
2
B
10
3
C
10
Table_2:
ID
Product
Amount
3
C
9
4
A
100
5
B
100
Desired result:
ID
Product
Amount
1
A
10
2
B
10
3
C
9
4
A
100
5
B
100
So always use the duplicates from table_2. In this example ID 3 is duplicate, so use the duplicate of table_2 with amount 9.
How to realize this with T-SQL? I used the code below:
Select * from Table_1 where Table_1.id != Table_2.id
Union All
Select * from Table_2
But then I receive the error:
'The multi-part identifier "Table_2.ID" could not be bound.'
Use not exists:
Select t1.*
from Table_1 t1
where not exists (select 1 from table_2 t2 where t2.id = t1.id)
Union All
Select t2.*
from Table_2 t2;
Try this:
SELECT T1.*
FROM #Table1 T1
WHERE T1.ID NOT IN (SELECT ID FROM #Table2)
UNION
SELECT T2.*
FROM #Table2 T2
I assume what you want is an EXISTS:
SELECT T1.ID,
T1.Product,
T1.Amount
FROM dbo.Table1 T1
WHERE NOT EXISTS (SELECT 1
FROM dbo.Table2 T2
WHERE T1.ID = T2.ID)
UNION ALL
SELECT T2.ID,
T2.Product,
T2.Amount
FROM dbo.Table2 T2;
A FULL OUTER JOIN, however, might also work if ID is unique in both tables:
SELECT ISNULL(T2.ID,T1.ID) AS ID,
ISNULL(T2.Product,T1.Product) AS Product,
ISNULL(T2.Amount,T1.Amount) AS Amount
FROM dbo.Table1 T1
FULL OUTER JOIN dbo.Table2 T2 ON T1.ID = T2.ID;
Union will give you the result. Union will always return unique values always. If you use union all you will get all with duplicates. Your answer would be to use union all.
SELECT
B.ID
,B.Product
,B.Amount
FROM
(
SELECT
A.ID
,A.Product
,A.Amount
,ROW_NUMBER() over (Partition BY ID, Product order by Amount ASC) AS [row_num]
FROM
(
SELECT
tb_1.*
FROM tb_1
UNION ALL
SELECT
tb_2.*
FROM tb_2
) AS A
) AS B
WHERE B.[row_num] = 1
I have two tables namely, Table1 and Table2
Table1
custId--custName--custAge
c1--c1name--32
c2--c2name--41
c3--c3name--41
Table2
custId--verified--custName
c1--Y--c1FullName
c2--N--c2FullName
I need to join Table1 and Table2, So that if verified column is Y in table 2, I need custName from Table2 instead of Table1.
So, desired output is: (overwrite custName column from Table2 if verified column is Y for that custId)
custId--custName--custAge
c1--c1FullName--32
c2--c2name--41
c3--c3name--41
I wrote following query, which is not giving proper result. Please help.
select T1.custId, NVL(T2.custName, T1.custName),T1.custAge
from Table1 T1
left join Table2 T2 on T1.custId=T2.custId and T2.verified='Y'
You can use a CASE statement to achieve this :
SELECT tab1.custId,
CASE
WHEN (tab2.verified = 'Y')
THEN tab2.custName
ELSE tab1.custName
END AS CustName,
tab1.custAge
FROM Table1 tab1
LEFT JOIN Table2 tab2 ON tab1.custId = tab2.custId
See this here -> http://rextester.com/EVOMK25746 (This fiddle is built on SQL Server , however, the query should work on Oracle databases as well)
Hope this helps!!!
Try this query once..
select * into #tab1 from
(
select 'c1' custId,'c1name' custName,31 custAge
union all
select 'c2' ,'c2name' ,41
union all
select 'c3' ,'c3name' ,41
) as a
select * into #tab2 from
(
select 'c1' custId,'Y'verified,'c1FullName' custName
UNION ALL
SELECT 'c2','N','c2FullName '
) as a
SELECT T1.custId,CASE WHEN T2.verified='Y' THEN T2.custName ELSE T1.custName END AS CUSTNAME,T1.custAge FROM #tab1 T1
LEFT JOIN #tab2 T2 ON T1.custId=T2.custId
select custId, custName, custAge
from Table1
natural join
( select custId
from Table2
where verified='N' ) t2
union
select custId, custName, custAge
from ( select custId, custAge from Table1 ) t1
natural join
( select custId, custName
from Table2
where verified='Y' ) t2;
Table_1 Table_2
name=john name=john
age=15 age=18
My goal is to select john, because it is the same name.
Now my idea is:
SELECT * FROM Table_1,Table_2 WHERE (name=name).
You can use an inner join to merge the two tables where the key you specified are same like:
Select t1.name, t1.age as AgeFromT1, t2.age as AgeFromT2
from Table_1 t1
inner join Table_2 t2 on t1.name = t2.name
Or you can use a subquery like:
Select table_1.*
from table_1
where name in (
Select Table_2.name
from Table_2
)
Suppose, we have query like this:
SELECT
1
FROM DUAL WHERE
(SELECT id FROM table_1 t1 WHERE /*conditions*/)
IN
(SELECT id FROM table_1 t2 WHERE /*conditions*/)
I want to check if first query
SELECT id FROM table_1 t1 WHERE /*conditions*/
returns the same ids like the second query.
Of course this query (IN statement) doesn't work.
Try:
SELECT id FROM table_1 t1 WHERE /*conditions1*/ and id not in (SELECT id FROM table_1 t2 WHERE /*conditions2*/)
union
SELECT id FROM table_1 t1 WHERE /*conditions2*/ and id not in (SELECT id FROM table_1 t2 WHERE /*conditions1*/)
If both queries gives you the same id's the result should be empty.
This will return nothing if sets are equal:
SELECT id FROM table_1 t1 WHERE /*conditions*/
EXCEPT
SELECT id FROM table_1 t2 WHERE /*conditions*/
You can use EXCEPT.
EXCEPT returns distinct rows from the left input query that aren’t
output by the right input query.
EXCEPT sample in your case:
SELECT id
FROM table_1 AS t1
WHERE /*conditions*/
EXCEPT
SELECT id
FROM table_1 AS t2
WHERE /*conditions*/
Just as an alternative method that used Full Join in tsql:
SELECT CASE WHEN isnull(Count(*), 0) > 1 then 1 else 0 end as result
FROM (SELECT t1.id as t1_id, t2.id as t2_id FROM
(SELECT id FROM table1 WHERE /*conditions*/) As t1
Full Outer Join
(SELECT id FROM table2 WHERE /*conditions*/) As t2
On t1.id = t2.id
) As ft
WHERE ft.t1_id is null or ft.t2_id is null
And I think this can marked as a stupid way.
I have a tables Table1 and Table2 like this:
Table1
Col1
====
A
B
Table2
Col1
====
A
B
C
D
Now using an INNER JOIN I need to find unmatched records from Table2
(Note: WHERE clause is NOT ALLOWED)
Expected output:
Col1
====
C
D
What can be the SQL Query for this?
I have already tried following its not working.
Select Distinct
Table2.col1
from
Table1
Inner Join
Table2 On Table1.col1 <> Table2.col1
Jumped the gun the first time - should have read properly, sorry. Had to think about this one:
SELECT Table2.col1
FROM Table1 RIGHT JOIN Table2
ON Table1.col1 = Table2.col1
GROUP BY Table1.col1, Table2.col1
HAVING Table1.col1 IS NULL;
You can use the following query
SELECT * FROM Table2
WHERE Col1 NOT IN
(SELECT Col1 FROM Table1)
The following query also works
SELECT Col1 FROM Table2
EXCEPT
SELECT Col1 FROM Table1
nobody said it had to be sensible did they?
with
t1 as (
select col1 as c1
, row_number() over (order by col1 ASC) as rn1
from table1
),
t2 as (
select col1 as c2
, row_number() over (order by col1 DESC) as rn2
from table2
)
select
c2 as Col1
from t2
inner join t1 on t2.rn2 = t1.rn1 and t2.c2 <> t1.c1
order by c2 ASC
;
http://sqlfiddle.com/#!15/4747e/1 (postgres 9.3.1)