#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Update table on real time, query req. Help


    Hi All,

    Here is the scenario, I have a table called "Table1" which updates on real time basis, i want to copy the data from Table1 to my newly created Table2 which is having 2 columns, Number and Count.
    While copying the data from Table1 to Table2 if it founds matches record in "Number" column then it increments it to 2 in Count column.

    Need query for above mentioned scenario.....any help will be remembers in good wordings.....
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,178
    Rep Power
    4274
    use GROUP BY in your SELECT
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    @r937 ,Sorry to say but i need spoon feeding here.
  6. #4
  7. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,178
    Rep Power
    4274
    Originally Posted by farhanmazhari
    @r937 ,Sorry to say but i need spoon feeding here.
    don't be sorry

    but also, don't be so vague

    please show the actual table layouts

    do you already have a query that copies data from table1 to table2? if so, show that as well
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    No I dont have any query, as i dont know how to built it

    Table1

    Number
    12345
    45678
    65432
    12321


    Table2
    Number Count
    12345 2
    45678 1
    65432 0
    12321 5


    Copying records from Table1 to Table2, if any Number comes again then the count increases.
    I hope that will give you the picture.
  10. #6
  11. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,178
    Rep Power
    4274
    Code:
    INSERT
      INTO table2
         ( number
         , count )
    SELECT number
         , COUNT(*)
      FROM table1
    GROUP
        BY number
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0
    So is this code, increases the count value if number comes again? every time I run this query?
  14. #8
  15. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,178
    Rep Power
    4274
    Originally Posted by farhanmazhari
    every time I run this query?
    no, this is new information

    your original request was for a "newly created table"

    you did not say that you wanted to run this query multiple times

    your solution will now be very much more complicated
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo