SQL Server Update row in the same table

mccptt67  于 2023-06-21  发布在  其他
关注(0)|答案(6)|浏览(215)

I have Table 1 that look something like this

EID      Hold        Source
3232     KLME         jame.k@gmail.com
         KLME         https://google.com
         EEME         david.e@gmail.com
         JJIN         Test@gmail.com
7232     JJIN         https://google.com

I'm just wondering how can I update the second row of hold "KLME" using the first row.

example like this

EID      Hold        Source
3232     KLME         jame.k@gmail.com
3232     KLME         https://google.com
         EEME         david.e@gmail.com
7232     JJIN         Test@gmail.com
7232     JJIN         https://google.com
UPDATE Test_Table 
SET EID = (
    SELECT TOP 1 Test_Table.EID
    FROM Test_Table 
    WHERE Test_Table.Hold = Test_Table.Hold
    AND Test_Table.EID <> '' OR Test_Table.EID <> NULL
)

I have this simple script but I'm getting a different EID from different user instead of the correct EID for some reason.

kcwpcxri

kcwpcxri1#

Use MAX() :

UPDATE Test_Table 
SET EID = (
    SELECT MAX(EID)
    FROM Test_Table a
    WHERE a.Hold = Test_Table.Hold
)

Note how you don't need to filter out blanks or nulls because MAX ignores nulls and any non-blank value is greater than blank.

Also, by not adding a WHERE clause to the UPDATE , you'll automatically correct any inconsistencies with EID being different for a given Hold .

zvms9eto

zvms9eto2#

You can use an updatable CTE and a window function. The benefit of this is that the table is only scanned once.

WITH cte AS (
    SELECT *,
      OtherEID = MIN(NULLIF(tt.EID, '')) OVER (PARTITION BY tt.HOLD)
    FROM Test_Table tt
)
UPDATE cte
SET EID = OtherEID
WHERE (EID IS NULL OR EID = '')
  AND OtherEID IS NOT NULL;
nsc4cvqm

nsc4cvqm3#

You can do it using group by and max() as follows :

First get the max(Eid) per Hold :

select Hold, max(EID) as Eid
from Test_Table
group by Hold

Then apply the update after joining it to your table:

update t
set Eid = S.Eid
from Test_Table t
inner join (
  select Hold, max(EID) as Eid
  from Test_Table
  group by Hold
) s on s.Hold = t.Hold

demo here

fjaof16o

fjaof16o4#

your data

CREATE TABLE mytable(
   EID    INTEGER  
  ,Hold   VARCHAR(20) NOT NULL
  ,Source VARCHAR(100) NOT NULL
);
INSERT INTO mytable(EID,Hold,Source) VALUES 
(3232,'KLME','jame.k@gmail.com'),
(NULL,'KLME','https://google.com'),
(NULL,'EEME','david.e@gmail.com'),
(NULL,'JJIN','Test@gmail.com'),
(7232,'JJIN','https://google.com');

use following query

update m1
set m1.EID=iif(m1.EID is null,m2.EID,m1.EID)
from mytable m1
join mytable m2
on m1.Hold=m2.Hold and m1.Source<>m2.Source

dbfiddle

or more simply

update m1
set m1.EID=m2.EID
from mytable m1
join mytable m2
on m1.Hold=m2.Hold and m1.Source<>m2.Source and m1.EID is null

dbfiddle

rur96b6h

rur96b6h5#

Try the below code to get the desired outputs. Hope this helps.

Create table t
(
EID varchar(20),
HOLD varchar(20),
[Source] varchar(40)
)

insert into t
Select '3232','KLME','jame.k@gmail.com'
union all select '    ','KLME','https://google.com'
union all select '    ','EEME','david.e@gmail.com'
union all select '    ','JJIN','Test@gmail.com'
union all select '7232','JJIN','https://google.com'

update t
set t.EID = t1.EID
FROM t,
(Select EID,HOLD from t where t.EID <> '') t1
where t1.HOLD = t.hold
and t.EID = ''

Select * from t

OUTPUT:

EID     HOLD    Source
---------------------------
3232    KLME    jame.k@gmail.com
3232    KLME    https://google.com
        EEME    david.e@gmail.com
7232    JJIN    Test@gmail.com
7232    JJIN    https://google.com
rryofs0p

rryofs0p6#

There is a lot going wrong with your query.

  1. You cannot use an equality (or inequality) check against null, you must use IS NOT NULL
  2. You need brackets around your OR ed conditions to get the correct logic
  3. You really only need to update rows with a missing EID .

A possible solution which updates the correct rows, and correlates the new value correctly is

UPDATE t1 SET
  EID = (SELECT TOP 1 Test_Table.EID FROM Test_Table t2 WHERE t2.[Hold] = t1.[Hold] AND COALESCE(t2.EID,'') <> '')
FROM Test_Table t1
WHERE Test_Table.EID = '' OR Test_Table.EID IS NULL;

相关问题