Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How create virtual table and inner join them in a single query
Original Table:
table1 fields:A,B,C,D,E
Table2 Fields:F,G,H,B,K


Virtual table1 fields:B,sum(A)
Virtual table2 fields:B,sum(K)

Join results needed:B,sum(A),sum(K)
Posted
Comments
RDBurmon 25-May-12 4:19am    
Rajin , I think you have got enough solution to solve this . So as a member of CP We would request you to please analyze below solutions and accept the right one and mark your question as [Solved]

Okey , Since you have not specified exact table structure and data so I have considered below

SQL
Table1
 (A BIGINT ,
  B NVARCHAR(50) ,
  C NVARCHAR(50) ,
  D NVARCHAR(50) ,
  E NVARCHAR(50)
 )


SQL
Table2
 (F NVARCHAR(50) ,
  G NVARCHAR(50) ,
  H NVARCHAR(50) ,
  B NVARCHAR(50) ,
  K BIGINT
 )



Table1
ABCDE
10B1C1D1E1
20B2C2D2E2



Table2
FGHBK
F1G1H1B110
F1G2H2B220



And here is the sql proc as per your need

SQL
CREATE TABLE #table1 (B NVARCHAR(10),SumOfA BIGINT,C NVARCHAR(50),D NVARCHAR(50),E NVARCHAR(50))
CREATE TABLE #table2 (B NVARCHAR(10),SumOfK BIGINT,F NVARCHAR(50),G NVARCHAR(50),H NVARCHAR(50))

INSERT INTO #table1
SELECT B,SUM(A),C,D,E FROM Table1 GROUP BY B,C,D,E

INSERT INTO #table2
SELECT B,SUM(K),F,G,H FROM Table2 GROUP BY B,F,G,H

SELECT B,SumOfA,SumOfK,C,D,E,F,G,H FROM #table1 T1 Inner Join #table2 T2 On T1.A=T2.A 




Hope this helps , If yes then plz accept and vote the answer. Any queries / questions on this are always welcome.

Thanks & Regards
RDBurmon.Sr.Software Engineer
 
Share this answer
 
v3
Comments
Maciej Los 25-May-12 4:15am    
Good one, but you're late ;)
5 of course!
RDBurmon 25-May-12 4:46am    
Ya I was typing the answer. But failed three time to upload it. Lastly uploaded without any issue
Maciej Los 25-May-12 4:53am    
Sometimes it happens ;)
RDBurmon 25-May-12 5:14am    
Yeah, I agree
Manas Bhardwaj 25-May-12 4:38am    
nice . +5
The answer of Manas Bhardwaj is good, but i prefer to use aliases[^], like this:
SQL
SELECT t1.B, SUM(t1.A), SUM(t2.K)
FROM Table1 AS t1 INNER JOIN Table2 AS t2 ON t1.B = t2.B
GROUP BY t1.A, t1.B, t2.K

In my opinion it looks more readible ;)

If youwould like to create temporary table, use # with CREATE TABLE[^] command, for example

SQL
CREATE TABLE #MyTempTable (
    ID INT NOT NULL,
    Name VARCHAR(30) NULL)

--usage:
INSERT INTO #MyTampTable (ID, Name)
    VALUES(1,'SomeText')

SELECT *
FROM #MyTempTable

DROP TABLE #MyTempTable
 
Share this answer
 
Comments
RDBurmon 25-May-12 4:17am    
My 5
Maciej Los 25-May-12 4:50am    
Thank you, RDBurmon ;)
RDBurmon 25-May-12 5:15am    
welcome
Manas Bhardwaj 25-May-12 4:39am    
nice +5
Maciej Los 25-May-12 4:50am    
Thank you, Manas ;)
SQL
SELECT
    Table1.B,
    SUM(Table1.A),
    SUM(Table2.K)
FROM
    Table1 INNER JOIN Table2 ON
        Table1.B = Table2.B
GROUP BY Table1.A, Table1.B, Table2.K
 
Share this answer
 
Comments
Maciej Los 25-May-12 4:07am    
Good answer, My 5!
Manas Bhardwaj 25-May-12 4:39am    
thanks!
rajin kp 25-May-12 4:46am    
thanks
Prasad_Kulkarni 25-May-12 5:12am    
Good answer Manas! 5!
Manas Bhardwaj 25-May-12 5:16am    
thanks! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900