Select Records with same email in different columns - sql

I want to extract all records that has same email in different fields from an email table. I am not sure how to do that in sql. looking for help.The image has id and email fields. I am trying to extract records which has same email in different fields.

If I understand your question correctly, you need to compare each email address in a row with every other email address in the same row, taking into account that you don't want to have empty email addresses compare equal. Here's my suggestion (which I've played around with a bit here):
select * from email
where
(email1 <> '' and email1 in (email2, email3, Add1, Add2))
or (email2 <> '' and email2 in (email3, Add1, Add2))
or (email3 <> '' and email3 in (Add1, Add2))
or (Add1 <> '' and Add1 = Add2)
Additional note: If you have superfluous spaces tagged on to any emails, you may need to trim them all.

Related

How to distribute different types of values from one column into individual columns

I have one table that stores different values in one column.
Lets say the structure is like:
column_a: name
column_b: contact_type - email or phone
column_c: contact_value - exact email address or phone number
Shall a person have both email and phone, there are two rows.
I trying to get a select that would distribute the different values into respective columns:
column_a: name
column_b: email_value
column_c: phone_value
One person should have exactly one row.
So far all my attempts ended one person having two rows - email address and phone null and vice versa...
Is there a way how to distribute the two values into two respective columns and have just one record for each person?
Hope it is not too confusing question...
You can use conditional aggregation:
select column_a,
max(case when column_b = 'phone' then column_c end) as phone,
max(case when column_b = 'email' then column_c end) as email
from t
group by column_a;

Update Column from Columns within same table using SQLite

I’m trying to update an email address value from users first- and lastname within the same table.
Pattern for generated Mail: <Firstname>.<Lastname>#test.org
Example Table “Persons”:
Firstname Lastname email
Henley Figueroa none#none.com
Samina Morrison none#none.com
Dev Rowe none#none.com
Wished result for table “Persons”:
Firstname Lastname email
Henley Figueroa Henley.Figueroa#test.org
Samina Morrison Samina.Morrison#test.org
Dev Rowe Dev.Rowe#test.org
SQL Code
UPDATE Persons
SET
email = (SELECT FirstName || "." || LastName ||"#"||"test.org" FROM Persons)
Actual result for table “Persons”:
Firstname Lastname email
Henley Figueroa Henley.Figueroa#test.org
Samina Morrison Henley.Figueroa#test.org
Dev Rowe Henley.Figueroa#test.org
Only the first record of the returned result table is used over and over. Why?
If I ommit the SELECT:
UPDATE Persons
SET
email = FirstName || "." || LastName ||"#"||"test.org"
I get the expected result.
Referring to your answers, I'm extending my question with an example of two tables. One with only names, the second for mail addresses.
Table Persons:
ID*
Firstname
Lastname
1
Henley
Figueroa
2
Samina
Morrison
3
Dev
Rowe
Table Addresses:
ID*
email
1
wrong#wrong.ng
2
wrong#wrong,ng
3
wrong#wro.ng
UPDATE Addresses
SET email = (SELECT Firstname ||"."|| Lastname || "#test.org"
FROM Persons WHERE Persons.id = Addresses.id)
Demo
Here the UPDATE with SET and SELECT works (Every person gets a unique mail address). Shouldn’t I get the same problem here?
From the output of SELECT I see that I get again (as expected) three records.
You have the right syntax for your problem. So the question is why does this not do what you want?
UPDATE Persons
SET email = (SELECT FirstName || "." || LastName ||"#"||"test.org"
FROM Persons
);
In fact, this should produce an error in any SQL engine -- although SQLite can be a bit lax about such errors. Why? The SET is expecting a single value. This code has a subquery that returns three values.
Note: The SQL Fiddle does show that SQLite accepts this syntax. Argggh! You can switch this to Postgres to see the error. With some modifications, to the create code you could also see the same error (subquery returns more than one row) using SQL Server or Oracle or just about any database.
Apparently, SQLite is just arbitrarily choosing one of those values -- the one that it first encounters. And this is the same value for all three rows.
You already know that a subquery is not correct here. You have the correct code in your question.
From Subquery Expressions:
A SELECT statement enclosed in parentheses is a subquery. All types of
SELECT statement, including aggregate and compound SELECT queries
(queries with keywords like UNION or EXCEPT) are allowed as scalar
subqueries. The value of a subquery expression is the first row of
the result from the enclosed SELECT statement. The value of a
subquery expression is NULL if the enclosed SELECT statement returns
no rows.
So the value you get by the subquery is the first row of the rows returned.
Of course, you don't need the SELECT statement.
This is a simple UPDATE statement that involves only the values of the columns of the current row:
UPDATE Persons
SET email = FirstName || '.' || LastName || '#' || 'test.org'
See the demo.
Results:
Firstname
Lastname
email
Henley
Figueroa
Henley.Figueroa#test.org
Samina
Morrison
Samina.Morrison#test.org
Dev
Rowe
Dev.Rowe#test.org
For the 2nd query with the 2 tables:
Shouldn’t I get the same problem here?
No, because the subquery is a correlated subquery, which returns for each row in the table Addresses only 1 row (provided there are no duplicate ids in Persons), the row which matches the condition Persons.id = Addresses.id.
If there is no row that matches the condition then it will return NULL.

Trying to write a SQL expression that drops a collection of rows

I've indexed my email inbox and have a table of email_addresses, thread_id, subject, to, from...etc
I would like to "group" (but group by doesn't work) the rows by email address, and then eliminate that collection of rows if any row inside that collection contains a certain email address inside the to field.
I've tried:
select *
from emails
where emails.[to] != "test#email_address.com"
But that just eliminates individual rows, and not the collection of rows that share an email address.
Unfortunately using group by loses all information beyond the top level row and the database no longer can check to see if there are other rows with that email address in the to field.
Any thoughts?
You need a group by on email_address and a conditional check for any of the to email specified.
select email_address
from emails
group by email_address
having count(case when emails.[to] = 'test#email_address.com' then 1 end) = 0
If you need the entire row information for such email_addresses use
select * from emails where email_address in (
select email_address
from emails
group by email_address
having count(case when emails.[to] = 'test#email_address.com' then 1 end) = 0)

SQL command to clear specific text

Trying to run a SQL command that reads * from my database but I need it to seek specific text in the email field and return a null if it doesn't equal. Example, I'm trying to filter out all emails in our database that are not internal email addresses, so it would need to filter out any that don't have our company name in it.
I was able to filter this with a LIKE command, but it just simply ignores the rest of the fields in the results. I know access has the IIf(InStr command, so I'm hoping there is something similar in SQL
Not sure I fully understand, but something like this may work for you.
USE Server
SELECT
firstName AS EmployeeFirstname
, lastName AS EmployeeLastname
, title AS EmployeeTitle
, id AS EmployeeID
,
Case
When emailAddress Like '%#mycompany.com'
Then emailAddress
Else Null
End AS EmployeeEmailAddress
FROM db.Information
WHERE ISNUMERIC(id)<> 0
AND empStatus = 'A'
So what this will do is give you the rows regardless of what email they have (still taking into account your other selections of ISNUMERIC(id)<> 0 AND empStatus = 'A'). But in those rows, if the email is your company they would display and if not the field would be Null. Obviously change the “mycompany.com” it the Like to whatever string you actually need to search for,
If you want to exclude all records where the company is not in the email address, add
where
emailAddress not like '%companyname%'
If you want to blank out the non-company email addresses, you can use case
Select
case when emailaddress not like '%company%' then null else emailaddress end

Selecting all uppercased-value rows of a table in SQL Navigator

I have a table with an email address column. Some email addresses in the table contain uppercase letters. I would like to fetch all the rows with uppercase emails (in order to set them to lowercase). How do I select all the rows where the email address contains uppercase letters?
I believe Oracle is case sensitive by default? If so, then this should work:
SELECT *
FROM table_name
WHERE LOWER(email) <> email
If this works then you can simply update them with
UPDATE table_name
SET email = LOWER(email)
WHERE LOWER(email) <> email