I have a table and I want to update some columns in this table randomly.
This is my update script
update personnels set
first_name=(select top 1 first_name from personnels order by NEWID()),
I tried this script but select top 1 first_name from personnels order by NEWID() query took too long. How can I update column efficiantly random.
Try generating a Random sequence outside and update using a self-join. Something like this
;WITH CTE
AS
(
SELECT
RN = ROW_NUMBER() OVER(ORDER BY NEW_ID()),
FristName
FROM personnels
),C2
AS
(
SELECT
RN = ROW_NUMBER() OVER(ORDER BY NEW_ID()),
FristName
FROM personnels
)
UPDATE C2
SET
FristName = CTE.FristName
FROM C2
INNER JOIN CTE
ON C2.RN = CTE.RN
I'm not sure what possible purpose you would want this for but try using the rand function
update personnels
set first_name =
(select first_name
from personnels p
where id = (select rand(1 + (RAND() * max(id)))
from personnels p1))
i lost count on the brackets but think this is OK to run
UPDATE table SET val =
( SELECT val FROM table table1 WHERE col =
( SELECT RAND( 1+ ( RAND() * MAX(col)))
FROM table table2
)
)
Related
Is it possible to generate a unique ID for the auxiliary table? I am retrieving data from several tables, but I do not know how to create a new ID for the results:
I would like to have an additional column with ID.
I tried to look for several methods, but nothing helped me.
I will be very grateful.
Greetings,
with ct as (
select *
INTO temp_table
from dba.view_NEW_Users_AreaCodes ur
join dba.view_NEW_Customers_SalesTowns ct on ct.CustSalesTerritoryTTID = ur.UserAreaCodeID
where ur.UserType = 'TT'
and ct.CustSalesTerritoryTTID <> 0
union all
select *
from dba.view_NEW_Users_AreaCodes ur
join dba.view_NEW_Customers_SalesTowns ct on ct.CustSalesTerritoryMTID = ur.UserAreaCodeID
where ur.UserType = 'MT'
and ct.CustSalesTerritoryMTID <> 0
union all
select *
from dba.view_NEW_Users_AreaCodes ur
join dba.view_NEW_Customers_SalesTowns ct on ct.CustSalesTerritoryHRCID = ur.UserAreaCodeID
where ur.UserType = 'HRC'
and ct.CustSalesTerritoryHRCID <> 0
union all
select *
from dba.view_NEW_Users_AreaCodes ur
join dba.view_NEW_Customers_SalesTowns ct on ct.CustSalesTerritoryDevID = ur.UserAreaCodeID
where ur.UserType = 'DEV'
and ct.CustSalesTerritoryDevID <> 0
)
select row_number() over (order by newid()) as DATA_ID,
ct.*
from ct;
You could use row_number():
with t as (
< your query here >
)
select row_number() over (order by newid()) as seqnum,
t.*
from t;
newid() is just an arbitrary value that randomizes the numbering. You can use a column there if you prefer a more canonical ordering.
I want to update a column in a table that would set to plus 1 if it can find a duplicate number in the column cownnum and else it would set to 1 if no duplicate was found
I tried the code below but show error
Msg 512, Level 16, State 1, Line 53
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
update dbo.temp1 set SEQNO = case SEQNO when (SELECT
cownnum
FROM
dbo.temp1
GROUP BY
cownnum
HAVING
COUNT(*) > 1) then SEQNO = 2 else SEQNO = 1 end
for example the result would be
cownnum 1 1 2 3
SEQNO 1 2 1 1
Your question is confusing, but there's no reason to use the GROUP BY clause:
UPDATE dbo.temp1 SET SEQNO = 1 WHERE SEQNO = cownnum
or perhaps you need to check cownnum somehow:
UPDATE dbo.temp1 SET SEQNO = 1 WHERE cownnum = (SELECT TOP 1 xxx FROM table WHERE ???)
Please provide an example of your data and the expected results.
You can use ROW_NUMBER.
UPDATE t1
SET t1.SEQNO = t2.rowno
FROM table_with_duplicates t1
INNER JOIN
(
SELECT unique_id,
ROW_NUMBER() OVER (PARTITION BY cownnum ORDER BY cownnum) AS rowno
FROM table_with_duplicates
) t2
ON t1.unique_id = t2.unique_id;
The perfect solution for your requirement is
UPDATE #Temp1
SET SEQNO = B.NewSEQ
FROM #Temp1 A ,
( SELECT id,
CASE WHEN COWNNUM = LAG(COWNNUM) OVER ( ORDER BY id ) THEN
ROW_NUMBER() OVER ( PARTITION BY COWNNUM ORDER BY id ) ELSE 1 END AS NewSEQ
FROM #temp1
) B
WHERE A.id = B.id
you can check this query execution by clicking on DEMO
You can use lag() to check previous row value in sqlserver
Update temp1
set temp1.SEQNO = B.SEQNO
FROM temp1 A, (select id , COWNNUM,
case when COWNNUM = lag(COWNNUM) over(order by id)
then ROW_NUMBER() OVER ( PARTITION BY COWNNUM ORDER BY id ) else 1 end as SEQNO
from temp1) B
WHERE A.id = B.id
DEMO
You can use window functions. This is simplest using an updatable CTE:
with toupdate as (
select t.*,
row_number() over (partition by cownum order by (select null)) as new_seqno
from dbo.temp1 t
)
update toupdate
set seqno = new_seqno;
If I understand the question correctly, no join is required.
I have this query:
Select *
from
(Select
*
ROW_NUMBER() OVER (PARTITION BY TID ORDER BY TID) AS RowNumber
from
MyTable
where
Eid = 'C1') as a
where
a.RowNumber = 1
and it displays these results:
Column1 Column2 RowNumber
------------------------------
Value1 value2 1
I want to ignore the RowNumber column in the select statement and I don't want to list all columns in select query (100+ columns and given is just an example).
How to do this in SQL Server?
Well, you would have to list all the columns in the outer select, if you use a subquery and row_number() to get a unique row.
An alternative method uses a correlated subquery, but requires having some unique column in the table. If you have one:
select t.*
from mytable t
where t.col = (select max(t2.col) from mytable t2 where t2.tid = t.tid and t2.eid = 'C1');
With the right indexes, this can have better performance than the row_number() version.
If you don't have a unique column, you can do:
select t.*
from (select distinct tid from mytable where eid = 'C1') tc cross apply
(select top 1 t.*
from mytable t
where t.tid = tc.tid and t.eid = 'C1'
) t;
Wrap your query as a subquery and select specific columns from it like so:
SELECT x.Column1, x.Column2
FROM
(
Select * from (Select * ROW_NUMBER() OVER (PARTITION BY TID ORDER BY TID)
AS RowNumber from MyTable where Eid="C1") as a where a.RowNumber=1
) AS x
OR Change your original Select to:
Select a.[Column1], a.[Column2]
from
(
Select * ROW_NUMBER() OVER (PARTITION BY TID ORDER BY TID)
AS RowNumber from MyTable where Eid="C1"
) as a
Where a.RowNumber=1
Replace * from your query in clarify exactly columnd which you whant
select x.Column1, x.Column2 FROM (
Select * from (Select * ROW_NUMBER() OVER (PARTITION BY TID ORDER BY TID)
AS RowNumber from MyTable where Eid="C1") as a where a.RowNumber=1) AS x
I was working on a task to update columns which have duplicate ID's in a column
how can we update column only DrugLabelName ? i need to update old_drug_name with new_drug_name using the duplicate ID 00004029830 ?
Please advise
If you want all rows with the same id to have the same name, you can use window functions:
with toupdate as (
select t.*,
first_value(druglabelname) over (partition by id order by intid desc) as new_druglabelname
from t
)
update toupdate
set druglabelname = new_druglabelname
where druglabelname <> new_druglabelname;
How about?
CREATE TABLE tbl
(INTid int
,ID varchar (20)
,DrugLabelName varchar(200)
)
INSERT tbl (INTid, ID, DrugLabelName)
SELECT 137272, '00004029830', 'old_drug_name'
INSERT tbl (INTid, ID, DrugLabelName)
SELECT 1668177, '00004029830', 'New_drug_name'
INSERT tbl (INTid, ID, DrugLabelName)
SELECT 1668178, '00004029831', 'Other_drug_name'
GO
UPDATE t
SET DrugLabelName = x.DrugLabelName
FROM tbl AS t
INNER JOIN tbl as x
ON t.ID = x.id
AND x.INTid > t.INTid
SELECT *
FROM tbl
DROP TABLE tbl
Is there a T-SQL statement to auto fill an empty column in a table with incremented values starting at one specific value?
E.g.
UPDATE A SET A.NO = ROW_NUMBER() OVER (ORDER BY A.NO) + #max WHERE A.NO is NULL
This statement doen't work and I don't know how to make it run...
Any help is appreciated!
WITH q AS
(
SELECT a.*, MAX(no) OVER() + ROW_NUMBER() OVER (ORDER BY a.no) AS rn
FROM a
)
UPDATE q
SET no = rn
This works. You need to decouple the ranking function from the update
UPDATE
bar
SET
NO = bar.foo + #max
FROM
(SELECT
A.NO,
ROW_NUMBER() OVER (ORDER BY A.NO) AS foo
FROM
A
WHERE
A.NO is NULL
) bar