Get values from 2 different table but shows only 1 table data - sql

Hie
It might be unclear my query in the title,Let me clear you in the description.
I have 2 tables in the DB with name of 'ABC' and 'XYZ'.
ABC table
| ID | name | phone | gender |
1 | dave | 23423 | Male
2 | rayman | 987887 | female
3 | shawn | 6237267 | male
XYZ table
| ID | user-id | blood-group | rh-factor |
1 | 3 | AB | +
2 | 1 |
B | -
As you seen above there are two table, now I want result that user whose gender is Male, blood-group is B and rh-factor is negative.
but output should be
| ID | name | phone | gender |
1 | dave | 23423 | Male
hope you understand what I mean.I don't know there should be user GROUP BY or what.
thanks in advance.

SELECT a.* FROM ABC a JOIN XYZ b ON a.ID=b.ID
WHERE b.Bloodgroup='B' AND a.gender='Male' and b.rh_factor='-'

Try this!..
select abc.id,abc.name,abc.phone,abc.gender from abc inner join xyz
on abc.id = xyz.user-id where abc.gender = 'male' and xyz.blood-group = 'b' and xyz.rh-factor='-'
Regards.
SK

You can simply use INNER JOIN :
SELECT a.* FROM ABC a INNER JOIN XYZ b ON a.ID = b.`user-id`
WHERE b.bood_group = 'B' and b.rh-factor = '-'
Use backtick(`) if your column has hyphen in it like it in your user-id

Related

Updating the column that the table was joined on

I need to update a column that two tables were joined on and I'm having a difficult time wrapping my head around it. This is for SQL Server. Loose example below...
User
ID | Name | GroupID |
---------------------
1 | Bob | 100 |
2 | Alex | 300 |
3 | Sara | 300 |
Group
ID | Name |
----------------
100 | Produce |
200 | Cashier |
300 | Stocker |
GroupID is a foreign key to the Group table and they are being joined on that. I HAVE to update the GroupID column in User based on the Name column in Group. For example, I want Alex and Sara to change from 'Stocker' to 'Cashier'. My solution is below, but it doesn't seem to work.
UPDATE User
SET User.GroupID = G.ID
FROM User U
JOIN Group G ON U.GroupID = G.ID
WHERE User = 'Sara' OR User = 'Alex'
Expected Result
User
ID | Name | GroupID |
---------------------
1 | Bob | 100 |
2 | Alex | 200 |
3 | Sara | 200 |
You don't need and updated with join ..
but you could use a subquery for get the expected id from group
update user
set User.GroupID = (select id
from group where name = 'Cashier )
where User = 'Sara' OR User = 'Alex'

Join three tables based on one key, putting data into same column

I have three tables that I am trying to join together to check that the proper data matches. I have table A which is a list of all accounts that a commission was paid on and what that commission amount was. I have Table B and Table C which are two tables that have commission calculations in it. The goal is to compare Table A to Table and to Table C and pulling back the amounts from both tables to ensure a match. The part I am struggling with is, Table A has all the accounts that are the base population. Table B has some and Table C as some. An account will be in either Table B or C, but never in both. I want to pull the payment from Table A, and then verify to the payment in Table B or C(whichever it occurs) and the same with commission. I then am doing a case when that compares the two fields and tells me if it matches are not.
+---------+---------+-----+------+
| Table A | | | |
+---------+---------+-----+------+
| Account | Uniq_ID | Pay | Comm |
| 12345 | ABCD | 100 | 10 |
| 23456 | OLPOL | 25 | 2 |
| 45678 | LKJHG | 200 | 15 |
| 96385 | LKJ67 | 250 | 26 |
+---------+---------+-----+------+
+---------+---------+-----+------+
| Table B | | | |
+---------+---------+-----+------+
| Account | Uniq_ID | Pay | Comm |
| 12345 | ABCD | 100 | 8 |
| 45678 | LKJHG | 200 | 15 |
+---------+---------+-----+------+
+---------+---------+-----+------+
| Table C | | | |
+---------+---------+-----+------+
| Account | Uniq_ID | Pay | Comm |
| 23456 | OLPOL | 25 | 2 |
| 96385 | LKJ67 | 250 | 32 |
+---------+---------+-----+------+
I am trying to get my results to show up in a columns called pay_ver and comm_verf, and it would populate with the data from either Table B or C based on which it matched with. I am hoping to have to output look like so....
+---------+---------+-----+----------+------+-----------+---------+
| Output | | | | | | |
+---------+---------+-----+----------+------+-----------+---------+
| Account | Uniq_ID | Pay | Pay_verf | comm | comm_Verf | Matched |
| 12345 | ABCD | 100 | 100 | 10 | 8 | No |
| 23456 | OLPOL | 25 | 25 | 2 | 2 | Yes |
| 45678 | LKJHG | 200 | 200 | 15 | 15 | Yes |
| 96385 | LKJ67 | 250 | 250 | 26 | 32 | No |
+---------+---------+-----+----------+------+-----------+---------+
This is the code I have used to join Table A to B, and Table A to C but I have done this in two separate queries giving me two outputs. I would like to be able to do this in one, so I only have one output.
select a.account, a.uniq_id, a.pay, b.pay as pay_verf, a.comm, b.comm as comm_verf,
CASE WHEN a.comm = b.comm THEN 'MATCHED'
ELSE 'UNMATCHED'
END as Matched
from tblA a
left join tblB b
on a.account = b.account
and a.uniq_id = b.uniq_id;
I can not just figure out how to also get it to join to Table C without adding an extra column.
You can do:
select
account, uniq_id, pay,
pay_total as pay_verf,
comm,
comm - comm_total as comm_verf,
case when comm = comm_total then 'Yes' else 'No' end as matched
from (
select
a.account, a.uniq_id, a.pay, a.comm,
coalesce(b.pay, 0) + coalesce(c.pay, 0) as pay_total,
coalesce(b.comm, 0) + coalesce(c.comm, 0) as comm_total
from table_a a
left join table_b b on a.account = b.account
left join table_c c on a.account = c.account
) x
You are very close. Just need to add one more join and an addition WHEN to your case statement. This should act like an if elseif else logic. So it checks if a.comm = b.comm and then checks a.comm = c.comm. If neither match if will set to unmatched. This works well because you stated the ID can't be in both B and C.
select a.account, a.uniq_id, a.pay, b.pay as pay_verf, a.comm, b.comm as comm_verf,
CASE WHEN a.comm = b.comm THEN 'MATCHED'
WHEN a.comm = c.comm THEN 'MATCHED'
ELSE 'UNMATCHED'
END as Matched
from tblA a
left join tblB b
on a.account = b.account
and a.uniq_id = b.uniq_id;
left join tblB c
on a.account = c.account
and a.uniq_id = c.uniq_id;
Yet another option could be something like
SELECT a.account, a.uniq_id, a.pay, bc.pay as pay_verf, a.comm, bc.comm as comm_verf
FROM a left join (
SELECT * from b
UNION ALL
SELECT * from c
) bc on (a.account = bc.account and a.uniq_id = bc.uniq_id)

Why is a WHERE clause to exclude rows in SQLite not working as expected?

I am trying to exclude a list of Names in an
I have the table Names
id| Name | Surname
---------------------
1 | Michael | Kane
2 | Torben | Dane
3 | Dinge | Chain
4 | Django | Fain
5 | Juliett | Bravo
And i have the Table Excludes
id| Name
-----------
1 | Michael
2 | Torben
Now I have two queries:
SELECT * From Names, Excludes
WHERE Names.Name = Excludes.Name
GROUP BY Names.Name
which results in
id | Name | Surname | id | Name
--------------------------------
1 | Michael | Kane | 1 |Michael
2 | Torben | Dane | 2 |Torben
Now i want to do the exact opposite with != to do the actual purpouse and erase the lines which have the names Michael and Torben in it
The seconds query is:
SELECT * From Names, Excludes
WHERE Names.Name != Excludes.Name
GROUP BY Names.Name
The Result is
id | Name | Surname | id | Name
--------------------------------
3 | Dinge | Chain | 2 |Torben
4 | Django | Fain | 2 |Torben
5 | Juliett | Bravo | 2 |Torben
1 | Michael | Kane | 2 |Torben
2 | Torben | Dane | 1 |Michael
The Result I would want to have is
id| Name | Surname
---------------------
3 | Dinge | Chain
4 | Django | Fain
5 | Juliett | Bravo
What am I doing wrong?
If you are going to use a join (as opposed to not in or not exists), you want a left join and where clause:
SELECT n.*
From Names n LEFT JOIN
Excludes e
ON n.Name = e.Name
WHERE e.Name IS NULL;
A simple rule: Never use commas in the FROM clause.
select * from Names
where Name not in (select Name from Excludes)
Use a left join instead and filter out null values:
SELECT Names.*
FROM Names
LEFT JOIN Excludes ON Names.Name = Excludes.Name
WHERE Excludes.Name IS NULL
The group by clause seemed meaningless so I removed it.
Another option is to use the not exists predicate with a correlated subquery:
SELECT * FROM Names n
WHERE NOT EXISTS
(SELECT Name FROM Excludes e WHERE e.Name = n.Name)
You can simply use (not in) with a inner query on Excludes ,
select * from Names where Name not in (select Name from Excludes)

Decode more than on ID with two tables

TABLE PEDIDO
id_name | ID_cabimento | ID_direction
1 | 4 | 5
2 | 3 | 6
3 | 4 | 5
TABLE USER
id_name | name
1 | João
2 | Maria
3 | António
4 | Manuel
I WANT FOR RESULT
name | cabimento | direction
João | Manuel | Tozé
Maria | António | Joaquim
António | Manuel | Tozé
...
I tried UNION and JOIN but did not get the desired result... because I only can decode 1 ID.
you can join n times on same table, just using n aliases
select n.name as name, c.name as cabimento, d.name as direction
from pedido p
inner join user n on p.id_name = n.id_name
inner join user c on p.id_name = c.id_cabimento
inner join user d on p.id_name = d.id_direction

display basic information from tables

I have two tables i.e.
Users
uid | firstname
1 | John
2 | Bob
3 | Paul
4 | Peter
Calls
cid | assigned_to | caller_id
1 | 2 | 1
2 | 1 | 3
3 | 2 | 4
4 | 4 | 2
assigned_to and caller_id are just the uid in users.
I just want to display the results of each call:
call_id | username(assigned_to) | username(caller_id)
How can I do this in SQL?
Thanks,
Try this:
select
cid as call_id,
A.username, -- assingned to
B.username -- caller id
from calls
left join users A on calls.assigned_to = A.uid
left join users B on calls.caller_id = B.uid