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.
6条答案
按热度按时间kcwpcxri1#
Use
MAX()
: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 theUPDATE
, you'll automatically correct any inconsistencies withEID
being different for a givenHold
.zvms9eto2#
You can use an updatable CTE and a window function. The benefit of this is that the table is only scanned once.
nsc4cvqm3#
You can do it using
group by
andmax()
as follows :First get the max(Eid) per Hold :
Then apply the update after joining it to your table:
demo here
fjaof16o4#
your data
use following query
dbfiddle
or more simply
dbfiddle
rur96b6h5#
Try the below code to get the desired outputs. Hope this helps.
rryofs0p6#
There is a lot going wrong with your query.
IS NOT NULL
OR
ed conditions to get the correct logicEID
.A possible solution which updates the correct rows, and correlates the new value correctly is