How to update this? please

Dear all expert
I have 2 tables 'CUST_AND_MKT' and 'CUST_SUMMARY' as below
select * from cust_and_mkt;cust_id   mkt_id    sale_volume
C0001     M0124     10,000
C0001     M7208     4,000
C0001     M0010     1,000
C0444     M0080     340,000
C0444     M1200     100,000
C0878     M0505     450,000
select * from cust_summary;cust_id    cust_name         mkt_id   total_volume
C0001      JOHN EDWARD       <null>   15,000
C0444      SARAH LANE        <null>   440,000
C0878      TOMMY E.CLEK      <null>   450,000I want to assign marketing who has a maximun sale_volume
in each customer to take care customer. My problem is how
to write SQL command to update mkt_id from 'CUST_AND_MKT'
to 'CUST_SUMMARY' as below
cust_id    cust_name         mkt_id   total_volume
C0001      JOHN EDWARD       M0124    15,000
C0444      SARAH LANE        M0080    440,000
C0878      TOMMY E.CLEK      M0505    450,000Thank you all for your kindness
JOE-Thailand

SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bi
PL/SQL Release 10.2.0.2.0 - Production
CORE    10.2.0.2.0      Production
TNS for HPUX: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - Production
SQL> create table cust_summary
  2  as
  3  with t
  4  as
  5  (select 'C0001' cust_id, 'JOHN EDWARD' cust_name, cast(null as varchar2(20)) mkt_id,
  6           15000 total_volume from dual union all
  7   select 'C0444', 'SARAH LANE', null, 440000 from dual union all
  8   select 'C0878', 'TOMMY E. CLEK', null, 450000 from dual
  9  )
10  select * from t;
Table created.
SQL> create table cust_and_mkt
  2  as
  3    with t
  4    as
  5  (select 'C0001' cust_id, 'M0124' mkt_id, 10000 sales_volume from dual union all
  6   select 'C0001', 'M7208', 4000 from dual union all
  7   select 'C0001', 'M0010', 1000 from dual union all
  8   select 'C0444', 'M0080', 340000 from dual union all
  9   select 'C0444', 'M1200', 100000 from dual union all
10   select 'C0878', 'M0505', 450000 from dual
11  )
12  select * from t;
Table created.
SQL> select * from cust_summary;
CUST_ CUST_NAME     MKT_ID               TOTAL_VOLUME
C0001 JOHN EDWARD                               15000
C0444 SARAH LANE                               440000
C0878 TOMMY E. CLEK                            450000
SQL> select * from cust_and_mkt;
CUST_ MKT_I SALES_VOLUME
C0001 M0124        10000
C0001 M7208         4000
C0001 M0010         1000
C0444 M0080       340000
C0444 M1200       100000
C0878 M0505       450000
6 rows selected.
SQL> l
  1      merge into cust_summary
  2      using (select cust_id, max(mkt_id) keep(dense_rank first order by sales_volume desc) mkt_id
  3             from cust_and_mkt
  4             group by cust_id
  5            ) cust_and_mkt
  6      on (cust_summary.cust_id = cust_and_mkt.cust_id)
  7      when matched
  8      then
  9*    update set mkt_id = cust_and_mkt.mkt_id
SQL> /
3 rows merged.
SQL> select * from cust_summary;
CUST_ CUST_NAME     MKT_ID               TOTAL_VOLUME
C0001 JOHN EDWARD   M0124                       15000
C0444 SARAH LANE    M0080                      440000
C0878 TOMMY E. CLEK M0505                      450000For more information check the following link.
Merge Oracle10g
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9016.htm#SQLRF01606
Keep Analytics
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions056.htm#SQLRF00641
Merge Oracle9i
http://download.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_915a.htm#SQLRF01606
Hope that helps.
Regards
Raj
P.S : If you are using oracle 9i then when not matched is compulsory and you need to include that clause. For more information check the link on merge statement for Oracle 9i
[Edit : ] Added Oracle version and caveat for merge when using oracle 9i
Message was edited by:
R.Subramanian

Similar Messages

Maybe you are looking for

  • Upgrading old code

    My small company (Academic Software, Inc.) has a few educational games designed (and fairly popular) for very young (or developmentally young) children with severe disabilities. These were produced with Director 7 for Mac and for PC and I recall writ

  • Flash builder 4.7 iOS Debug Binary (IDB) component  is not installed.

    I get the error below consistantly. Is the IDB supposed to be part of the flex 4.6 sdk or part of an air sdk? (I've not seen it in either) Some components required to launch the application were not found. Please reinstall the AIR SDK and try again.

  • How to load xml data into a table

    Hi, i am a newbie. I want to insert the data of xml file into a table. I am doing this using XSU api for java. I am using oracle 9i and jdk 1.7. I am using OracleXmlSave class. but i am getting following error. java.lang.NoClassDefFoundError: oracle/

  • How do I get my Itouch to unlock?

    Unable to slide to unlock or slide to power off, unresponsive. Any suggestions?

  • Unable to open downloaded adobe xi pro trial. windows 7. please help.

    this is giving me a lot of headache. there is no  instruction on what to do next. nothing just happens.