Alter a column with data to an existing table - sql-server

I have an SQL table illustrated below but with much more rows (around 500):
Current Table
+----------------+--------+
| EmployeeID | EMP |
+----------------+--------+
| 01 | val |
+----------------+--------+
| 02 | val |
+----------------+--------+
| 03 | val |
+----------------+--------+
| 04 | val |
+----------------+--------+
and an following Excel sheet with the same data except the additional OtherID column:
Excel Sheet
+------------+----------------+--------+
| OtherID | EmployeeID | EMP |
+------------+----------------+--------+
| 001 | 01 | val |
+------------+----------------+--------+
| 002 | 02 | val |
+------------+----------------+--------+
| 003 | 03 | val |
+------------+----------------+--------+
| 004 | 04 | val |
+------------+----------------+--------+
How can I update the original table (without deleting and creating a new one) by adding the missing OtherID column and make sure that the OutherID is related to the EmployeeID?
Desired result in SQL
+------------+----------------+--------+
| OtherID | EmployeeID | EMP |
+------------+----------------+--------+
| 001 | 01 | val |
+------------+----------------+--------+
| 002 | 02 | val |
+------------+----------------+--------+
| 003 | 03 | val |
+------------+----------------+--------+
| 004 | 04 | val |
+------------+----------------+--------+
Thanks.

1) Insert Excel Sheet Data to Dummy Table. (ex: BulkInsert)
2) Use the next code:
Alter table Originaltable
ADD OtherID datatype;
go
UPDATE Originaltable
SET Originaltable.OtherID = DummyTable.OtherID
FROM OriginaltableINNER JOIN DummyTable
ON Originaltable.id = DummyTable.id

Related

How can generate one record into two records and then insert them into the table?

I have PKT table in sql server with structure(PKT(pso, day, msch, mskh, tkc, tkdu, st, mskh1)) and an other table(dong_bo(pso, day, msch, mskh, tk, psn, psc)).
With each row in PKT, i need to generate it to two rows then insert them into dong_bo table.
For Example: PKT table.
| pso | day | msch | mskh | tkc | tkdu | st | mskh1 |
| --- | --- | ---- | ---- | --- | ---- | -- | ----- |
| PKHT000105-12-20 | 31/12/2020 | 03 | 0802345 | C1411 | N3311 | 20000 | 7101259|
dong_bo table:
pso
day
msch
mskh
tk
psn
psc
PKHT000105-12-20
31/12/2020
03
0802345
1411
0
20000
PKHT000105-12-20
31/12/2020
03
7101259
3311
20000
0
I need a statement to generate it in SQL server.
Basically you can use union all statement as approach:
insert into dong_bo
select pso, day, mskh, right(tkc, 4) tkc, 0 psn, st psc from PKT
union all
select pso, day, mskh1, right(tkdu, 4) tkc, st psn, 0 psc from PKT;
Check this SQL online
Result:
+==================+============+=========+======+=======+=======+
| pso | day | mskh | tkc | psn | psc |
+==================+============+=========+======+=======+=======+
| PKHT000105-12-20 | 31/12/2020 | 0802345 | 1411 | 0 | 20000 |
+------------------+------------+---------+------+-------+-------+
| PKHT000105-12-20 | 31/12/2020 | 7101259 | 3311 | 20000 | 0 |
+------------------+------------+---------+------+-------+-------+

Pivoting Data in SQL and Updting it into Destination Table

I am working on a Store Procedure where I need to update records from from table 1 into table 2.
Table1: contains the following 4 columns
----------------------------------------------
| ID | TableName | ColumnName | Value |
----------------------------------------------
| 1 | [UserDetails] | [FinalScore] | 92 |
| 2 | [UserDetails] | [FinalScore] | 89 |
| 3 | [UserDetails] | [FinalScore] | 65 |
| 4 | [UserDetails] | [FinalScore] | 91 |
| 5 | [UserDetails] | [FinalScore] | 76 |
| 1 |[EmployeeDetail]| [ScoreData] | 0.91 |
----------------------------------------------
UserDetails table
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 0 |
| 2 | Leonard J | +14581232342 | 0 |
| 3 | Max R | +17123992342 | 0 |
| 4 | Robert H | +15688992122 | 0 |
| 5 | Jimmy L | +1328996782 | 0 |
-----------------------------------------------------------
Need to load all the data from table1 into the corresponding destination table for large amount of data (30,000 to 60,000) records.
Table1 contains the ID (from ID Column in table1) and FinalScore (from ColumnName Column in table1) in the destination table (from TableName Column in table1) where the value (from Value Column in table1) needs to be loaded.
End Result of UserDetails table after SP execution :
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 92 |
| 2 | Leonard J | +14581232342 | 89 |
| 3 | Max R | +17123992342 | 65 |
| 4 | Robert H | +15688992122 | 91 |
| 5 | Jimmy L | +1328996782 | 76 |
-----------------------------------------------------------
I am not sure how to load the data from "table1" into destination table (userdetails table) for bulk data as update query in a loop is taking very long time to complete.
It's not pivot what you're after but a simple join:
select
a.userid, a.contactno, b.finalscore
from UserDetails a
join table1 b on a.id = b.id
alternatively, if you want to update the old table without creating a new one, you can update join
SQL update query using joins

Group By with Multiple Unique Constraints Condition

I need to fetch data from below mentioned table Sizes where data will be unique by Code,ArtId and Size OR unique By Code,ArtId and SizeIndex (i.e There are two unique constraints).How I can get unique records in single select statement using group by .
ArtId | SizeIndex | Size | Description | Code
001 | 000000000001111 | X | TEST | 01
002 | 000000000001111 | XL | NULL | 02
003 | 000000000001111 | L | NULL | 03
004 | 000000000009999 | SL | TEST2 | 04
005 | 000000000009999 | ML | LIGHT | 05
006 | 000000000009999 | M | Filter element,Air | 06
Your help will be highly appreciated
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE FROM
(SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROM TABLE GROUP BY CODE,ARTID,SIZE
UNION
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROMM TABLE GROUP BY CODE,ARTID,SIZEINDEX)A

MS ACCESS insert records base on quantity number

I have 2 table and the relation is [Booking_Num]
table A: [ID],[Booking_Num] [Prod_Name],[Qty_Order]
table B: [ID],[Booking_Num],[Prod_Name],[Date_Out]
when I get insert to table A, should be the table B get many record base on Qty_Order..?
sample:
table A
ID | Booking_Num | Prod_Name | Qty_Order |
01 | BOK001 | Hammer | 4 |
table B
ID | Booking_Num | Prod_Name | Date_Out |
01 | BOK001 | Hammer | Null |
02 | BOK001 | Hammer | Null |
03 | BOK001 | Hammer | Null |
04 | BOK001 | Hammer | Null |
Thanks

Access db loop - for each record in one table create array of records in another table

Is it possible to create a nested looping query in Access DB that will update a third table?
I have a master (header) table:
------------------------
masters
------------------------
num | modality | cost |
------------------------
01 | thing | 23.00 |
02 | thing | 42.00 |
03 | thing | 56.00 |
04 | apple | 11.00 |
05 | apple | 17.00 |
and a temporary table containing detail info that I'll need to create a third (actual) details table which will key off of the masters table
here's a sample of the temp details table.
----------------------------------
temps
----------------------------------
modelnumber | modality | priceEa |
----------------------------------
| 123 | thing | 1.00 |
| 234 | apple | 2.00 |
| 345 | apple | 3.00 |
| 456 | apple | 4.00 |
| 567 | thing | 5.00 |
Basically, I need to loop through every record in the masters table.
Outer loop:
For each record in the masters table, grab the modality.
Inner loop:
Then for each record in the temps table, where the modalities match, create a record in the details table (and in the process, do some calculations based on temps.priceEa and masters.cost).
This should create (masters * temps) number of new records in the details table for every record in the masters table.
the details table, should end up looking like
----------------------------------------------------------
details
----------------------------------------------------------
num | modelnumber | modality | priceEa | adjustedCost |
----------------------------------------------------------
| 01 | 123 | thing | 1.00 | (do calc here)
| 01 | 567 | thing | 5.00 | (do calc here)
| 02 | 123 | thing | 1.00 | (do calc here)
| 02 | 567 | thing | 5.00 | (do calc here)
| 03 | 123 | thing | 1.00 | (do calc here)
| 03 | 567 | thing | 5.00 | (do calc here)
| 04 | 234 | apple | 2.00 | (do calc here)
| 04 | 345 | apple | 3.00 | (do calc here)
| 04 | 456 | apple | 4.00 | (do calc here)
| 05 | 234 | apple | 2.00 | (do calc here)
| 05 | 345 | apple | 3.00 | (do calc here)
| 05 | 456 | apple | 4.00 | (do calc here)
...etc
SELECT m.num, t.modelnumber, m.modality, t.priceea
into myNewTempTable
from masters m inner join temp t on m.modality = t.modality
order by m.num, t.modelnumber

Resources