Replicating a VLOOKUP (with column insertion) in MS SQL? - sql

I am trying to replicate a VLOOKUP I have done in Excel in SSMS. I have two tables - table 1 is an application master listing, table 2 is an application listing. I want to simply verify that every column in table 1 has an identical match in table two. I also want to have my results populate with an inserted column, column B with the results.
I have already done a very straight forward VLOOKUP in Excel with a 1:1 match. I now have Col A with the application master listing names, and then the new lookup column B with the 1:1 match.
Does anyone know how to go about this in SQL?

Related

Is there a way to reewrite postgresql selects to include multiple table records?

I'm working with a multi-tenant application that has one schema per company using postgreSQL, each company can only access its own schema, but we also have a admin company that should see the data from all the other companies.
Example:
company1.table
Column A
Column B
Cell 1
Cell 2
company2.table
Column A
Column B
Cell 3
Cell 4
admin.table
Column A
Column B
company_id
Cell 1
Cell 2
1
Cell 3
Cell 4
2
I want the ORM to select from admin.table directly but with some intermediate step that could iterate all schemas replacing the current_query() to reference each schema and then applying 'union all' in the end of each iteration.
I tried using views but things started to get extremely slow due to indexing lost, same thing with inherited tables
I also tried materialized views, but by doing that i'm not able to create triggers for insertion.
If there is a way of doing this, what would be the best approach?

Picking column names from an excel sheet in sql server while writing a macro

I am writing a macro for automating some task where i need to pick column names from an excel sheet , use that columns in select statement in sql and then fetch the values for those columns from database table. is there any way through which i can acheive this.
e.g.
in my DB there is a table employee having column names as - empname,empadd,empcontact ,empsal,dep,branch,location and 50 more columns.
my excel sheet is having 3 column names empname,empadd,laction.
now i want my code to pick only those 3 columns which are in excel and fetch the values from employee database.
i.e. select empname,empadd, location from employee;
i tried multiple things but no luck.
it would be great if anyone can suggest some way. thanks

How to replicate records using PLSQL in two tables

I have two existing tables and many records already added.
formula(formulaId,formulaName,formulaType)
and formula_detail(detailId,formulaId,fieldType,value)
Now there is change in formula table and new column is being added , branchId as
formula(formulaId,formulaName,formulaType,branchId),
and branch table is branch(branchId,branchName)
I want to copy and paste every existing record in formula table for every branch.
e.g if there are 3 existing records in formula table (with ID 1,2,3) and 2 branches. Then copy paste operation should produce total new (3*2)=6 records in formula table and also replicate records in formula_detail table for every newly created formula as follows
for formulaId 1 , If there were 5 records in formula_detail table, then copy paste in formual_detail table will have (2*5) new records added in formual_detail table.
I tried some solutions but number of are records huge and script is taking time. Please help. If need any test code I can add.
First of all replicating same column in detail table is against the Normalized Form used in your Data model.
Still If you want to add the column anyway,
Add the column using ALTER statement in formula_detail table
Try using this Merge statement
.
MERGE INTO formula_detail fd
USING (SELECT formulaId, branchId from Formula) temp
ON (fd.formulaId=temp.formulaId)
WHEN MATCHED THEN
UPDATE SET fd.branchId=temp.branchId;

Combining different tables in excel by looking up values

Okay, so let's say I have two tables in Excel. I want to create a third table that looks up data from the other two and combines them. I'll explain what I mean
Here is my first table:
It has a number, which is like a primary key, and a corresponding name for each entry.
I also have a second table:
This one just contains some random info, and there's numerous entries. The Client Name is not listed here, but it does have the key for the client that each entry corresponds to.
I want to create a third table that shows me all the Client Info data, but replaces the key with the actual client name. So basically, wherever it sees a key of "1", it'll look that up from the first table and find "Comet" instead.
The desired table would look like this:
What kind of formula would I need to pull data from one table based on a value? Note that all my tables are in different worksheets.
Assuming that the three tables are in Sheet1, Sheet2, Sheet3, this is the formula you need in Sheet3, cell A2
=vlookup(Sheet2!A2,Sheet1!$A2$b$6,2,false)
Copy down as many rows as there are rows in Sheet2
If you need the client info from Sheet2, use this in Sheet3, cell B2 and copy down
=sheet2!B2
As you put an "SQL" tag, I assume that you are also looking for an SQL based answer. So in addition to #teylyn suggestion, you can use the following SQL query if you are working with SQL databases:
SELECT table1.Client_Name, table2.Client_Info
FROM table1
RIGHT JOIN table2 ON table1.ID_Number = table2.ID_Number
Here is what this query does:
The RIGHT JOIN will return all rows from the right table (here table2), with the matching rows in the left table (here table1) according to the condition specified by the ON clause (here, if the ID Number is the same). Then, the SELECT clause will return a table with ONLY the columns specified after the keyword SELECT.

Creating a join list between two tables VBA

Good afternoon,
I am still quite a novice with VBA but am trying to create a loop that will be able to sift through a long list of data within a given column (in my case, both tables have one common identifier, the system ID) and if a system ID is matched in one column with a column from the other table, then a new sheet is created that combines all of the rows associated with both sets of data into one row.
For example, if my data looked like this:
Table 1
Column A, Column B, Column C |
ID, Name, Birthday
Table 2
Column A, Column B, Column C|
Purchase, Amount, ID
And I had the same ID in both Tables 1 and 2, for each match, I would like to have all rows associated with the match joined together.
This would really enable me to speed things up with organizing information, so I was not sure if it would be possible... Any Ideas are welcome!
since excel is not a database program like access, you can not use sql-like joins natively. you would have to program your own join function:
(Since i do not have MS Office installed, i can only give you pseudo-code)
for each-loop going through IDs of Table1
for each-loop going through IDs of Table2
if(Table1.ID = Table2.ID) then
copy data of Table1 into a new sheet
copy data of Table2 into the same sheet, next to Table1 data
PS: i assume you use excel because of the vocabulary (column, worksheet,..)