Find duplicate values within 3 columns SQL - mysql

I have a table like the below:
Site | Name | ID
A | Mike | 1
A | Mary | 2
A | Mary | 3
B | Mary | 1
B | Rich | 2
I'd like to find all the duplicate Name's within a Site. So I'm trying to return:
Site | Name | ID
A | Mary | 2
A | Mary | 3
I've tried this:
SELECT DISTINCT Site, Name, ID
from table
group by ID having count(*) > 1
The results come back erroneously because it's counting Sites A & B together. I would like to only find the duplicates for within each Site--not duplicates across Sites.

You can use exists or in:
select t.*
from t
where exists (select 1
from t t2
where t2.site = t.site and t2.name = t.name and t2.id <> t.id
);

You can try to use NOT exists with subquery having.
the duplicate Name's within a Site
CREATE TABLE T(
Site varchar(5),
Name varchar(5),
ID int
);
insert into t values ('A','Mike', 1);
insert into t values ('A','Mary', 2);
insert into t values ('A','Mary', 3);
insert into t values ('B','Mary', 1);
insert into t values ('B','Rich', 2);
Query 1:
SELECT * FROM T t1 WHERE
NOT exists
(
SELECT 1
FROM T tt
where t1.name = tt.name
group by Name
HAVING MIN(tt.ID) = t1.ID
)
Results:
| Site | Name | ID |
|------|------|----|
| A | Mary | 2 |
| A | Mary | 3 |
find the duplicates for within each Site
CREATE TABLE T(
Site varchar(5),
Name varchar(5),
ID int
);
insert into t values ('A','Mike', 1);
insert into t values ('A','Mary', 2);
insert into t values ('A','Mary', 3);
insert into t values ('B','Mary', 1);
insert into t values ('B','Rich', 2);
Query 1:
SELECT t1.*
FROM T t1
WHERE not exists
(
SELECT 1
FROM T tt
where t1.name = tt.name and t1.Site = tt.Site
group by Name,Site
HAVING MIN(tt.ID) = t1.ID
)
Results:
| Site | Name | ID |
|------|------|----|
| A | Mary | 3 |

Related

2 ids in one row, firstname and lastname in other. How to receive firstname from other table

I'm struggling with 1 excercise since month I guess and hope you can help me. I've tried subquery in from, with or join tables in many ways and didn't received proper result.
OK. One row in Table1 stores information like:
Client's ID
firsname
lastame
Waiter id
One row in Table2 stores information like:
userID (client and waiter are the user)
firstname
lastname
I'm new to SQL but how I see this is:
WITH waiter AS(
SELECT
waiter_id,
firstname,
lastname
FROM Table1
JOIN Table2 ON Table1.waiter_id = Table2.user_id
)
SELECT
client_id,
firstname as Firstname_client,
lastname as Lastname_client,
waiter_id,
waiter.firstname as Firstname_waiter,
waiter.lastname as Lastname_waiter
FROM Table1
JOIN waiter ON Table1.waiter_id = waiter.waiter.id
I would be very grateful for any clues!
You can try this query. just use an alias name and JOIN
TestDLL
CREATE TABLE Table1(
client_id int,
firstname varchar(50),
lastname varchar(50),
waiter_id int
);
INSERT INTO Table1 values (1,'Andrew','x',5);
INSERT INTO Table1 values (2,'Chris','xx',6);
INSERT INTO Table1 values (3,'Anna','xxx',7);
INSERT INTO Table1 values (4,'Julia','xxxx',8);
CREATE TABLE Table2(
user_id int,
firstname varchar(50),
lastname varchar(50)
);
INSERT INTO Table2 values (1,'Andrew','x');
INSERT INTO Table2 values (2,'Chris','xx');
INSERT INTO Table2 values (3,'Anna','xxx');
INSERT INTO Table2 values (4,'Julia','xxxx');
INSERT INTO Table2 values (5,'Mat','xxxxx');
INSERT INTO Table2 values (6,'Kathy','xxxxxx');
INSERT INTO Table2 values (7,'Pual','xxxxxxx');
INSERT INTO Table2 values (8,'John','xxxxxxxx');
Query
SELECT t1.client_id ,
t1.firstname AS Firstance_client,
t1.lastname as Lastname_Client,
t1.waiter_id,
t2.firstname as Firstname_waiter ,
t2.lastname as Lastname_Waiter
FROM Table1 t1
inner join Table2 t2 on t1.waiter_id = t2.user_id
sqlfiddle
[Results]:
| client_id | Firstance_client | Lastname_Client | waiter_id | Firstname_waiter | Lastname_Waiter |
|-----------|------------------|-----------------|-----------|------------------|-----------------|
| 1 | Andrew | x | 5 | Mat | xxxxx |
| 2 | Chris | xx | 6 | Kathy | xxxxxx |
| 3 | Anna | xxx | 7 | Pual | xxxxxxx |
| 4 | Julia | xxxx | 8 | John | xxxxxxxx |
You can use below query to get third table
Select ClientId,
c.FirstName as Client_FirstName,
c.LastName Client_LastName,
w.FirstName as Waiter_FirstName,
w.LastName as Waiter_LastName,
w.UserId as WaiterId
FROm dbo.Table1 as c
LEFT JOIN dbo.Waiter as[enter image description here][1] w on c.WaiterId = w.UserId
Sql Snippet

Avoide duplicate id in the response

I am trying to accomplish the following sql statement but I am getting one duplicate id in my response.
SELECT ci.customer_id,
ci.first_name,
ci.user_gender,
ci.customer_status,
fr.relation
FROM customerinfo ci
INNER JOIN familyrelation fr
ON ( fr.personid_two = ci.customer_id )
WHERE ci.customer_id IN (SELECT personid_two
FROM familyrelation
WHERE personid_one = 17)
AND ci.csp_user_id = 5;
When i run this query, I am fetching the proper result, but one customer_id is getting repeated. Any help/advice greatly appreciated.
If your data looks like this
drop table if exists ci,fr;
create table ci(customer_id int, name varchar(3),csp_user_id int);
create table fr(personid_one int,personid_two int,relation varchar(10));
insert into ci values (1,'aaa',5),(2,'bbb',5);
insert into fr values (17,1,'mother'),(17,1,'father'),(17,2,'niece');
Then your query selects the rows I would expect
SELECT ci.customer_id,
ci.name,
fr.relation
FROM ci
INNER JOIN fr
ON ( fr.personid_two = ci.customer_id )
WHERE ci.customer_id IN (SELECT personid_two
FROM fr
WHERE personid_one = 17)
AND ci.csp_user_id = 5;
+-------------+------+----------+
| customer_id | name | relation |
+-------------+------+----------+
| 1 | aaa | mother |
| 1 | aaa | father |
| 2 | bbb | niece |
+-------------+------+----------+
3 rows in set (0.00 sec)

Join related Issue

New to SQL
Suppose we have two tables
One has got the ID and Name column :
+----+-------+
| ID | Name |
+----+-------+
| 1 | Sam |
| 1 | Dan |
+----+-------+
and the second one has also got two columns as follow :
+----+------------+
| ID | Relatives |
+----+------------+
| 1 | Uncle |
| 2 | Aunty |
+----+------------+
If we do inner join we would only get the rows where the condition satisfies. But i want the output to be Like
+------+------------+
| ID | Relatives |
+------+------------+
| 1 | Uncle |
| NULL | Aunty |
+------+------------+
once only the value in the ID column should be shown. If the occurrence is twice or thrice it should come as null.
Just tell me if it is possible or not? and How for both the cases.
As your question is not clear, so assuming that you need to retrieve id from table a and name from table b and you also want to avoid duplicate rows, then an option could be to use distinct along with left join:
select distinct a.id, b.name
from b
left outer join a
on b.id = a.id
order by id desc
Result:
+------+-------+
| id | name |
+------+-------+
| 1 | Uncle |
| NULL | Aunty |
+------+-------+
DEMO
Try this:
SELECT
T1.Id,
T2.Relatives
FROM SecondTable T2
LEFT JOIN FirstTable T1
ON T1.ID = T2.ID
GROUP BY T1.Id,
T2.Relatives
This is what I get exactly:
CREATE TABLE #a (
id int,
name varchar(10)
)
CREATE TABLE #b (
id int,
name varchar(10)
)
INSERT INTO #a
VALUES (1, 'sam')
INSERT INTO #a
VALUES (1, 'Dan')
INSERT INTO #b
VALUES (1, 'Uncle')
INSERT INTO #b
VALUES (2, 'Aunty')
SELECT
T1.Id,
T2.name
FROM #b T2
LEFT JOIN #a T1
ON T1.ID = T2.ID
GROUP BY T1.Id,
T2.name
DROP TABLE #a
DROP TABLE #b
Output:
Id name
NULL Aunty
1 Uncle
Hope, this is what you ask in your question.

Odd behavior of max and having in MySQL when max==0

I have the following table:
mysql> select * from foo;
| id | value | bar |
+----+-------+------+
| 1 | 2 | 3 |
| 2 | 0 | 3 |
| 1 | 1 | 5 |
I want to select the tuple with the maximum value for each id. However, when max(value) is 0, I don't get a result.
mysql> select id,max(value),bar from foo group by id having max(value);
| id | max(value) | bar |
+----+------------+------+
| 1 | 2 | 3 |
Is this supposed to behave like that and if so, why?
HAVING cannot be used in any way to pick a record out of a group of records as defined by the fields used in the GROUP BY clause. It is rather applied to the group as a whole.
So, in your case, you have to do a self-join to get the rest of the table fields:
select t1.id, t1.value, t1...
from foo as t1
join (
select id, max(value) as max_value
from foo
group by id
) as t2 on t1.id = t2.id and t1.value = t2.max_value
IMHO you can get MAX couple by multiplying (id x value).
create table foo(id int, value int);
insert into foo values
(2,0),
(1,0),
(2,1),
(3,0),
(2,2);
select id, value
from foo
order by (id * value) desc
limit 1;
id | value
2 | 2
drop table foo;

MySQL query eleminating duplicates from second table

I have two mysql tables: table_1 and table_2.
table_1:
| c_id | name | email |
| 1 | tom | t#t.com |
table_2:
| a_id | c_id | address | street |
| 1 | 1 | 67 | home |
| 2 | 1 | 68 | main |
How to create mysql query that will select table_1.name, table_1.email, table_2.address and table_2.street returning only one record like:
| name | email | address | street |
| tom | t#t.com | 67 | home |
Thanks for any suggestion.
Regards, T
Try this:
SELECT table_1.name, table_1.email, table_2.address, table_2.street
FROM table_1
JOIN table_2
ON table_1.c_id = table_2.c_id
GROUP BY table_1.c_id
SQLfiddle demo
The GROUP BY will determine which column you want to use to group the results by. B.T.W. if you like the change the column head you can add AS followed by the column head name (table_1.name AS "First name").
Table structure and sample data:
CREATE TABLE table_1
(`c_id` int, `name` varchar(3), `email` varchar(7))
;
INSERT INTO table_1
(`c_id`, `name`, `email`)
VALUES
(1, 'tom', 't#t.com')
;
CREATE TABLE table_2
(`a_id` int, `c_id` int, `address` int, `street` varchar(4))
;
INSERT INTO table_2
(`a_id`, `c_id`, `address`, `street`)
VALUES
(1, 1, 67, 'home'),
(2, 1, 68, 'main')
;
If you like to limit the sql query to a certain person say tom WHERE table_1.name LIKE 'Tom'. Like so:
SELECT table_1.name, table_1.email, table_2.address, table_2.street
FROM table_1
JOIN table_2
ON table_1.c_id = table_2.c_id
WHERE table_1.name LIKE 'Tom'
GROUP BY table_1.c_id;
You can also use = but with LIKE you can use wildcards like T%
you should use the SQL statement LEFT JOIN, for example
SELECT name, email, address, street
FROM table1
LEFT JOIN table2
ON table1.c_id=table2.c_id;
with the possible option WHERE name = 'tom'
select table_1.name as name, table_1.email as email, table_2.address as address, table_2.street as street
from table_1
left join table_2 on table_1.c_id = table_2.c_id