SQL Server SQL Replace Null entry with value

bis0qfac  于 2023-03-07  发布在  其他
关注(0)|答案(3)|浏览(165)

I'm looking for a query/script in MS SQL Server that will do the following:

Table1
| Project | Employee | Date | Comment |
| ------------ | ------------ | ------------ | ------------ |
| 1111 | 007 | 23-02-01 | Found Bad Guy |
| 1111 | 007 | 23-02-01 | NULL |
| 1112 | 007 | 23-02-18 | Drank Martini |
| 1112 | 007 | 23-02-18 | NULL |

I need a way to replace the NULL s with the appropriate comment for that same day, but only if the project/date is the same (not put Martini on the 1111 project). So the final result should look like this...

ProjectEmployeeDateComment
111100723-02-01Found Bad Guy
111100723-02-01Found Bad Guy
111200723-02-18Drank Martini
111200723-02-18Drank Martini

I'm not going to know which projects or which dates have the null comment, but would be ok if I have to run it twice if there are two null's for the same date with one having the comment. I'm only going to be able to search a date range >= '2023-02-01' I have edited the above to respond to questions and better clarification

khbbv19g

khbbv19g1#

update destination
set destination.comment = source.comment
from your_table destination
join your_table source on source.project = destination.project
                      and source.date = destination.date
                      and source.comment is not null
where destination.comment is null
zu0ti5jz

zu0ti5jz2#

Just updating some NULL values..

First determine the new value for that column

SELECT 
   Project,
  Employee,
  Date,
  CASE WHEN Comment is NULL THEN LAG(Comment) OVER (PaRTITION BY Project,Date ORDER BY Employee) ELSE Comment END as Comment
FROM mytable;

and use this query in an UPDATE statement:

UPDATE mytable 
SET Comment = x.Comment
FROM mytable m
INNER JOIN (
   SELECT 
      Project,
      Employee,
      Date,
      CASE WHEN Comment is NULL THEN LAG(Comment) OVER (PaRTITION BY Project,Date ORDER BY Employee) ELSE Comment END as Comment
   FROM mytable
  ) x on x.Project = m.Project and x.Employee = m.Employee and x.Date = m.Date
WHERE m.Comment IS NULL

see: DBFIDDLE

dgsult0t

dgsult0t3#

Its works with mysql and postgres
Scenario 1:Nested Query works with postgres and mysql

update your_table as s set comment =(
select comment from your_table  where project=s.project and date=s.date and comment is not null limit 1
)
where s.comment is null;

Scenario 2:Using Join works with mysql

UPDATE your_table d
JOIN your_table s ON s.date =d.date and s.project=d.project 
SET    d.comment = s.comment 
where d.comment is null 
and s.comment is not null

Scenario 3:works with MIcrosoft SQL Server tested on this Site

update your_table set comment =(
select top 1 comment from your_table as t  where project=your_table.project and date=your_table.date
and comment is not null
)
where your_table.comment is null;

Scenario 4:This join is also works with Microsoft Server tested on this Site

UPDATE st
SET st.comment = sc.comment
FROM your_table AS st
JOIN your_table AS sc
ON st.Project = sc.Project and st.date = sc.date
WHERE sc.Comment is not null
AND st.Comment is  null

相关问题