SQL Query question

Jayesh Dave 296 Reputation points
2022-09-14T16:00:43.897+00:00

Hello:

I have 2 tables where it doesn't have standard\common columns and don't have the same columns. There is no relationship between Table Cust_Tab" and "Table_Schema_Info.

Attached sample data file.

Any pointer is appreciated.

Thanking in advance.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
{count} votes

Answer accepted by question author
  1. LiHongMSFT-4306 31,616 Reputation points
    2022-09-15T02:44:22.707+00:00

    Hi @Jayesh Dave
    If the final Output does not include the Table_Name and Column_Name column, then you could use CROSS JOIN (Cartesian Product).
    Check this:

    SELECT *   
    FROM (SELECT DISTINCT Customer_ID FROM Table_Schema_Info) S CROSS JOIN Cust_Tab  
    

    Output:
    241230-image.png

    If you need both Table_Name and Column_Name columns appear in the final output, then you could use ROW_NUMBER() to create unique RNum column for both tables, and then JOIN with these two RNum columns. Like this:

    SELECT B.Customer_ID,A.TicketID,A.FirstName,A.LastName,A.DATEOPENED,B.Table_Name,B.Column_Name   
    FROM (SELECT *,ROW_NUMBER()OVER(ORDER BY TicketID)AS RNum_A FROM Cust_Tab) A  
         FULL JOIN   
         (SELECT *,ROW_NUMBER()OVER(PARTITION BY Customer_ID ORDER BY Column_Name)AS RNum_B FROM Table_Schema_Info)B  
         ON A.RNum_A=B.RNum_B  
    

    Output:
    241255-image.png

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Naomi Nosonovsky 8,881 Reputation points
    2022-09-14T17:36:52.75+00:00

    Can you post some sample data? How do you want to match records from one table against the other?

    0 comments No comments

  2. Jayesh Dave 296 Reputation points
    2022-09-14T19:04:49.8+00:00

    I have attached the txt file, and please look for 2 tabs - "Cust_Tab" and "Table_Schema_Info". These are 2 tables that I mentioned in my question which don't have common columns and no relationship.

    Thank you for helping me out.
    241191-sample-data-2.txt


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.