problem with bulk data update in sql - sql

i have this values in a coulmn say accountno in table record:
1,002
1,044
1,086
1,108
1,126
1,190
1,226
1,258
1,260
now i want to update them as
1002
1044
1086
1108
1126
1190
1226
1258
1260
the column is of type string. how can i do it??

assuming you are using SQL server -
update table
set accountno = REPLACE(accountno,',', '')

UPDATE record
SET accountno = replace(accountno,',','')

Related

Merge Output - Capture Column Name Updated

I am merging a table and want to retain the previous value in a seperate table, along with the column that was updated.
I have got it working to retain the values, but want to know how I can retain the column name.
Existing table: tTable1
qID qUnits qDateTime
1001 4900 2022-09-13 12:00:00.000
1002 6800 2022-09-14 15:00:00.000
1003 7400 2022-09-14 13:00:00.000
Temp Table (holds updated values): #updateValues
qID qOriginalUnit qUpdatedUnit
1001 4900 8900
1002 6800 13400
1002 7400 16500
The code i'm using currently, to output what the existing value was, before the update;
DECLARE #auditRecords TABLE(qID INT, PreviousValue VARCHAR(100)
MERGE tTable1 AS TARGET
USING #updateValues AS SOURCE
ON (SOURCE.qID = TARGET.qID)
WHEN MATCHED
THEN UPDATE SET
TARGET.qUnits = SOURCE.qUpdatedUnit
OUTPUT SOURCE.qID, SOURCE.qOriginalUnit INTO #auditRecords(qID,PreviousValue);
I'd like to be able to include the column name that was updated, in this instance QUnits, so the output would look like the following;
qID PreviousValue UpdatedColumn
1001 4900 qUnits
1002 6800 qUnits
1003 7400 qUnits
In this particular example, where you are only updating a single column, you can just hardcode the column name in the OUTPUT results:
OUTPUT SOURCE.qID,
SOURCE.qOriginalUnit,
'qUnits'
INTO #auditRecords(qID, PreviousValue, UpdatedColumn)
If you are updating multiple columns it becomes a bit more cumbersome as you need to compare values column by column.
You could also move this into an UPDATE TRIGGER in which you have access to the results of the COLUMNS_UPDATED function.

How would I create one data set from a table that contains two identical values in the first column but different data in the remaining columns Oracle

I have produced a dataset where some of the data has two identical memberkeys but different contract values, while other memberkeys only appear once. I need to merge those memberkeys that have two rows into one distinct memberkey row containing all the data from both rows while leaving the single row memberkey as is.
Current
MemberKey
SubscriberKey
VALUEONE
VALUETWO
VALUETHREE
VALUEFOUR
VALUEFIVE
VALUESIX
VALUESEVEN
VALUEEIGHT
VALUENINE
VALUETEN
VALUEELEVEN
2235
H4931
MA84100303
ENGLISH
ACOC
5TX4VV3TD79
13
1
2235
2235
A84100303
b
ENGLISH
AUCOC
A84100303
4375
H4931
MA48450239
SPANISH
APIM
9QP3K96WK88
14
1
4375
4375
A48450239
SPANISH
AUPIM
A48450239
375
375
H4931
MA08111511
ENGLISH
AMAR
8B06P95CG54
Desired
MemberKey
SubscriberKey
VALUEONE
VALUETWO
VALUETHREE
VALUEFOUR
VALUEFIVE
VALUESIX
VALUESEVEN
VALUEEIGHT
VALUENINE
VALUETEN
VALUEELEVEN
2235
2235
A84100303
b
H4931
MA84100303
ENGLISH
ACOC
AUCOC
A84100303
5TX4VV3TD79
13
1
4375
4375
A48450239
H4931
MA48450239
SPANISH
APIM
AUPIM
A48450239
9QP3K96WK88
14
1
375
375
H4931
MA08111511
ENGLISH
AMAR
8B06P95CG54
I've tried several approaches (ctes, temp tables, convoluted joins etc) without success.
Thanks
Could you create a table and then update this new table with your old table using by joining on the memberkey?
update new_table a
set a.valueone = (select max(x.valueone)
from old_table x
where a.memberkey = x.memberkey);
commit;
This would work as long as for the same memberkey you do not have a different value for the same column.
This seems to work
MERGE INTO MEMBERS m2
USING MEMBERS_GTT m1 ON ( m1.MemberKey = m2.MemberKey )
WHEN MATCHED
THEN
UPDATE SET m2.SUBSCRIBERKEY = COALESCE(m1.SUBSCRIBERKEY,m2.SUBSCRIBERKEY)
WHEN NOT MATCHED
THEN
INSERT ( MemberKey ,
SUBSCRIBERKEY
)
VALUES ( m1.MemberKey ,
m1.SUBSCRIBERKEY
)

SQL Code to Update Entities with the same ID

We have an app to manage our Member's information that is tied to a SQL Database. We have attributes that the users can set that apply to the whole family. I am trying to write a SQL Script that will update the values for the whole family.
Example:
Here is a sample of a few columns of our dbo.AttributeValue column:
AttributeID
EntityID
Value
CreatedDateTime
ModifiedDateTime
5856
733
True
2021-11-06 17:30:38.207
2021-11-10 13:52:09.843
5856
613
Fale
2021-11-05 12:12:08.207
2021-11-16 3:32:01.843
Here is a sample of a few columns in our dbo.Person Table:
ID
PrimaryFamilyID
733
187
709
187
137
187
I would like for anyone with the same value in PrimaryFamilyID to have the same values in the dbo.AttributeValue table. Bonus points if we can make it update to the value with the most recent ModifiedDateTime in the dbo.AttributeValue table so that if someone in the family modifies the value after every has an assigned attribute, it will go ahead an update those as well.
Desired outcome:
AttributeID
EntityID
Value
CreatedDateTime
ModifiedDateTime
5856
733
True
2021-11-06 17:30:38.207
2021-11-10 13:52:09.843
5856
709
True
2021-11-06 17:30:38.207
2021-11-10 13:52:09.843
5856
137
True
2021-11-06 17:30:38.207
2021-11-10 13:52:09.843
It took me a while to get to a solution what you want is but here it is
DBFiddleRunningSolution
You can start somewhere from here.
With PersonCTE as (
Select Count(*) as cnt,PrimaryFamilyId
from Person
group by PrimaryFamilyId
having count(*)>1
)
Select AV.AttributeId,P.Id,AV.Value,AV.CreatedDateTime,AV.ModifiedDateTime
into NewAttributeValue
from Person P inner join PersonCTE C
ON P.PrimaryFamilyId = C.PrimaryFamilyId
cross join AttributeValue AV
where AV.EntityId in (Select distinct Id from Person)

Fill a Number field with Auto Increment values in MS ACCESS

I have a Access Table with following structure:
INO IDATE NAME ADDRESS CITY PHONE CONTACT & Other Fields.....
1430 01-Apr-15
1431 01-Apr-15
1432 01-Apr-15
1435 01-Apr-15
1436 01-Apr-15
1440 01-Apr-15
1441 01-Apr-15
1448 01-Apr-15
1450 01-Apr-15
1455 02-Apr-15
1456 02-Apr-15
1459 02-Apr-15
Field "INO" is set To Data type "NUMBER" Field Size: Double Indexed= Yes
This field is auto increment programmatically, but after deletion of some useless & cancelled records from database. INo. series is not in order. I want to re generate the numbers & fill the gap of deleted numbers.
i.e. INO 1435 should be 1433, 1436 to 1434, 1440 to 1435 & so on.
Select Statement i use to filter records from table:
SELECT *
FROM IDETAIL
WHERE TYPE=True AND INO>=1430 ORDER BY INO;
Till 1430 records are in order there is gap after 1430 & dated 01-Apr-15.
there are about 18,000 records in table so not easy to manually correct the records. How can i correct this with SQL or any other command.
P.S. : No related table or entry is effected. We just want to maintain the sequence of entries for accounting purpose.
Just like we use "Do While" function.
Starting entry INo is correct and known. All the entries got numbered in a loop starting with 1st record INo=1430, next record INO = 1430+1, next 1430+2 until last entry. How to implement same in UPDATE query alongwith "WHERE TYPE=True".
If no table is referenced from this table, You can use following query:-
UPDATE YourTable
SET INO = DCount('*', 'YourTable', 'INO <= ' & INO)
Hope this helps
If you insert a new autonumber field into the table, it should generate a new set of IDs for the rows. Then, if you want, you can update your existing field to the new ID value and delete the autonumber field.
Using and improving the great answer from #Ankit Bajpai here is maybe what you need:
UPDATE (SELECT * FROM IDETAIL WHERE TYPE=True AND INO>=1430 ORDER BY INO)
SET INO = DCount('*', 'IDETAIL ', 'INO <=' & INO & ' AND INO >= 1430 AND Type = True') + 1429
Be aware that if INO is indexed and without duplicates,
it may create errors in the recordsets Type=false

How can I add a timestamp column in hive

I have 2 rows like below:
941 78 252 3008 86412 1718502 257796 2223252 292221 45514 114894
980 78 258 3064 88318 1785623 269374 2322408 305467 46305 116970
I want to insert current time stamp while inserting each row.
finally in my hive table row should be like below:
941 78 252 3008 86412 1718502 257796 2223252 292221 45514 114894
2014-10-21
980 78 258 3064 88318 1785623 269374 2322408 305467 46305 116970
2014-10-22
Is there any way I can insert timestamp directly into hive without using pig script?
You can use from_unixtime(unix_timestamp()) while inserting.
For example, suppose you have following tables:
create table t1(c1 String);
create table t2(c1 String, c2 timestamp);
Now you can populate table t2 from t1 with current timestamp:
insert into table t2 select *, from_unixtime(unix_timestamp()) from t1;