×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

here is details. thanks.

本文发表在 rolia.net 枫下论坛Now there are some data in invvendor.CATALOGCODE, i want to move it to inventory.il1. for one itemnum in inventory, there are more than one invvendor records.
For example: for inventory.itemnum=1, there are 3 related records: invvendor.catalogcode=a, invvendor.catalogcode=b, invvendor.catalogcode=c.
Now I want to change inventory.il1="a,b,c"




SQL> desc inventory;
Name Null? Type
------------------------------- -------- ----
ITEMNUM NOT NULL VARCHAR2(30)
LOCATION NOT NULL VARCHAR2(8)
BINNUM VARCHAR2(8)
VENDOR VARCHAR2(8)
MANUFACTURER VARCHAR2(8)
MODELNUM VARCHAR2(8)
CATALOGCODE VARCHAR2(30)
MINLEVEL NOT NULL NUMBER(15,2)
MAXLEVEL NOT NULL NUMBER(15,2)
CATEGORY NOT NULL VARCHAR2(4)
ORDERUNIT VARCHAR2(8)
ISSUEUNIT VARCHAR2(8)
CONVERSION NOT NULL NUMBER(15,2)
ORDERQTY NOT NULL NUMBER(15,2)
STDCOST NOT NULL NUMBER(10,2)
AVGCOST NOT NULL NUMBER(10,2)
LASTCOST NOT NULL NUMBER(10,2)
LASTISSUEDATE DATE
ISSUEYTD NOT NULL NUMBER(15,2)
ISSUE1YRAGO NOT NULL NUMBER(15,2)
ISSUE2YRAGO NOT NULL NUMBER(15,2)
ISSUE3YRAGO NOT NULL NUMBER(15,2)
ABCTYPE VARCHAR2(1)
CCF NOT NULL NUMBER
SSTOCK NUMBER(15,2)
DELIVERYTIME NOT NULL NUMBER
LDKEY NUMBER
IL1 VARCHAR2(10)
IL2 VARCHAR2(10)
IL3 VARCHAR2(10)
IL4 DATE
IL5 NUMBER(15,2)
IL6 VARCHAR2(1)
IL7 VARCHAR2(1)
IL8 VARCHAR2(1)
IL9 VARCHAR2(1)
IL10 VARCHAR2(1)
GLACCOUNT VARCHAR2(20)
CONTROLACC VARCHAR2(20)
SHRINKAGEACC VARCHAR2(20)
INVCOSTADJACC VARCHAR2(20)
SOURCESYSID VARCHAR2(10)
OWNERSYSID VARCHAR2(10)
EXTERNALREFID VARCHAR2(10)
APISEQ VARCHAR2(50)
INTERID VARCHAR2(50)
MIGCHANGEID VARCHAR2(50)
SENDERSYSID VARCHAR2(50)
ROWSTAMP NOT NULL VARCHAR2(40)


SQL> desc invvendor;
Name Null? Type
------------------------------- -------- ----
ITEMNUM NOT NULL VARCHAR2(30)
VENDOR VARCHAR2(8)
MANUFACTURER VARCHAR2(8)
MODELNUM VARCHAR2(8)
CATALOGCODE VARCHAR2(30)
LDKEY NUMBER
IV1 VARCHAR2(10)
IV2 VARCHAR2(10)
IV3 VARCHAR2(1)
IVCO1 VARCHAR2(10)
IVCO2 DATE
IVCO3 NUMBER(15,2)
TAX1CODE VARCHAR2(8)
TAX2CODE VARCHAR2(8)
TAX3CODE VARCHAR2(8)
BIDPRICE NOT NULL NUMBER(10,2)
BIDDATE DATE
PROMDELIVERYTIME NOT NULL NUMBER
TAX4CODE VARCHAR2(8)
TAX5CODE VARCHAR2(8)
LASTCOST NUMBER(10,2)
LASTDATE DATE
CATALOGWEBPAGE VARCHAR2(124)
SOURCESYSID VARCHAR2(10)
OWNERSYSID VARCHAR2(10)
EXTERNALREFID VARCHAR2(10)
APISEQ VARCHAR2(50)
INTERID VARCHAR2(50)
MIGCHANGEID VARCHAR2(50)
SENDERSYSID VARCHAR2(50)
ORDERUNIT VARCHAR2(8)
CONVERSION NUMBER(15,2)
ISDEFAULT NOT NULL VARCHAR2(1)
ROWSTAMP NOT NULL VARCHAR2(40)更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / One question about Oracle.
    The relationnship between Table1 and Table2 is one to multi.
    Now I want to copy data from Table2.field1 to Table1.field1.
    But now, I need to merge the data in Table2.field1 before I copy this data into Table1.field1.
    For example, for one related item: Table2.Field1 has "1","2","3","4" values, now I need to copy "1,2,3,4" in to Table.Field1.
    Must I write program to solve this problem, or there is easy way to do this just by sql in oracle?
    • So easy ! Think it by yourself !
    • Write a store procedure if possible. I already feel the pain of terrible design ...:-)
      • Just SQL Statement is OK
        • Ok, then tell me. I am new to Oracle.
          • inside
            insert into table1(filed1,filed2,...)
            select exp1,t2.fld2,...
            from (select fld2 from table2 where ...) t2
            where ...
          • 看起来你是在工作,还是用个store procedure吧, 我记得单行SQL是不行的
    • Do you mean create one string which contains all distinct values of table2.field1 and then insert into table1 as one row?
    • try this statement: inert into table1(field1) select distinct field1 from table2; (it is correct when table1 has no other not null columns)
      • Hi, Thanks for answering my question about oracle. I tried the following method but not successful.
        Do you mind have a look at my SQL to help me find out what is the problem?


        update inventory
        set il1 =
        (select exp1, t2.itemnum
        from
        (select invvendor.itemnum from invvendor) t2
        where
        inventory.itemnum=t2.itemnum)

        relationship between inventory and invvendor is
        inventory.itemnum(1)----(multi)invvendor.itemnum
        I want to copy catalog from invvendor to inventory
        • This SQL statement can not work. I need more information about your question.
          Please use : DESC <your tablename>;
          to list these two tables' structure.

          Then, Tell me really what you want to do.

          Merge data or select data or sort data.

          I'll wait your feedback.
          • here is details. thanks.
            本文发表在 rolia.net 枫下论坛Now there are some data in invvendor.CATALOGCODE, i want to move it to inventory.il1. for one itemnum in inventory, there are more than one invvendor records.
            For example: for inventory.itemnum=1, there are 3 related records: invvendor.catalogcode=a, invvendor.catalogcode=b, invvendor.catalogcode=c.
            Now I want to change inventory.il1="a,b,c"




            SQL> desc inventory;
            Name Null? Type
            ------------------------------- -------- ----
            ITEMNUM NOT NULL VARCHAR2(30)
            LOCATION NOT NULL VARCHAR2(8)
            BINNUM VARCHAR2(8)
            VENDOR VARCHAR2(8)
            MANUFACTURER VARCHAR2(8)
            MODELNUM VARCHAR2(8)
            CATALOGCODE VARCHAR2(30)
            MINLEVEL NOT NULL NUMBER(15,2)
            MAXLEVEL NOT NULL NUMBER(15,2)
            CATEGORY NOT NULL VARCHAR2(4)
            ORDERUNIT VARCHAR2(8)
            ISSUEUNIT VARCHAR2(8)
            CONVERSION NOT NULL NUMBER(15,2)
            ORDERQTY NOT NULL NUMBER(15,2)
            STDCOST NOT NULL NUMBER(10,2)
            AVGCOST NOT NULL NUMBER(10,2)
            LASTCOST NOT NULL NUMBER(10,2)
            LASTISSUEDATE DATE
            ISSUEYTD NOT NULL NUMBER(15,2)
            ISSUE1YRAGO NOT NULL NUMBER(15,2)
            ISSUE2YRAGO NOT NULL NUMBER(15,2)
            ISSUE3YRAGO NOT NULL NUMBER(15,2)
            ABCTYPE VARCHAR2(1)
            CCF NOT NULL NUMBER
            SSTOCK NUMBER(15,2)
            DELIVERYTIME NOT NULL NUMBER
            LDKEY NUMBER
            IL1 VARCHAR2(10)
            IL2 VARCHAR2(10)
            IL3 VARCHAR2(10)
            IL4 DATE
            IL5 NUMBER(15,2)
            IL6 VARCHAR2(1)
            IL7 VARCHAR2(1)
            IL8 VARCHAR2(1)
            IL9 VARCHAR2(1)
            IL10 VARCHAR2(1)
            GLACCOUNT VARCHAR2(20)
            CONTROLACC VARCHAR2(20)
            SHRINKAGEACC VARCHAR2(20)
            INVCOSTADJACC VARCHAR2(20)
            SOURCESYSID VARCHAR2(10)
            OWNERSYSID VARCHAR2(10)
            EXTERNALREFID VARCHAR2(10)
            APISEQ VARCHAR2(50)
            INTERID VARCHAR2(50)
            MIGCHANGEID VARCHAR2(50)
            SENDERSYSID VARCHAR2(50)
            ROWSTAMP NOT NULL VARCHAR2(40)


            SQL> desc invvendor;
            Name Null? Type
            ------------------------------- -------- ----
            ITEMNUM NOT NULL VARCHAR2(30)
            VENDOR VARCHAR2(8)
            MANUFACTURER VARCHAR2(8)
            MODELNUM VARCHAR2(8)
            CATALOGCODE VARCHAR2(30)
            LDKEY NUMBER
            IV1 VARCHAR2(10)
            IV2 VARCHAR2(10)
            IV3 VARCHAR2(1)
            IVCO1 VARCHAR2(10)
            IVCO2 DATE
            IVCO3 NUMBER(15,2)
            TAX1CODE VARCHAR2(8)
            TAX2CODE VARCHAR2(8)
            TAX3CODE VARCHAR2(8)
            BIDPRICE NOT NULL NUMBER(10,2)
            BIDDATE DATE
            PROMDELIVERYTIME NOT NULL NUMBER
            TAX4CODE VARCHAR2(8)
            TAX5CODE VARCHAR2(8)
            LASTCOST NUMBER(10,2)
            LASTDATE DATE
            CATALOGWEBPAGE VARCHAR2(124)
            SOURCESYSID VARCHAR2(10)
            OWNERSYSID VARCHAR2(10)
            EXTERNALREFID VARCHAR2(10)
            APISEQ VARCHAR2(50)
            INTERID VARCHAR2(50)
            MIGCHANGEID VARCHAR2(50)
            SENDERSYSID VARCHAR2(50)
            ORDERUNIT VARCHAR2(8)
            CONVERSION NUMBER(15,2)
            ISDEFAULT NOT NULL VARCHAR2(1)
            ROWSTAMP NOT NULL VARCHAR2(40)更多精彩文章及讨论,请光临枫下论坛 rolia.net
            • OK. I think you need a loop to handle this problem.
              If it is number, we can sum it up. But for varchar, we need to append it
              one by one. In this case, we need loop function to do that because the
              update only run once.
              • thanks
        • Your SQL might look like this. Because:
          update inventory
          set il1=
          (
          select inventory.exp1
          from
          (select itemnum from inwendor) t2, inventory
          where
          inventory.itemnum = t2.itemnum
          );

          Note: You want to update one field (il1) so you will not select two
          fields in your sub-query.

          Wish this can help you.
        • Update doesn't work at this case. Why not try Insert?
          • they are not new records.
      • 如果table2中filed1的值不可以作table1中的PK怎么办?
    • you can not do it in one simple SQL statement, you can try T-SQL or PL-SQL, which means stored procedure solution.
      Straight SQL can not do a pivot table query with an unknown number of columns.

      One can do a generalized pivot table query using either a cursor or T-SQL (T-SQL
      supports the WHILE statement which enables one to loop). But, to repeat, one
      SELECT statement by itself can not produce a generalized pivot table. BTW, this
      is not just my opinion, this is proven mathematical fact.
    • hope this one will help you.
      本文发表在 rolia.net 枫下论坛/*
      Too bad MS SQL Server does not have an aggregate function
      like SUM() that worked on character columns and
      concatenated the values.

      Instead, we have to use techniques similar to cross tabulation
      queries.

      Two example algorithms are listed below. One works if there is
      a limited number of items to concatenate together, and has good
      performance. The other does not have that limitation (although
      there is another limitation), but does not perform as well.

      The first section below generates random sample data.
      */

      -------------------------------------------------
      --SAMPLE DATA
      -------------------------------------------------

      /*
      data table columns:

      id: object id.
      like a person id.
      may be multiple records with same id
      in the table.
      item: value to be concatenated.
      each row for a particular id may have
      different values for item.
      sort_column: sort order.
      item's for a particular id will be concatenated
      in ascending order of this column.
      if sort order not needed, see comments below
      for simplifying the select statement.
      pk: primary key.
      unique identifier for row in table.

      The routine below generates sample data for
      10 ids with 1 - 4 items per id. Each item
      and sort_column value is a random integer 0 - 9.
      */

      --create table.
      if (object_id("data") is not null) drop table data
      go
      create table data(id int, item varchar(10),
      sort_column int, pk int primary key)
      go
      --populate table with random data.
      set nocount on
      declare @id int
      declare @cnt int
      declare @pk int
      select @pk = 0
      select @id = 0
      while (@id<10)
      begin
      select @id = @id + 1
      select @cnt = floor(rand()*4)+1
      while (@cnt > 0)
      begin
      select @cnt = @cnt - 1
      select @pk = @pk + 1
      insert into data values (
      @id,
      convert(varchar, floor(rand()*10)),
      floor(rand()*10),
      @pk
      )
      end
      end
      set nocount off
      go
      -------------------------------------------------

      /*
      -------------------------------------------------
      SAMPLE DATA
      -------------------------------------------------

      id item sort_column pk
      ----------- ---------- ----------- -----------
      1 2 1 1
      1 5 4 2
      1 9 5 3
      2 7 1 4
      2 6 9 5
      3 9 3 6
      3 6 9 7
      4 1 6 8
      4 0 9 9
      4 7 4 10
      4 6 7 11
      5 6 3 12
      5 6 2 13
      6 5 8 14
      6 3 4 15
      6 5 1 16
      7 3 7 17
      7 4 8 18
      7 4 4 19
      8 9 7 20
      8 8 3 21
      8 1 9 22
      8 9 0 23
      9 6 0 24
      9 7 0 25
      9 2 1 26
      10 5 1 27
      10 5 3 28
      10 3 8 29

      (29 row(s) affected)



      -------------------------------------------------
      DESIRED RESULT FROM SAMPLE DATA
      -------------------------------------------------

      id items
      ----------- ----------------------------------------------
      1 2, 5, 9
      2 7, 6
      3 9, 6
      4 7, 1, 6, 0
      5 6, 6
      6 5, 3, 5
      7 4, 3, 4
      8 9, 8, 9, 1
      9 6, 7, 2
      10 5, 5, 3

      (10 row(s) affected)


      -------------------------------------------------
      */


      -------------------------------------------------
      --LIMITED ROWS PER ID.
      -------------------------------------------------
      /*
      If you can limit the number of items per ID to a small number, you can get
      it all into one query, which will probably give you better performance.

      Example is for maximum of 4 rows per id. Repeat a "+ isnull(...)" section
      up to the maximum number of rows per id, incrementing the n in ") = <n>"
      for each section.

      If item values can be sorted by pk insted of a sort_column column,
      the query can be simplified quite a bit. Change...
      | and (
      | (data2.sort_column < data1.sort_column)
      | or (
      | (data2.sort_column = data1.sort_column)
      | and (data2.pk <= data1.pk)
      | )
      | )
      ...to...
      | and (data2.pk <= data1.pk)
      ...in each "isnull(...)" section.
      */
      select
      id,
      items =
      (
      select item
      from data data1
      where data1.id = data.id
      and (
      select count(*)
      from data data2
      where (data2.id = data1.id)
      and (
      (data2.sort_column < data1.sort_column)
      or (
      (data2.sort_column = data1.sort_column)
      and (data2.pk <= data1.pk)
      )
      )
      ) = 1
      )
      + isnull(
      (
      select ', ' + item
      from data data1
      where data1.id = data.id
      and (
      select count(*)
      from data data2
      where (data2.id = data1.id)
      and (
      (data2.sort_column < data1.sort_column)
      or (
      (data2.sort_column = data1.sort_column)
      and (data2.pk <= data1.pk)
      )
      )
      ) = 2
      ),
      '')
      + isnull(
      (
      select ', ' + item
      from data data1
      where data1.id = data.id
      and (
      select count(*)
      from data data2
      where (data2.id = data1.id)
      and (
      (data2.sort_column < data1.sort_column)
      or (
      (data2.sort_column = data1.sort_column)
      and (data2.pk <= data1.pk)
      )
      )
      ) = 3
      ),
      '')
      + isnull(
      (
      select ', ' + item
      from data data1
      where data1.id = data.id
      and (
      select count(*)
      from data data2
      where (data2.id = data1.id)
      and (
      (data2.sort_column < data1.sort_column)
      or (
      (data2.sort_column = data1.sort_column)
      and (data2.pk <= data1.pk)
      )
      )
      ) = 4
      ),
      '')
      from
      data
      group by
      id
      go
      -------------------------------------------------



      -------------------------------------------------
      --UNLIMITED ROWS PER ID.
      -------------------------------------------------
      /*
      If you want to have an unlimited or high number of items per ID, you
      can't avoid using a temp table and looping. However, you can avoid
      using a cursor.

      This algorithm will work for an unlimited number of items per id.
      However, performance is not as good because the routine is procedural,
      uses a temp table, cannot be used in a view, and probably slower.

      The output of algorithm is still limited by the length of the column
      that holds the concatenated data.

      If item values can be sorted by pk insted of a sort_column column,
      the query can be simplified quite a bit. Change...
      | and (
      | (data2.sort_column < data1.sort_column)
      | or (
      | (data2.sort_column = data1.sort_column)
      | and (data2.pk <= data1.pk)
      | )
      | )
      ...to...
      | and (data2.pk <= data1.pk)
      ...in each "where @cnt = (...)" section.
      */
      set nocount on
      --temp table to hold results
      create table #results (id int, items varchar(40))
      --counter variable.
      declare @cnt int
      set @cnt = 1
      --insert id and first item.
      insert into #results (id, items)
      select
      id,
      items =
      (
      select item
      from data data1
      where @cnt =
      (
      select count(*)
      from data data2
      where (data2.id = data1.id)
      and (
      (data2.sort_column < data1.sort_column)
      or (
      (data2.sort_column = data1.sort_column)
      and (data2.pk <= data1.pk)
      )
      )
      )
      and (data1.id = data.id)
      )
      from data
      group by id
      --append other items.
      while 1=1 --loop until no more updates.
      begin
      set @cnt = @cnt + 1
      update #results
      set items = items + ', ' + item
      from
      #results,
      (
      select id, item
      from data data1
      where @cnt =
      (
      select count(*)
      from data data2
      where (data2.id = data1.id)
      and (
      (data2.sort_column < data1.sort_column)
      or (
      (data2.sort_column = data1.sort_column)
      and (data2.pk <= data1.pk)
      )
      )
      )
      ) afltr
      where
      afltr.id = #results.id
      --break loop if nothing updated.
      if @@rowcount = 0 break
      end
      set nocount off
      --display.
      select id, items
      from #results
      order by id
      --clean up
      drop table #results
      go
      -------------------------------------------------


      "Microsoft" <msullivan@getconnected.com> wrote in message
      news:Ou1sEOmsAHA.684@tkmsftngp04...
      I am looking to do the following without using a cursor.

      I have two tables:
      Source (i int, desc varchar(10))
      Destination (i int primary key, desc
      varchar(2000))

      I would like to append all desc of each i value into a second table
      A value in Destination may have (0 - n) number of rows in Source.


      For example:

      Source (1, 'A')
      Source (1, 'B')
      Source (1, 'Q')
      Source (1, 'Z')
      Source (2, 'A')
      Source (3, 'C')
      Source (4, 'B')
      Source (4, 'Q')

      Would result in:

      Destination (1,'ABQZ')
      Destination (2,'AC')
      Destination (3,'C')
      Destination (4,'BQ')

      Any ideas?更多精彩文章及讨论,请光临枫下论坛 rolia.net