-1

What I exactly want to do is like this:

  1. I have two tables, Table 1 has 2 columns A (statements) and column B (userids)
  2. Table 2 has columns G (username) and H (userids)

Column B and column H hold the same type of data (integer) but column names are different.

Output: I need to have both the column from table 1 i.e. column A and column B and column G from table 2 based on column H.

Basically, for every statement in table one it has corresponding userids, but usernames are there in table 2. So how can I map it. I know we need to write join here. but the concern is as below

Secondly, the trouble is table 1 is output of a select query . So can we input a select query as table 1 while writing a join?

UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51
  • 1
    #user10914967- you should show your table and required table example to get proper solution. – Zakir Hossain Jan 31 '19 at 03:04
  • 1
    Examples of the actual tables, sample data, expected output, and SQL tried so far as all very helpful. – UnhandledExcepSean Jan 31 '19 at 03:09
  • 1
    I find it hard to understand what the real issue is. It sounds like you are asking how to join two tables which you should be able to find on about the second page of every SQL book or tutorial. As to your second question: you access a result by putting it in parentheses, e.g. `select * from ( ) table_alias;`. – Thorsten Kettner Jan 31 '19 at 07:22
  • 1
    1. Show sample data for the two tables and the expected result. 2. Tell us what DBMS you are using. 3. Show us your query and tell us where you are stuck. – Thorsten Kettner Jan 31 '19 at 07:23

1 Answers1

0

These are called derived tables and are very common. The follow a pattern like this:

SELECT *
FROM TABLE1
INNER JOIN (
   SELECT *
   FROM TABLE2
) AS T2 ON TABLE1.COLUMN=T2.COLUMN

The inner join's SQL should be able to be run standalone to produce the records you expect for that "table".

The accepted answer to this question is a good example.

UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51
  • wow, wonderful, thanks for all your quick and exact help. Can I please ask you one more thing ? instead of the first *, I would write column name as I am looking for distinct statements and get the corresponding data for that. – user10914967 Jan 31 '19 at 05:20
  • @user10914967 It can; you should look up examples. There should be plenty in stack overflow or the internet at large – UnhandledExcepSean Feb 01 '19 at 00:26
  • Thanks again for being so quick and answering.this is error message:ORA-00932: inconsistent datatypes: expected - got CLOB 00932. 00000 - "inconsistent datatypes: expected %s got %s" *Cause: *Action: Error at Line: 1 Column: 17 I am checking google as well. – user10914967 Feb 01 '19 at 00:27
  • @user10914967 are you using oracle? – UnhandledExcepSean Feb 01 '19 at 00:28
  • Oracle SQL developer. Version 18.4.0.376 – user10914967 Feb 01 '19 at 00:30
  • @user10914967 that is a critical piece of information for all future sql questions by the way. Check out this https://www.techonthenet.com/oracle/distinct.php – UnhandledExcepSean Feb 01 '19 at 00:31
  • Sure, Thank you so much. – user10914967 Feb 01 '19 at 00:32
  • @user10914967 oracle specific derived table example and documentation: https://docs.oracle.com/cd/E57185_01/IRWUG/ch03s07.html – UnhandledExcepSean Feb 01 '19 at 00:33
  • Thank you very much,The issue was "CLOB" data type.. figured it out that cant find distinct on that.. Thank you everyone for help.... Now, the solution is I am exporting it as CSV (excel is too big to export as it has many rows and then after exporting converting to excel and using excel doing a "Remove duplicate") – user10914967 Feb 04 '19 at 05:35