2017-07-31 124 views
1

其他表列有三個表:核心,系統,和事實:SQL更新基於條件

  1. 核心表包含以下幾列core_date,close_date,X,Y,Z,ATA和soln_imp柱

  2. 系統表包含以下各列system_date中,x,y和z柱

  3. 事實表包含fact_date柱

我想更新core_date(喂此列)基於以下條件核心表的列,

當核心表的ATA柱10開始,然後選擇事實表的fact_date列

其他 從系統表中選擇系統日期根據以下匹配列如 x,y,z列的核心應該等於系統的x,y,z列

無論如何,如果核心表的soln_imp列爲null,然後選擇核心表的close_date

您能否對上述要求進行查詢?

我已經創建了下面的查詢:

update DB set 
    core_date = 


    case 
     when ata like '10%' 

     THEN (select distinct fact_date from FACT) 

    else  
    case when soln_imp is null 
        then close_date 

       else 

        (select distinct system_date 
        from Core c left join Systems s on 
          c.x = s.x 
          and c.y = s.y and c.z = s.z 
          where c.soln_imp is not null) 

       end 
    end 

運行查詢後,我得到這個錯誤:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

可否請你建議我正確的腳本,如果我的錯?

+0

也許一個MIN或MAX聚合,而不是獨特可能會幫助,與適當的GROUP BY一起。可以肯定的是,這些選擇僅返回一個值。因此,在把它們放在一起之前先測試你的選擇。除此之外,如果沒有表格的樣本數據,很難提供幫助。 – Myonara

+0

mysql和sql-server的不同。請只選擇一個。 –

回答

0

我跑你的查詢,我可以進行更新。這似乎可以是數據,我重現你的查詢,你的表,但不是你的數據。錯誤是這樣選擇的:

select distinct system_date 
       from Core c left join Systems s on 
         c.x = s.x 
         and c.y = s.y and c.z = s.z 
         where c.soln_imp is not null 

給出多於一行。我要說的第一件事就是把別名放在system_date中,這樣s.system_date。然後,我會運行這個查詢,直到它獲得您需要的一行。

我可以幫助你更多,但你必須給我一些你的數據樣本。