SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert from one table to another

    Table A:
    id | pnumber | type
    ----------------------
    45 | 1111 | a
    98 | 1314 | a
    223 | 2151 | b
    45 | 1111 | a

    How can i get the values 'name' and 'pnumber' from Table A where type='a' and insert it into table B
    and if there is 2 rows with the same id and pnumber it will only add 1 row and write add the sum to the total
    for ex:
    Table B should look like that:
    id | pNumber | Total Found
    -----------------------
    45 | 1111 | 2
    98 | 1314 | 1
    223| 2151 | 1

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Code:
    INSERT
      INTO tableB
    SELECT id
         , pnumber
         , COUNT(*)
      FROM tableA
    GROUP
        BY id
         , pnumber
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •