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

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

Related

Power BI Compare two tables and display missing rows from the first table

I am facing a challenge in powerBI. I have two different tables that have hundreds of rows in them. These two tables are connected by a common column (Name). I want to compare two tables on the attribute name and then display the rows of table-1 whose names are not present in Table-2. It would be great if anyone can help me in this regard.
My data looks like this:
Table 1
Name | Occupation | Address
Joe | Teacher | Dallas
Ann | Hunter | Houston
Saly | Artist | Novi
Table 2
Name | Marital Status | Phone
Saly | Married | 3132485889
Niky | Single | 9831773846
Desired Output
Name | Occupation | Address
Joe | Teacher | Dallas
Ann | Hunter | Houston
You need to filter for blanks in the "Name" column of the second table. See the below screenshot.
Create a grid which has all the columns from both tables. "Name 2" is the Name column from the second table. In the "Filters" pane, for "Name 2" column, choose "Blank".

How can I separate out multiple values from a column via another table?

I have a table named Team. Each record can have more than one member. Rather than jamming members comma separated value, I am hoping to use separate table to meet the requirement. Id is primary key.
----------------------------
| Id | Name | Member
----------------------------
| 1 | TeamA | Jim, Jack
----------------------------
| 2 | TeamB | Micah
I thought of creating a table named User. UserId is the PK and TeamId is FK.
--------------------------
| UserId | Name | TeamId
--------------------------
| 123 | Jim | 1
--------------------------
| 456 | Jack | 1
--------------------------
| 789 | Micah | 2
Then I want parent table Team be able to refer Jim and Jack from its child table but I am not sure how to represent it in one to many relationship... I know below expression is not correct....
------------------------
| id | Name | Member
------------------------
| 1 | TeamA | 123, 456
(Table) Team:
TeamId(PK), Name
(Table) Member:
MemberId(Pk), Name
Case 1 (one to many):
Every Member can be in one team:
Simply add TeamId(FK) to Member Table
Case 2 (many to many):
Every Member can be in as many teams as you want:
Add a linking table:
(Table) TeamMember:
TeamId(FK), MemberId(FK)
That indeed means to normalize data further meaning to split tables your data isnt atomic at each column level. Hence should be normalized to avoid difficulties in data retrievel.
Users Table
User Id(pk) TeamId(fk)
Members Table
TeamId(pk) , MemberId, NAME
Query
Select
u.userid, memberid, m.namefrom
users u join members m
On u.userid=m.memberid and
u.teamid=m.teamid
It looks like you need to create a mapping table to represent the Team, Member relationship, which would make this a many-to-many relationship.
TeamMemberMap - TeamId, MemberId

“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

Insert SQL Server row into specific position in formatted table

I'm new at creating database and currently trying to accomplish something that is really necessary for me.
Lets say I have a Database "Customer" with 300 rows all with a unique identifier called Id_.
Id_ | Customer | Postal | Country |
200 | Mica Sa. | 99582 | USA
201 | Shum Jr. | 10258 | USA
202 | Carl Ro. | 45697 | USA
203 | Brad Mi. | 24761 | USA
If i delete a row number 202 using:
DELETE FROM Customer
WHERE Id_ = 202;
I Get:
Id_ | Customer | Postal | Country |
200 | Mica Sa. | 99582 | USA
201 | Shum Jr. | 10258 | USA
203 | Brad Mi. | 24761 | USA
But when I try to insert a row using:
INSERT INTO Customer (Id_, Customer, Postal, Country)
VALUES (202, 'Peter R.', 08574, 'USA');
I get the row randomly inserted in the database, so my question is how do I insert this row exactly after 201(Id_) and before 203(Id_)?
To help you clear some things up:
ID field seems to be int and not unique identifier
Insert statements are not made randomly in the DB, they go to the last record. E.G if you have 201,203 and you insert 202 it will go after 203.
The way you select the records (and thus they fetched and displayed) is another thing. You may run a query that return 202 before 203 but this doesn't mean that this is the way that are stored in the DB
If ID are actually of type int I recon you make them auto incremental
Select * from Customer Order by Id_ Desc

UNIQUE - way to have unique rows in table?

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