2014-01-11 2 views
1

내가 2 table.Below에 집계 함수를 사용하여 첫 번째 업데이트하는 코드가되어 노력하고,이 개 테이블을 가지고있다 -업데이트 여러 열

Update temp1 t1 
    set t1.Margin = SUM(t2.Margin2), 
    t1.Revenue = SUM(t2.Revenue2), 
    t1.Sales = SUM (t2.Sales2), 
    t1.Revenue = SUM (t2.Revenue2) 

    from t1 inner join tempcost t2 
    on t1.P_Id = t2.P_Id 

오류를 표시합니다 "집계가 UPDATE 문 집합 목록에 나타나지 않을 수 있습니다." 이를 달성하는 방법에 대한 제안.

+0

당신이 MySQL의 또는 SQL Server를 사용하고 있습니까? –

+0

SQL Server 사용. – Shivam657

답변

4

MySQL은 올바른 구문 :

Update temp1 t1 join 
     (select p_id, SUM(t2.Margin2) as margin2, SUM(t2.Revenue2) as revenue2, 
       SUM(t2.Sales2) as sales2 
     from tempcost t2 
     group by p_id 
     ) t2 
     on t1.P_Id = t2.P_Id 
    set t1.Margin = t2.margin2, 
    t1.Revenue = t2.Revenue2, 
    t1.Sales = t2.Sales2; 

SQL Server의 올바른 구문 :

Update t1 
    set Margin = t2.margin2, 
     Revenue = t2.Revenue2, 
     Sales = t2.Sales2 
    from temp1 t1 join 
     (select p_id, SUM(t2.Margin2) as margin2, SUM(t2.Revenue2) as revenue2, 
       SUM(t2.Sales2) as sales2 
      from tempcost t2 
      group by p_id 
     ) t2 
     on t1.P_Id = t2.P_Id; 
+0

도움 주셔서 감사합니다 :). – Shivam657

+0

영업 = t2. 판매 (세미콜론 없음) – Shivam657

1

하위 쿼리로 이동하는 방법은 어떻습니까?

Update temp1 t1 
    set t1.Margin = t2.Margin 
     ,t1.Revenue = t2.Revenue 
     ,t1.Sales = t2.Sales2 
    from t1 
    join (
     SELECT 
      SUM(Margin2) as Margin 
      ,SUM(Revenue2) as Revenue 
      ,SUM(Sales2) as Sales 
     FROM tempcost 
    ) t2 
    on t1.P_Id = t2.P_Id 
+0

하위 쿼리에서 쉼표를 잊어 버렸습니다. – Shivam657

+0

도움을 주셔서 감사합니다. – Shivam657