Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2009
    Posts
    79

    Unanswered: Moving data from one database table to others on the basis of criteria

    Hi All,

    I have huge amount of data in text files and I need to upload the same in an sql server database tables Now for e.g. I have text file which contains data for entire 2009 year but I have a sql database tables for each quater separately.
    For e.g. I have following tables.
    1) Data_2009Qtr1
    2) Data_2009Qtr2
    3) Data_2009Qtr3
    4) Data_2009Qtr4

    Now what I want to do is that I will import the data into a table named as Data_2009FullYear. Now I require a query which will copy / move data to above mentioned quarter tables from Data_2009Fullyear database table. for e.g. I have a Period column in my data where we have months in numbers like 1,2,3,4 and so on, so the query should check the column "Period" in Data_2009FullYear and if it finds the rows / records in which in column period the numbers is between 1 to 3 then it should copy / move that data records to Data_2009Qtr1 and if it between 4 to 6 then it should copy data records to Data_2009Qtr2.

    Thanks a lot for your help in advance.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    you need to look up the documentation for the INSERT statement using the SELECT option (i.e. not the VALUES option)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Sep 2009
    Posts
    79

    Moving data from one database table to others on the basis of criteria

    Hi All,

    I am not very good at sql, can you please provide me an example.

    Thanks a lot for your help in advance.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    here's an example --
    Code:
    INSERT
      INTO quistipunctous
         ( scrimflabat 
         , frabilgimjer
         , horbliston )
    SELECT haplistplogget
         , opsilfrummer 
         , quistipunctous
      FROM whipfintaggle
     WHERE gromulphastic > 937
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Sep 2009
    Posts
    79

    Moving data from one database table to others on the basis of criteria

    Hi,

    Thanks a lot for your reply.. but I am unable to figure out that which is the table name which is the column name in the below query. Following are my details

    my database name : DATA_META
    my database table name which contains full year data : Data_2009FullYear
    Database tables in which I want to move the data : Data_2009Qtr1, Data_2009Qtr2, Data_2009Qtr3 and Data_2009Qtr4.
    Common column in the above mentioned database tables : Period

    Can you please provide me an example using above parameters. I want to select all columns when moving the data from Data_2009FullyYear to quarterly tables but using month criteria from period column which is common in fullyear and quarterly tables. Something like Select * from Data_2009Fullyear and copy to quarterly tables based on period criteria.

    Thanks a lot for your help in advance.
    Last edited by abhay_547; 07-11-10 at 03:32.

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by abhay_547 View Post
    ... but I am unable to figure out that which is the table name which is the column name in the below query.
    okay, i have made it a little easier for you

    the table names are in blue and the column names are in red --
    Code:
    INSERT
      INTO quistipunctous
         ( scrimflabat 
         , frabilgimjer
         , horbliston )
    SELECT haplistplogget
         , opsilfrummer 
         , quistipunctous
      FROM whipfintaggle
     WHERE gromulphastic > 937
    the INSERT INTO table is the one that is receiving the rows being copied, and the SELECT FROM table name is the one which has the rows that are to be copied over

    the WHERE clause is where you put the condition on the period for the rows to be copied
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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