Given below is table A:
Cust_id
Code
1
101
1
102
1
103
2
201
Table B:
Cust_id
Code
1
101
1
102
Table B has only customer 1 and contains only two product codes of customer 1.
I want to identify the code of customers in Table B that is not present in Table B.
Cust_id
Code
1
103
To get this, I did Table A left join Table B on cust_id and code and thought those with null values of code in Table B would give the desired result, but it did not seem to work. It would be really helpful if someone could tell what should the correct steps be. Thanks
My query so far:
select a.cust_id, a.code, b.cust_id as customer, b.code as product
from a
left join b on a.cust_id = b.cust_id and a.code = b.code
Two conditions:
cust_id must be in table B
(cust_id, code) must not be in table B
The query with the conditions almost literally translated from human language to SQL:
select *
from a
where cust_id in (select cust_id from b)
and (cust_id, code) not in (select cust_id, code from b);
List item
Hello other solution using exists
select a.cust_id, a.code
from TABLEA a left join TABLEB b on
a.cust_id=b.cust_id and a.code=b.code
where b.cust_id is null and exists( select * from TABLEB
where cust_id=a.cust_id)
Related
I have two tables that have the same column of position and salary.
Table_a
ID
Name
Position
Salary
1
Peter
Sale
10,000
2
Tom
IT
15,000
3
Jane
Sale
12,000
4
Mary
PR
8,000
5
John
IT
14,000
Table_b
ID
Position
Salary
1
Driver
9,000
2
Manager
20,000
4
Sale
13,000
I would like to merge two tables that the output is based on the Position value and Salary value of Table_b.
Output
ID
Name
Position
Salary
1
Peter
Driver
9,000
2
Tom
Manager
20,000
3
Jane
Sale
12,000
4
Mary
Sale
13,000
5
John
IT
14,000
Please give me an advice how can I query and get the above output.
SELECT id,
t1.name,
COALESCE(t2.position, t1.position),
COALESCE(t2.salary, t1.salary)
FROM table_a t1
LEFT JOIN table_b t2 USING (id)
Some rows in table_a don't have a matching row in table_b. Any rows in table_b that don't have a matching row in table_a will be ignored. We assume that ID is unique in each table (meaning for example there won't be two rows in table_b with the same ID value). We will match the rows on ID value.
We can make table_a the driving table in an outer join to table_b. Let's first get that working, returning all the columns from both tables, and verify the return is what we expect.
SELECT a.ID
, a.Name
, a.Position
, a.Salary
, b.ID AS b_ID
, b.Position AS b_position
, b.Salary AS b_salary
FROM table_a a
LEFT
JOIN table_b b
ON b.ID = a.ID
ORDER
BY a.ID
We see that in the result from the first query, on rows where we didn't get a matching row from table_b, column b_ID (and all the columns from table_b) are NULL.
We can add some expressions in the SELECT list to get the Salary from table_b where we found a matching row, or otherwise return Salary from table_a where we didn't.
There's lots of possible expressions to do that, but they all key on the idea that when we don't have a matching row from the outer joined table, the values in those columns will be NULL.
So the trick is test whether we found a matching row from table_b (or if we did get a matching row in table_b, the row had a NULL value for Salary)
Adding some example expressions to check if b.ID or b.Salary is NULL.
SELECT a.Name
, a.Position
, a.Salary
, b.ID AS b_ID
, b.Position AS b_position
, b.Salary AS b_salary
, CASE WHEN b.ID IS NULL THEN a.Salary ELSE b.Salary END AS _Salary_x1
, IFNULL(b.Salary,a.Salary) AS _Salary_x2
, IF(b.Salary IS NULL,a.Salary,b.Salary) AS _Salary_x3
FROM table_a a
LEFT
JOIN table_b b
ON b.ID = a.ID
ORDER
BY a.ID
I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL
I have the following two tables:
Table a:
name qty
a 10
b 20
c 30
d 40
and table b
name qty
a 10
b 20
d 20
e 60.
I want to merge there two tables and create a new table like this
name qty
a 20
b 40
c 30
d 60
e 60
The objective is to add the values if there is have the same value in name or else just append the values in table two to table 1.
Unfortunately, MySQL does not support full outer join. Here is a method using union all and group by:
select name, sum(qty) as qty
from ((select name, qty from a) union all
(select name, qty from b)
) ab
group by name;
To simulate a full outer join, just execute a left outer join (gives all the rows of Table A with all matching rows of Table B or NULL) and a right outer join where Table A is NULL (gives all the rows of Table B that have no match in Table A -- matches are already provided in first query).
In the first query, there will always be a Qty value from Table A with either a Qty value or NULL from Table B. In the second query, there will only be a Qty value from Table B.
See Fiddle results.
select a.Name, a.Qty + IsNull( b.Qty, 0 ) as Qty
from #TableA a
left outer join #TableB b
on b.Name = a.Name
union all
select b.Name, b.Qty
from #TableA a
right outer join #TableB b
on b.Name = a.Name
where a.Name is null;
You may use union or union all with the same results. Since there is less processing required with union all, that's what I chose.
now i am working with three tables,A,B and C:
A table structure:
ID,
Name,
Age
B table structure:
ID,
A.ID-> foreign key,
Hospital Name
C table structure:
ID,
A.ID->foreign key,
Drug Name
so the relation between A and B is one to many and also the same with A and C
when i make any query to find how many persons in my database i found duplicated rows
actually its not duplicated but it has for example 2 rows with the child table like: the one person has 2 records in table B so the result doesn't reflect the actual number of matched record because its linked with child tables .
Question is : how to prevent duplication in case like that?
You can use Distinct or a subquery.
SELECT DISTINCT a.ID, a.Name
FROM a
INNER JOIN b
ON a.ID = b.aID
WHERE b.Hospital = 123
Or
WHERE b.Hospital IN ( 123, 456 )
Also
SELECT a.ID, a.Name
FROM a
INNER JOIN (SELECT aID, Hospital FROM b) x
ON a.ID = x.aID
WHERE x.Hospital - 123
And
SELECT a.ID, a.Name
FROM a
WHERE a.ID
IN ((SELECT aID FROM b WHERE Hospital = 123))
I need to get the distinct rows based on a single column (code in this case) where there are duplicates of that column value. Along with other information from the row and the number of duplicate rows there are. for example:
ID code ownerName
--------------------------
1 001 Mr. Brown
2 001 Mr. Pink
3 002 Mr. White
4 003 Mr. Blonde
I need this query to return
ID code ownerName count
----------------------------------
1 001 Mr. Brown 2
the duplicate row information does not matter which gets returned, but I'm having trouble combining the distinct codes with the count column.
I've tried a query like this:
SELECT DISTINCT A.code, A.ownerName
FROM Customers A WHERE
EXISTS( SELECT * FROM Customers WHERE code = A.code AND id <> A.id)
order by A.code;
but I'm having trouble getting the count; and with this query
SELECT code, COUNT(*) as numberDuplicates
FROM Customers GROUP BY code HAVING COUNT(*) > 1
I'm having trouble getting other information I don't want to group by. Can anyone help me figure out how to structure the correct query?
If I understand what you are looking for, this should work:
This will select all entries with a non-unique code and return the number of records using that code.
SELECT DISTINCT A.ID, A.Code, A.ownerName, B.Count
FROM Customers A
JOIN (
SELECT COUNT(*) as Count, B.Code
FROM Customers B
GROUP BY B.Code
) AS B ON A.Code = B.Code
WHERE B.Count > 1
ORDER by A.Code;
I think you can try something like below
SELECT TOP 1 C.ID, C.Code, C.OwnerName, C1.NumberDuplicates
FROM
Customers C
INNER JOIN
(
SELECT Code, COUNT(*) as NumberDuplicates FROM Customers GROUP BY code HAVING COUNT(*) > 1
) C1
ON C.Code = C1.Code
Hope this Helps!!
SELECT COUNT(DISTINCT CustomerID) AS NumberOfCustomers FROM Orders;