My specific use case is that I want to sort a list of users by name; first name, last name. The user has a preferred name and a legal name. I want to order by the preferred name if it is present, but the legal name as fall back.
For example, given the follow table:
id | first_name | last_name | preferred_first_name | preferred_last_name
----+------------+-----------+----------------------+---------------------
9 | Ryan | Bently | Alan |
10 | Ryan | Do | Billy | Baxter
11 | Olga | Clancierz | |
12 | Anurag | Plaxty | | Henderson
13 | Sander | Cliff | Billy |
I want to sort like this:
Alan Bently
Anurag Henderson
Billy Baxter
Billy Cliff
Olga Clancierz
Normally, with just one name set of name fields I would just do this:
SELECT * from users ORDER BY users.first_name, users.last_name
What is the best way to order by preferred name fields when present, but fall back to other name fields when they are not present?
Try
ORDER BY COALESCE(users.preferred_first_name,users.first_name), users.last_name
Related
I can't give the actual table, but my problem is something like this:
Assuming that there is a table called Names with entries like these:
+--------------+
| name | id |
+--------------+
| Jack | 1001 |
| Jack | 1022 |
| John | 1010 |
| Boris | 1092 |
+--------------+
I need to select all the unique names from that table, and display them(only names, not ids). But if I do:
SELECT DISTINCT name FROM Names;
Then it will return:
+-------+
| name |
+-------+
| Jack |
| John |
| Boris |
+-------+
But as you can see in the table, the 2 people named "Jack" are different, since they have different ids. How do I get an output like this one:
+-------+
| name |
+-------+
| Jack |
| Jack |
| John |
| Boris |
+-------+
?
Assuming that some ids can or will be repeated(not marked primary key in question)
Also, in the question, the result will have 1 column and some number of rows(exact number is given, its 18,013). Is there a way to check if I have the right number of rows? I know I can use COUNT(), but while selecting the unique values I used GROUP BY, so using COUNT() would return the counts for how many names have that unique id, as in:
SELECT name FROM Names GROUP BY id;
+------------------+
| COUNT(name) | id |
+------------------+
| 2 | 1001 |
| 1 | 1022 |
| 1 | 1092 |
| 3 | 1003 |
+------------------+
So, is there something to help me verify my output?
You can use group by:
select name
from names
group by name, id;
You can get all the distinct persons with:
SELECT DISTINCT name, id
FROM names
and you can select from the above query only the names:
SELECT name
FROM (
SELECT DISTINCT name, id
FROM names
)
What I mean is the following.
I have a database containing people. These people van a name and a role.
It looks like this:
-----------------------------
| (PK) id | name | role |
-----------------------------
| 1 | ben | fireman |
| 2 | ron | cook |
| 3 | chris | coach |
| 4 | remco | barber |
-----------------------------
Ive created a searchbar where you can search for people in the database. When you press search, it looks for name and roles, for example:
When I type in 'co', the result I get is:
-----------------------------
| (PK) id | name | role |
-----------------------------
| 3 | chris | coach |
| 4 | remco | barber |
| 2 | ron | cook |
-----------------------------
This is because its looking for matches in the name and role column.
The query I use is:
SELECT * FROM people WHERE name LIKE '$search' OR role LIKE '$search' ORDER BY name";
The only issue with this is that it just order by name.
I want it to first order every result from the name column by name and then order every remaining result from the role column by name, so it ends up looking like this:
-----------------------------
| (PK) id | name | role |
-----------------------------
| 4 | remco | barber | <- 'co' found in name column, ordered by name
| 3 | chris | coach | <- 'co' found in role column, ordered by name
| 2 | ron | cook | <- 'co' found in role column, ordered by name
-----------------------------
How can I do this?
Edit: $search is the output from the searchbar
Use a case expression to put the 'co' names first:
order by case when name LIKE '$search' then 0 else 1 end, name, role
I currently have a situation where I will have 2 tables:
+--------------+-----------------+-----------------+-----------------+
| OriginalID | NewID | FirstName | lastName |
+--------------+-----------------+-----------------+-----------------+
| 123456 | | billy | bob |
| 234567 | | tommy | smith |
| 987654 | | sarah | anders |
+--------------+-----------------+-----------------+-----------------+ etc etc
and
+--------------+-----------------+
| OriginalID | NewID |
+--------------+-----------------+
| 123456 | 1111111 |
| 234567 | 1111112 |
| 987654 | 1111113 |
+--------------+-----------------+
Without going in-depth into the process itself, I take a record from the first table and insert it into a different system, which gives a record in the form of the second table (generates a custom ID for it).
What I want to do is for every record in the second table, take the NewID and place it into the row with the same OriginalID in the first table (so that it looks like this:
+--------------+-----------------+-----------------+-----------------+
| OriginalID | NewID | FirstName | lastName |
+--------------+-----------------+-----------------+-----------------+
| 123456 | 1111111 | billy | bob |
+--------------+-----------------+-----------------+-----------------+
As a note, the only values I care about dealing with are the OriginalID and NewID, none of the other values are needed. This will happen for multiple tables with different names, so I have given a generic example. For this example the tables can be called
ContactRecords (first table) and NewContact (second table)
I have read over several examples on SO about this type of problem, but none of them quite fit the solution I'm looking for.
Thanks in advance!
This looks like a join update which we had here many times.
update old
set NewID = new.NewID
from ContactRecords as old
inner join NewContact as New on new.OriginalID = old.OriginalID
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have spent hours researching how to write the proper SQL for the following task, and finally I feel that I have to get some help as this is the most complex SQL query I have ever had to write :(
I am putting together an email list with all the email adresses that we have on our customers.
I have two tables: one customer table that contains customer level information, and one contact person table that contains person level information. Most of the data is overlapping, so the same email adress will occure in both tables. But the email adress field can be empty in both tables, and I do not want to return any empty rows.
Users that buy in our physical store are often only registered in the customer level table, but users that buys online are always registered both in the customer level table and the person level table.
I want to create a full list where I get all email adresses, where all email adresses are unique, no email adresses are duplicates and no email adresses are null.
Also I want to join in columns from the customer table when the data is retrieved from the person table (the zip code in my example below).
Customers
| CustomerID | Firstname | Lastname | Email | Zipcode |
| 22 | Jeff | Carson | jeffcar#mail.com | 81712 |
| 29 | John | Doe | null | 51211 |
| 37 | Gina | Andersen | null | 21147 |
| 42 | Brad | Cole | brad#company.org | 39261 |
Contact persons
| PersonID | CustomerID | Firstname | Lastname | Email |
| 8712 | 22 | Jeff | Carson | null || 8916 | 29 | Jane | Doe | jane#doe.net || 8922 | 29 | Danny | Doe | null |
| 9181 | 37 | Gina | Andersen | gina#gmail.com |
| 9515 | 37 | Ben | Andersen | ben88#gmail.com |
I want to join the tables to generate the following:
Final table
| PersonID | CustomerID | Firstname | Lastname | Email | Zipcode |
| 8712 | 22 | Jeff | Carson | jeffcar#mail.com | 81712 |
| 8916 | 29 | Jane | Doe | jane#doe.net | 51211 |
| 9181 | 37 | Gina | Andersen | gina#gmail.com | 21147 |
| 9515 | 37 | Ben | Andersen | ben88#gmail.com | 21147 |
| null | 42 | Brad | Cole | brad#company.org | 39261 |
I guessed this would be a fairly common task to do, but I haven't found anyone with a similar question, so I put my trust in the expertise out there.
This SQL will get you exactly the results table you were looking for. I've made a live demo you can play with here at SQLFiddle.
SELECT
ContactPerson.PersonID,
Customer.CustomerID,
COALESCE(ContactPerson.FirstName, Customer.FirstName) AS FirstName,
COALESCE(ContactPerson.LastName, Customer.LastName) AS LastName,
COALESCE(ContactPerson.Email, Customer.Email) AS Email,
Customer.ZipCode
FROM Customer
LEFT JOIN ContactPerson
ON ContactPerson.CustomerID = Customer.CustomerID
WHERE COALESCE(ContactPerson.Email, Customer.Email) IS NOT NULL
Results (identical to your desired results):
| PersonID | CustomerID | FirstName | LastName | Email | ZipCode |
| 8712 | 22 | Jeff | Carson | jeffcar#mail.com | 81712 |
| 8916 | 29 | Jane | Doe | jane#doe.net | 51211 |
| 9181 | 37 | Gina | Andersen | gina#gmail.com | 21147 |
| 9515 | 37 | Ben | Andersen | ben88#gmail.com | 21147 |
| NULL | 42 | Brad | Cole | brad#company.org | 39261 |
A quick explanation of some key points to aid understanding:
The query uses a LEFT JOIN to join the two tables together. JOINs are pretty common once you get into SQL problems like this. I won't go into an in-depth explanation here: now that you know what they are called you should have no trouble Googling for loads of info on them!
NB: COALESCE basically means 'the first one of these options which isn't null' (docs). So this query will grab their name and email address from ContactPerson IF POSSIBLE, otherwise from Customer. If NEITHER of these tables hold an email address, then the WHERE clause makes sure that record isn't included at all, as required.
This will work:
SELECT b.PersonID
,a.CustomerID
,a.FirstName
,a.LastName
,COALESCE(a.Email,b.Email) AS Email
,a.ZipCode
FROM Customers a
LEFT JOIN Contact b
ON a.CustomerID = b.CustomerID
WHERE COALESCE(a.Email, b.Email) IS NOT NULL
Demo: SQL Fiddle
select con.personid,
con.customerid,
con.firstname,
con.lastname,
coalesce(con.email, cus.email) email,
cus.zipcode
from contact_persons con
right join
customers cus
on con.customerid = cus.customerid
Consider the following table structure with data -
AdjusterID | CompanyID | FirstName | LastName | EmailID
============================================================
1001 | Sterling | Jane | Stewart | janexxx#sterlin.com
1002 | Sterling | David | Boon | dav#sterlin.com
1003 | PHH | Irfan | Ahmed | irfan#phh.com
1004 | PHH | Rahul | Khanna | rahul#phh.com
============================================================
Where AdjusterID is the primary key. There are no. of adjusters for a company.
I need to have a query that will list single adjuster per company. i.e. I need to get the result as -
========================================================
1001 | Sterling | Jane | Stewart | janexxx#sterlin.com
1003 | PHH | Irfan | Ahmed | irfan#phh.com
========================================================
If any one could help me that will be great.
One way:
SELECT * FROM Adjusters
WHERE AdjusterID IN(SELECT min(AdjusterID)
FROM Adjusters GROUP BY CompanyID)
There are a handful of other ways involving unions and iteration, but this one is simple enough to get you started.
Edit: this assumes you want the adjuster with the lowest ID, as per your example
I know the answer from Jeremy is a valid one, so I will not repeat it. But you may try another one using a so called tie-breaker:
--//using a tie-breaker. Should be very fast on the PK field
--// but it would be good to have an index on CompanyID
SELECT t.*
FROM MyTable t
WHERE t.AdjusterID = (SELECT TOP 1 x.AdjusterID FROM MyTable x WHERE x.CompanyID = t.CompanyID ORDER BY AdjusterID)
It could be better performance-wise. But even more useful it is if you had another column in the table and you wanted to select not just one for each company but the best for each company using some other column ranking as a criteria. So instead of ORDER BY AdjusterID, you would order by that other column(s).