There are 2 tables Contacts & BlockedEntries
Table: Contact
Id
FirstName
LastName
Email
JobTitle
Table: BlockedEntries
Id
Email
Trying to find the entries from contact table with more than one occurence of blockedEntries
SELECT email, COUNT(*) as cc, GROUP_CONCAT( id SEPARATOR '#') AS ContactIds
FROM contacts
where email IN (SELECT email FROM BlockedEntries)
GROUP BY email
HAVING COUNT(*) > 1
Is there any way to get some more additional details like first name ,last Name, email,job title etc for the entries with count is more than 1
Unfortunately there is no relation between these 2 tables and email is the only possible mapping . There can be 1 or more entries present in COntact table with same email address
Sample data
Id FirstName LastName Email JobTitle
12 sam j samj#gmail.com engineer
23 bos j bosj#gmail.com accountnt
34 cas j samj#gmail.com engineer
33 xxx j bosj#gmail.com fied
55 dfe c dfe#gmail.com student
Table: BlockedEntries
Id Email CreateDate
1 samj#gmail.com 09/12/2020 19:30:20
2 bosj#gmail.com 09/12/2020 19:30:20
3 dfe#gmail.com 09/12/2020 19:30:20
Result expecting
email id firstname lastName jobtitle
samj#gmail.com 12 sam j engineer
samj#gmail.com 34 bos j accountnt
bosj#gmail.com 23 cas j engineer
bosj#gmail.com 33 xxx j fied
dfe#gmail.com only 1 instance and no need to add this to result set
On MySQL 8+, I would use COUNT() as an analytic function here:
WITH cte AS (
SELECT *, COUNT(*) OVER (PARTITION BY email) email_cnt
FROM contacts
)
SELECT c.Id, c.FirstName, c.LastName, c.Email, c.JobTitle
FROM cte c
WHERE email_cnt > 1 AND
EXISTS (
SELECT 1
FROM BlockedEntries be
WHERE be.email = c.email
);
Based on your data example you just need two inner joins, one with the BlockedEntries which will get you all the related emails between BlockedEntries and Contact table and another join with a subquery which will get the emails more than once on Contact table.
Try:
select c.Id,
c.FirstName,
c.LastName,
c.Email,
c.JobTitle
from Contact c
inner join BlockedEntries be on be.Email=c.Email
inner join (select Email
from Contact
group by Email
having count(*)>1
) as cnt on cnt.Email=c.Email;
https://dbfiddle.uk/P9Y4RHfu
Related
I have a tables companies and addresses and need to get all duplicated rows
Checking columns is companies.phone_number and addresses.columns
Table companies
uuid
name
phone_number
5esd
A INC.
667-784-343
6dcv
B INC.
866-653-343
56js
C INC.
111-222-333
tug8
D INC.
111-222-333
jkj9
E INC.
777-666-443
Table Addresses
id
parent_uuid
a1
a2
postal
1
5esd
st2
st3
444
2
6dcv
st2
st3
444
3
56js
st55
st56
545
4
tug8
st77
st78
675
I need four rows:
uuid
name
phone_number
5esd
A INC.
667-784-343
6dcv
B INC.
866-653-343
56js
C INC.
111-222-333
tug8
D INC.
111-222-333
Because two first records has same addresses and two last records has same phone numbers
One way:
select c.*
from companies c
where uuid in ( select parent_uuid
from ( select parent_uuid,
count(*) over(partition by a1,a2,postal) as cnt
from Addresses
union all
select uuid,
count(*) over(partition by phone_number) as cnt2
from companies
) as tbl
where cnt >1
) ;
https://dbfiddle.uk/EqGajA1t
With union all we find duplicates of each table getting the uuid and parent_uuid with duplicates and using the in operator containing the duplicated uuid and parent_uuid;
Something like this will give the desired output :
SELECT uuid,name,phone_number
FROM companies
INNER JOIN Addresses
ON companies.uuid = Addresses.parent_uuid;
I have two tables like this:
Employee Table:
EmployeeID
firstName
lastName
1
Johnny
Depp
2
Rebecca
Smith
3
Rodger
Doe
Sales Table:
EmployeeID
Sales
1
100.20
2
200.19
3
355.23
And I'd like to join the tables to do something like this:
EmployeeID
fullName
Sales
1
Johnny Depp
100.20
2
Rebecca Smith
200.19
3
Rodger Doe
355.23
How would I do that? Here's what I tried so far:
SELECT employee.firstName + employee.lastName AS fullName, employeeID, sales
FROM employee i
INNER JOIN Sales s ON s.customerID = i.CustomerID
I'm getting a syntax error at my "+" symbol.
What's my problem?
as #Gordon said ,use CONCAT():
SELECT CONCAT(employee.firstName, ' ', employee.lastName) AS fullName
, employeeID
, sales
FROM employee i
INNER JOIN Sales s
ON s.customerID = i.CustomerID
I have this table
CustomerID CustomerName Bank Amount
1 Martin BDO Php 55.00
1 Martin CBA Php 150.00
2 Grace BDO Php 45.00
2 Grace BDO Php 4100.00
3 Blake BPI Php 120
I need a sql statement that will display customer with accounts on two different banks.
The result should be
CustomerID CustomerName Bank Amount
1 Martin BDO Php 55.00
1 Martin CBA Php 150.00
How can I get this result?
You can use GROUP BY with HAVING to do this, e.g.:
SELECT *
FROM customer
WHERE customerID IN (
SELECT customerID
FROM customer
GROUP BY customerID
HAVING COUNT(DISTINCT(Bank)) > 1
);
There are several different ways you can get the result that you want. You could join on a subquery that gets a list of CustomerIds with more than one distinct Bank:
select
m1.CustomerId,
m1.CustomerName,
m1.Bank,
m1.Amount
from mytable m1
inner join
(
select
CustomerId
from mytable
group by CustomerId
having count(distinct Bank) >= 2
) m2
on m1.CustomerId = m2.CustomerId;
Or you could use a WHERE EXISTS to also get the result:
select
m.CustomerId,
m.CustomerName,
m.Bank,
m.Amount
from mytable m
where exists (select 1
from mytable m2
where m.CustomerId = m2.CustomerId
and m.Bank <> m2.Bank);
Here is a demo
Code:
SELECT s.*, k.address FROM student s
INNER JOIN address k
ON instr(s.fulladdress, k.address) > 0
WHERE j.phonetic like '%california%'
This result will display 2 rows with 2 parts of address:
30,Andre Hans, Address 1 Address 2,class IBA1001, Address 1
30,Andre Hans, Address 1 Address 2,class IBA1001, Address 2
I want to display it in 1 row:
30,Andre Hans, Address 1 Address 2,class IBA1001, Address 1 | Address 2
How i can do that?
Use group by and group_concat:
SELECT s.*, group_concat(k.address separator ' | ') FROM student s
INNER JOIN address k
ON instr(s.fulladdress, k.address) > 0
WHERE j.phonetic like '%california%'
GROUP BY s.col1, s.col2 .... -- all the columns in table student
Below is the table structure:
I have three tables : employee, skill and employee_skills.
Employee: id, firstname, lastname, etc.....
Skill : id, title, description
Emplyoee_skills : id, employee_id (FK of employee table), skill_id(FK of skill table)
Now, I want the below output:
Employee
Id firstname lastname
1 Rajnikant Patel
2 Steve Jobs
3 Sachin Tendulkar
4 Ratan Tata
Skill
Id title description
1 java java
2 mongodb mongodb
3 PHP PHP
4 spring Spring framework
Employee_skills
Id employee_id skill_id
1 1 1
2 1 2
3 2 1
4 3 2
So I want the query which can return the employee records who have the skills that are passed:
Let's say, I pass in where clause : s.title in ('mongodb', 'java'), then it should return record:
Id firstName lastName
1 Rajnikant Patel
Because this employee has both the skills.
Here is how to do it: Select all employee skills for the desired skills, then only keep employees having the full count.
select *
from employee
where id in
(
select employee_id
from employee_skills
where skill_id in
(
select id
from skill
where title in ('java', 'mongodb')
)
group by employee_id
having count(distinct skill_id) = 2
);
When adding another skill, you must check for a count of 3, of course, etc.
Below query will give you desired results.
SELECT e.id, e.firstname, e.lastname
FROM Employee e
JOIN Employee_skills es
ON es.employee_id = e.id
JOIN Skill s
ON es.skill_id = s.id
WHERE s.title IN ('mongodb', 'java')
GROUP BY e.id
HAVING count(s.id) = 2