SQL - 仅当具有相同id的所有记录满足要求时才修改字段

时间:2013-05-07 14:28:35

标签: sql sql-server sql-server-2008-r2

我对SQL很新,对UPDATE查询有疑问。我正在使用SQL Server 2008 R2。我写了以下SELECT查询:

SELECT res.labresultcat_id, res.patient_id, lab.test_performed,
       lab.result_status, lab.result_value, lab.abnormal_flag, res.recorded_on, 
       res.cat_test_name, res.results_to, usr.last_name, res.result_viewed
FROM dbo.med_labresultcat res
  INNER JOIN dbo.med_labresult lab
    ON res.labresultcat_id = lab.labresultcat_id
  LEFT JOIN dbo.res_user usr
    ON res.results_to = usr.resource_id
WHERE lab.result_status = 'F' AND usr.last_name = 'Smith'
  AND res.result_viewed is NULL

结果看起来像这样:

patient_id    result_id   test_performed     abnormal_flag    recorded_on    results_to    
0001          0045        ABC                N                4/1/2013       14            
0001          0045        CBS                A                4/1/2013       14           
0002          0061        NBC                N                4/3/2013       13            
0002          0061        MST                N                4/3/2013       13
0003          0090        ABC                H                4/20/2013      10
0003          0090        MST                N                4/20/2013      10

我想要做的是仅当具有相同result_id的所有记录都是= N或NULL时才更新results_to字段

所以,我写了以下UPDATE查询:

;WITH lab AS
  (
   select RES.labresultcat_id, res.cat_test_name, res.results_to, 
          res.result_status,
          lab.test_performed, lab.result_value, lab.abnormal_flag, 
          res.result_viewed, usr.last_name
   from DBO.med_labresultcat RES 
     INNER JOIN dbo.med_labresult lab
       ON res.labresultcat_id = lab.labresultcat_id
     LEFT JOIN DBO.res_user USR
       ON res.results_to = usr.resource_id
   WHERE res.result_viewed is null AND lab.result_status = 'F'
     and RES.test_date <= '03/15/2013' AND usr.last_name = 'Smith'
   )
   UPDATE lab SET results_to = CASE WHEN 0 = (SELECT COUNT(abnormal_flag)
   WHERE abnormal_flag = 'A' OR abnormal_flag = 'H' OR abnormal_flag = 'L')
   THEN '146' ELSE results_to END

但是,上面的查询会抛出SQL错误“聚合可能不会出现在更新语句的集合列表中”不幸的是,对此错误的研究超出了我对SQL的理解。

我真的很感激,如果有人可以提供一些明确的方向来推进这个方面,因为我现在非常困难。

1 个答案:

答案 0 :(得分:1)

尝试此查询

 ;WITH lab AS
 (
  select RES.labresultcat_id, res.cat_test_name, res.results_to, 
         res.result_status,
         lab.test_performed, lab.result_value, lab.abnormal_flag, 
         res.result_viewed, usr.last_name
  from DBO.med_labresultcat RES
    INNER JOIN dbo.med_labresult lab ON res.labresultcat_id = lab.labresultcat_id
    LEFT JOIN DBO.res_user USR ON res.results_to = usr.resource_id
  WHERE res.result_viewed is null AND lab.result_status = 'F' 
    and RES.test_date <= '03/15/2013' AND usr.last_name = 'Smith'
  )
  UPDATE l
  SET l.results_to = '146'
  FROM lab l
  WHERE EXISTS (
                SELECT 1
                FROM lab l2
                WHERE l.result_id = l2.result_id                  
                HAVING COUNT(DISTINCT ISNULL(l2.abnormal_flag, 'N')) = 1
                ) AND ISNULL(l.abnormal_flag, 'N') = 'N'

SQLFiddle上的演示