0

I have two tables, Table1 and Table2.

Table1 has the following columns

  • date
  • u_id
  • t_id
  • col_t1_a
  • col_t1_b
  • col_t1_c

and Table2 has the following columns

  • date
  • ux_id
  • tx_id
  • col_t2_a
  • col_t2_b
  • col_t2_c

Table1.u_id is a sub-string of Table2.ux_id and Table1.t_id = Table2.tx_uid.

I am trying to get all the columns (col_t2_c) from Table2 when all the below conditions are true

  • Table1.t_id = Table2.tc_uid and
  • substring(Table2.ux_id,7,6) = Table1.u_id and
  • Table1.date = Table2.date = 'yymmdd' and
  • Table1.col_t1_a = 'A' and
  • Table1.col_t1_b = 'B' and
  • Table1.col_t1_c = 'C'

I need some help in writing a SQL Server 2008 query to achieve the above.

Any help will be grateful.

3 Answers 3

1

Try this query:

SELECT * 
FROM Table2 T2
INNER JOIN
    Table1 T1 ON T1.t_id = T2.tx_uid
    AND T1.u_id = substring(t2.ux_id, 7, 6)
    AND T1.[Date] = T2.[Date]
WHERE
    T1.[date] = 'date'
    AND T1.col_t1_a = 'A'
    AND T1.col_t1_b = 'B'
    AND t1.col_t1_c = 'C'
Sign up to request clarification or add additional context in comments.

Comments

0

All your conditions are already written. Just put them into query:

SELECT t2.col_t2_c
FROM Table2 t2
INNER JOIN Table1 t1
    ON t1.t_id = t2.tc_uid and 
        substring(t2.ux_id,7,6) = t1.u_id and
        t1.date = t2.date
WHERE   
        t2.date = 'yymmdd' and 
        t1.col_t1_a = 'A' and
        t1.col_t1_b = 'B' and
        t1.col_t1_c = 'C'

Comments

0
SELECT * 
FROM Table1 t1, Table2 t2
WHERE t1.t_id = t2.tx_uid 
AND t1.u_id = substring(t2.ux_id,7,6)
AND t1.date = t2.date
AND t2.date = 'yymmdd'
AND t1.col_t1_a = 'A'
AND t1.col_t1_b = 'B'
AND t1.col_t1_c = 'C'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.