Help on a Query Logic

  • Hi All,

    I have 3 tables with the below Structure.

    Table - 1

    Cust_ID

    Order_Date

    Order_time

    Cust_Name

    Cust_Address

    Order_Status

    Emp_ID

    Cust_Prod_Cost

    Cust_Balance

    Table - 2

    Cust_ID

    Delivered_Time

    Delivered_Qty

    Payment_Type

    Cust_Prod_Cost

    Cust_Balance

    Amount_Paid

    Order_Status

    Emp_ID

    WHID

    Table - 3

    Cust_ID

    Order_Date

    Order_time

    Delivery_Time

    Cust_Prod_Cost

    Delivered_Qty

    Payment_Type

    Amt_To_Pay

    Amount_Paid

    Due_Amount

    Order_Status

    Emp_ID

    WHID

    Now i would like to insert the data from Table - 1 and 2 into Table -3 when Table2-Cust_ID=Table1.Cust_ID and Table2.Order_Status='0'

    Can you please help me out with an SQL Query??. I tried many possible ways, but with no result

  • Hi and welcome to SSC. I would love to help you but you haven't provided enough information for me to help. In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Possibly the easiest method here providing the table schema is setup correctly is to write your insert as a 'select' statement so in this case possibly along the lines of

    SELECT <column 1>,<column 2>,<column 3> .......

    FROM Table1 left outer join Table2

    WHERE <Enter Clause Required>

    once you have the data correct add and 'Insert into Table3' clause above the select to insert into Table 3. Just make sure the columns in your select match the columns in your table.

    Its noit bullet proof but should put you on the right track.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply