locked
add a data to table from another table with sum function RRS feed

  • Question

  • hello everyone.

    İ have 2 tables calisanlar and Siparis_onaylanan.In calisanlar table there are calisan_no,adet and etc. columns.In Siparis_onaylanan table there are Siparis_id,urun,adet,calisan_no and etc. columns.I want to add data adet in calisanlar from sum(adet) Siparis_onaylanan.

    For example

    Caner Ünver's calisan_no(worker_id) 147 and add a delivery 5 banana and 4 watermelon.I want to add this numbers of items to calisanlar's adet

    i wanna codes like that

    update calisanlar set adet=select sum(adet) from Siparis_onaylanan where Siparis_onaylanan.calisan_no=calisanlar.calisan_no

    but it doesn't work :)

    Sunday, July 28, 2013 8:20 PM

Answers

  • ;With cte As
    (Select calisan_no, Sum(adet) As Sum_adet
    From Siparis_onaylanan
    Group By calisan_no)
    Merge calisanlar c
    Using cte
    On c.calisan_no = cte.calisan_no
    When Matched Then Update Set adet = cte.Sum_adet;
    

    Tom
    • Proposed as answer by Naomi N Sunday, July 28, 2013 11:35 PM
    • Marked as answer by Caner Ünver Monday, July 29, 2013 12:20 PM
    Sunday, July 28, 2013 9:54 PM

All replies

  • ;With cte As
    (Select calisan_no, Sum(adet) As Sum_adet
    From Siparis_onaylanan
    Group By calisan_no)
    Merge calisanlar c
    Using cte
    On c.calisan_no = cte.calisan_no
    When Matched Then Update Set adet = cte.Sum_adet;
    

    Tom
    • Proposed as answer by Naomi N Sunday, July 28, 2013 11:35 PM
    • Marked as answer by Caner Ünver Monday, July 29, 2013 12:20 PM
    Sunday, July 28, 2013 9:54 PM
  • Deleted
    Sunday, July 28, 2013 11:37 PM
  • Tom thank you very much you are best :)
    Monday, July 29, 2013 12:20 PM