I have 500 records in Excel and it contains some columns which is same as the columns in SQL DB Table. I have old column value
as well as new column value in excel.
Example
Excel
Name Age OldEmployeeID NewEmployeeID
x 1 100 200
y 2 101 201
z 3 102 202
SqlTable
EmployeeTable
Name Age Department City EmployeeID
x 1 HR x 100
a 4 HR x 103
y 2 Admin x 101
b 5 Finance x 104
c 3 IT x 102
I want to update EmployeeID column in SQL Table as NewEmployeeID which is there in excel.
Can anyone suggest how to write a sql query to update the sql table.
Assuming the column is not an ID Column or having some other constraint you can:
Option 1:
Assuming your table is similar to the screenshot you have a concatenated column with this formula and drag it down and copy it directly into SQL.
=CONCATENATE("UPDATE dbo.EmployeeTable SET EmployeeID = ",D2," WHERE EmployeeID = ",C2,";")
Option 2:
You can import the Excel file into SQL and use a statement like the one below. However, be cautious if any of the old EmployeeIDs overlap the new EmployeeIDs. For Example if Jim has ID 100 and his new one is 500 and Jane's new ID is 100 and if you accidentally run it a second time, the Jane will also get an ID of 500.
UPDATE EmployeeTable SET EmployeeID = Excel.NewEmployeeID
FROM
EmployeeTable
JOIN ExcelTable
ON Excel.OldEmployeeID = EmployeeTable.EmployeeID
;
Related
Hello I don't know how to do changes of my MS SQL DB to integrate it to work with new software.
The case is related with software limitations.
Our old software can write, read and work with MS SQL DB with multiple tables but the new software understand only from one table or one view table.
My question is how can I edit my MS SQL DB form 20 tables to do it to be one DB with one table with all data from 20 tables and columns without data lost?
And one last question is true about View Tables in MS SQL that they are read only for applications and software?
Why is it not a good idea to put data from all your 20 tables into one table ?
I will try to explain with an example, since I do not know your database I just think of some tables here
suppose you have a table Clients
ClientID Name Street City
1 John ChuchStreet Denver
2 Anna FlowerStreet Boston
and a table Products
ProductID Name Price
1 Mouse 10
2 Keyboard 30
3 Usb Cable 10
and table Orders
OrderID OrderNumber CLientID TotalAmount
1 123 1 10
2 345 1 20
3 678 2 30
and finally table OrderDetail
OrderDetailID OrderID ProductID Quantity
1 1 1 1
2 2 1 1
3 2 3 1
4 3 2 1
Now to put this into one table, you could do this
ID ClientName ClientStreet ClientCity OrderNumber TotalAmount ProductName ProductPrice ProductQuantity
1 John ChurchStreet Denver 123 10 Mouse 10 1
1 John ChurchStreet Denver 345 20 Mouse 10 1
2 John ChurchStreet Denver 345 20 Usb Cable 10 1
3 Anna FlowerStreet Boston 678 30 Keyboard 30 1
Now you can already see the redundancy,
you need to repeat the address of each customer, time and time again in your table
you need to repeat the ordernumber and total amount time and time again
you need to repeat the productname and price time and time again
Now suppose that John moves to another address, now you have to search for John in every row in the table, and adjust the address
Now suppose a productname changes, again you have to search all rows and update
That is lots of work, very inefficient, and guaranteed to go wrong at some point
Now I only used 4 tables in this example, can you image what will happen if you would merge 20 tables into 1 ?
And the redundancy is not your only problem, what if you want to look at a client, what row should you use ?
What if you want to look at an order, what row should you use ?
What if you want to look at a product, what row should you use ?
In this one table design, you cannot identify a single row for customer, or order anymore. That is because each row contains everyting, there is no distinct row anymore for a customer, or a product, or an order...
Merging all tables into one big table is simply not possible to maintain
Hi thank you for your answers!
I am trying to integrate old ms sql db to new IDFLOW Software.
This software understand from ms sql db but only from one table but my old db contains 18 tables....
IDFLOW understand from views, but it is not good to work with views, they are okay for card design in IDFLOW, but views are not okay to write new data in db from IDFLOW interface , because in views it is not possible to write new data in db!
And now I started to think to create one db with one table with all columns, and I completed it successfully!
Now in my sql server I am with two db, old db and new db.
Now I don't know how to export data from old db and import it to new db?
I am talking about export column2 from table1 from db1 and import it in column2 from table1 from db2 ....
and so and so ........... column2 from table2 from db1 and import it in column3 from table1 from db2 ............
and so and so .... column 5 from table18 from db1 and import it in column10 form table1 from db2.
Is it possible to do that?
Okay, but IDFLOW works only with one table...
And I asked Jolly and they say utilize your DB for one table.
IDFLOW is a software for ID CARDS .
I think it is possible to use one db with one table and all columns for employee information.
The goal is.. in IDFLOW enter all data for new employee, when you enter data from IDFLOW interface they go into MS SQL DB and all fields from DB are in card design configured, and when you select records for one employee from IDFLOW interface (from db) you can print ID CARD with all data for employee.
The question is how to migrate old records from old db in new.
It is not big database, it is only 6 GB from 2006.
And we use it only for printing id cards.
I have table like below,
Id(Pk) User_Id C1 C2 C3 C4
1 111 2 a b c
2 111 5 d e f
3 111 7 a f ty
4 222 2 a b c
5 222 5 d e f
6 222 7 a f ty
This table almost having 10L records. And each user_id having almost 10k Records. If I am fetching details by User_id, it is getting almost 5 mins to get the details. Where i have to tune this?
Im using below query
Select * from User where user_id ='111'
And total number of columns in this table is around 130 columns.
Assuming you have a right index defined on (User_id) & just select the column which you actually want so, i would do with backed SSMS:
SET NOCOUNT ON
SELECT User_Id, C1, C2, . . .
FROM User
WHERE user_id = 111;
If, relative index is not present with user_id then you have to pay with poor performance.
Note : If user_id has numeric type, then you don't need to use ''.
If you PK on the Id column is a clustered index, change it to non-clustered and create a clustered index on the User_Id column.
This will most likely make SQL Server use a partial table scan, and as all requested records will be saved "close to eachother", this might reduce the number of page reads required to retrieve the values (which can be "grabbed" from the disk without further criteria checks).
I'm attempting to determine the best way, either in SSIS or straight TSQL, to merge two rows based on a given key, but taking specific data from each row based on various aggregate rules (MAX and SUM specifically). As an example, given the following dataset:
Customer Name Total Date Outstanding
12345 A 100 7/15/2015 500
12345 200 1/1/2015 300
456 B 500 1/2/2010 100
456 B 250 2/1/2015 900
78 C 100 9/15/2015 500
I wish to consolidate those to a single row per customer key, with the following rules as an example:
If any name is null, use a corresponding value for that customer that is not null
MAX(Total)
MAX(Date)
SUM(Outstanding)
The result set would be:
Customer Name Total Date Outstanding
12345 A 200 7/15/2015 800
456 B 500 2/1/2015 1000
78 C 100 9/15/2015 500
What's the best approach here? My first instinct is to query the table to join to itself on customer to get all values on a single row, and then use formulas in a Derived Column task in SSIS to determine the values to use. My concern there is that is not scalable - it works fine if I have a customer occur only twice in the main dataset, but the goal would be for the logic to work for N number of rows without needing to do a ton of rework. I'm sure there's also a TSQL approach that I'm missing here. Any help would be appreciated.
If name column in your query is not empty then you can do that simply by using aggregate function in one query
DECLARE #Customer TABLE
(
Customer INT, Name varchar(10), Total INT , PurchaseDate DATE , Outstanding INT
)
INSERT INTO #Customer
SELECT 12345,'A',100,'7/15/2015',500 UNION
SELECT 12345,'A',200,'1/1/2015',300 UNION
SELECT 456,'B',500,'1/2/2010',100 UNION
SELECT 456,'B',250,'2/1/2015',900 UNION
SELECT 78,'C',100,'9/15/2015',500
SELECT Customer,NAME ,MAX(Total), MAX(PurchaseDate), SUM(outstanding)
FROM #Customer
GROUP BY Customer, NAME
Demo
Now, if your name column is empty in few cases like you have mentioned in your example then you can update name table with correct name value
Somehow I have a feeling that this should be an easy one - but I cannot see how to fix this in a SSIS package.
I have two tables: Order and CustomerMapping. My CustomerMapping table is not containing all my customers, but only customers that have changed Id and CustomerName.
The definitions of the tables are:
Order:
Id (int)
CustomerId (int)
CustomerName (nvarchar(50))
CustomerMapping:
Id (int)
ObsoleteId (int)
CustomerName (nvarchar(50))
ObsoleteCustomerName (nvarchar(50))
Data in Order table is:
Id CustomerId CustomerName
1 100 Customer 1
2 101 Customer 2
3 102 Customer 3
Data in CustomerMapping:
Id ObsoleteId CustomerName ObsoleteCustomerName
20 100 New Customer 1 Customer 1
21 101 New Customer 2 Customer 2
I want to use the tools provided by the SSIS package to create what I would do like this in SQL:
SELECT o.Id,
CustomerId = ISNULL(cm.Id, o.CustomerId),
CustomerName = ISNULL(cm.CustomerName, o.CustomerName)
FROM Order AS o
LEFT JOIN CustomerMapping AS cm ON o.CustomerId = cm.ObsoleteId
The result of above query is
Id CustomerId CustomerName
1 20 New Customer 1
2 21 New Customer 2
3 102 Customer 3
I now want to take this result-set and save into a new table.
I know that I can just take the above SQL and do what I want (which might be what I end up doing), but somehow I believe I can make a lookup, merge and/or merge join... But I must admit I cannot really see how I do this without splitting the lookup into two and then having to save from each new "thread".
The above is rather simplified... I have 4 columns which I have to compare and then do some other stuff with the whole lot, before saving it into a new table again, which is why I want to keep a single "thread".
Edit: image added:
I have a table which have the following fields
Sno StockDate PartNo Quantity Location
1 22.11.2013 011011011 100 Perungudi
2 24.12.2013 011201120 1000 Kottivakkam
3 1.1.2014 011011011 80 Perungudi
If i need to select datas on 1.1.2014 means i have to list the following datas
Sno StockDate PartNo Quantity Location
2 24.12.2013 011201120 1000 Kottivakkam
3 1.1.2014 011011011 80 Perungudi
that is the stock available on that day is as showed above. i want to know how to i write the select query