0

This is the first table:

Table source 
(
     id int,
     accno varchar(50),
     duedate date,
     plafond int,
     customer_name varchar(100),
     bicoll int,
     ccy varchar(5),
     parent_id int
)

This is the second table:

Table result  
(
    id int,
    accno varchar(50),
    duedate date,
    plafond int,
    customer_name varchar(100),
    bicoll int,
    ccy varchar(5),
    jumlah int
)

From those tables, I need to make INSERT statement to the first table (source) with theseconditions :

  1. if plafond < 1000 then group by ccy with accno = "GRUP1", duedate = null, plafond = SUM(plafond), customer_name = "GRUP1", bicoll = NULL, jumlah = COUNT THE ROWS THAT HAVE BEEN GROUPED

  2. if plafond >= 1000 and < 5000 group by customer_name and ccy with due_date = max(duedate), plafond = sum(plafond), bicoll = max(bicoll), jumlah = COUNT THE ROWS THAT HAVE BEEN GROUPED

  3. if plafond >= 5000 do not group so that fields jumlah = 1

How to write this INSERT statement?

And the second table has relation between parent_id (source) and id (result), so if I want to modify something, I used those relation using UPDATE statement.

I am working on an UPDATE statement, but what confuses me is the INSERT statement with those conditions

Please help me, it has been bothering me for weeks. Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    Show us sample data and expected output. Please read [**How-to-Ask**](http://stackoverflow.com/help/how-to-ask) And here is a great place to [**START**](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) to learn how improve your question quality and get better answers. – Juan Carlos Oropeza Jan 21 '17 at 13:52
  • Thanks, i dont have a sample output, because all i need is INSERT STATEMENT for that table. I will edit my threads – Steven Chandra Jan 21 '17 at 13:56
  • Then show us how should end the db after the insert, or show us a select of the data you would insert. – Juan Carlos Oropeza Jan 21 '17 at 18:49

1 Answers1

1

Don't have time for a long answer but what you need is a basic SQL SWITCH CASE statement.

See this for examples.

Community
  • 1
  • 1
VeldMuijz
  • 605
  • 5
  • 18