UNIQUE - way to have unique rows in table? - sql

I have problem with unique rows in db table, now it is posible to do that:
id | Name | LastName | City
-------------------------------------
1 | John | Moore | London
2 | John | Moore | London
when i use UNIQUE attribute in all columns i have errors inserting second Moore even it is different Name :/
how use UNIQUE (or maybe INDEX?) to do something like that in my table in db:
id | Name | LastName | City
-------------------------------------
1 | John | Moore | London
2 | Jake | Moore | London
3 | John | Keen | London
4 | John | Moore | London //but good error when inserting the same row
Sorry if question is easy, but i am beginner at sql, and have problems with find some good example with using a UNIQUE like a want:/
or maybe I must just before inserting a new row selecting a table from db and check if it exist?

Remove the unique index on the individual column and make it on both columns together, like this:
CREATE UNIQUE INDEX ixFullName ON yourTable (LastName, Name);

Related

“Is there an Access SQL code/query for concatenating first letter plus a unique ID number and insert into a new column? [duplicate]

This question already has answers here:
Customized Auto-Number IDs for tables?
(2 answers)
Closed 3 years ago.
First of all, I am quite new to SQL and Microsoft Access.
I am setting a database in Access. My database collects information from four different departments. I store my data through forms. My main table (Business) stores information (department) using a Combo Box saving a number instead of text.
I want to have a column (similar to CODE ID already available in the table above) which shows the initial letter from a field (name Department) + a number.
Ie. In table "Business", I want to display a Code ID which contains the initials of column Department plus a number code (department order number ascending). I want to have this every time i add information.
+===============+=================+=========+==+
| DEPARTMENT | PARTNER | CODE ID | |
+===============+=================+=========+==+
| Data_Analysis | John Doe | D001 | |
+---------------+-----------------+---------+--+
| Marketing | Jane Doe | M001 | |
+---------------+-----------------+---------+--+
| Finance | Alex Mustermann | F001 | |
+---------------+-----------------+---------+--+
| Operations | Juan Perez | O001 | |
+---------------+-----------------+---------+--+
| Finance | Barack Trump | F002 | |
+---------------+-----------------+---------+--+
| Finance | Mark Merkel | F003 | |
+---------------+-----------------+---------+--+
| Marketing | Peggy Hilton | M002 | |
+---------------+-----------------+---------+--+
| Operations | Max Mustermann | O002 | |
+---------------+-----------------+---------+--+
| Operations | | OXXX | |
+---------------+-----------------+---------+--+
The values in column CODE ID are those I would like to have display every time I add a new row (new department order). I need this type of code for tracking my number of orders in each department and use it as a unique code for any inquires with partners. I dont want to have it as the primary key id.
Thanks in advance!
If you rethink the schema slightly it becomes trivial; instead of having the column with the ID and code combined, just keep a running count when inserting:
INSERT INTO business(department, name, code) SELECT Forms!Department, Forms!Name, COUNT(*)+1 FROM business WHERE name=Forms!Name
Then when you pull the information out:
SELECT department, name, LEFT(1, department) & code

How to match entries between datasets?

For Example, I have a dataset like this:
| People | ID |
|-------------|-----|
| John Smith |A1234|
| John Doe |A1235|
| Jane Doe |A1236|
| John Smith |A1237|
And I also have another dataset like this:
| People | Company | City | Rank |
|-------------|---------|--------|-------|
| John Smith | XXX |New York| 1 |
| John Doe | YYY |London | 2 |
| Jane Doe | ZZZ |Seoul | 3 |
| John Smith | WWW |Tokyo | 4 |
I want to find the company of each people in the first table, using the information in another table. Note there're people with the same name (though few) in the second (and also the first) tables, so we need other columns for assistance.
Is it necessary to import two tables in one project? The reality is I have multiple tables providing possible name / company matchings, but they have little similarity (i.e. different dataset provides entirely different information) other then each dataset have name and company rows.
You need to create two separate OpenRefine projects and join them using the cell.cross function. You can also see this tutorial for joining two projects in OpenRefine
cell.cross performs the equivalent of a database join. You will need a unique identifier common to your two projects for the function to match the records, otherwise, OpenRefine will return the first match.

SQL Help - Insert into a table, data from two different tables, based on a reference table

I have a four tables I am working with. One of the tables is empyt (ATM) with three columns (ID, Cust_AcctID, Brch_CtyID) and I would like to insert data into the Cust_AcctID and Brch_CtyID columns of this table.
ID | Cust_AcctID | Brch_CtyID
The second table (Cust_Acct) also has three columns (ID, Customer and Account)
The Customer and Account columns hold string data. The ID value in this column is what I am looking to have inserted into the ATM table in the Cust_AcctID column
ID | Customer | Account
1 | John Doe | Checking
2 | John Doe | Saving
3 | Jane Doe | Checking
4 | Jane Doe | Plan24
The Third table (Brch_Cty) has three columns as well (ID, Branch and City)
The Branch and City columns hold string data. The ID value in this column is what I am looking to have inserted into the ATM table in the Brch_CtyID column.
ID | Branch | City
1 | Main Branch | New York
2 | Second Branch | New York
3 | Main Branch | Chicago
4 | Uptown Branch | Detroit
The fourth table is a reference table that holds all the valid combinations Of (Customers, Account) and (Branch, City). All data in these columns are strings.
Customer | Account | Branch | City
John Doe | Checking | Main Branch | New York
John Doe | Savings | Second Branch | New York
John Doe | Checking | Uptown Branch | Detroit
Jane Doe | Checking | Uptown Branch | Detroit
Using the data from table 4, I would like to insert into the ATM table the the data from the ID column from the Cust_Acct Table into the Cust_AcctID column where it matches the data in the fourth table. The same goes for the Brch_Cty table ID to be inserted into the Brch_CtyID column
So the ATM table should look like this
ID | Cust_AcctID | Brch_CtyID
1|1|1
2|2|2
3|1|4
4|3|4
Could you please help me with building a SQL statement for this. I am really stuck figuring this one out. Thanks for any help given.
Can you try the following :
Select ca.ID,bc.ID
From dbo.temp t Inner join dbo.Cust_Acct ca on ca.Customer=t.Customer AND
ca.Account=t.Account
inner join dbo.Brch_City bc on bc.Branch=t.Branch AND bc.City=t.City
Where ca.Customer=t.Customer AND ca.Account=t.Account AND bc.Branch=t.Branch
AND bc.City=t.City
Hope this helps. :)

How do you conditionally order by multiple fields in postgres

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

Need select query

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).