简体   繁体   中英

Join two tables in one table using SQL

Good evening,

I have an issue trying to find a solution to join two tables :

For example I have

Table 1 :

Date  | Item
12/03 | aaaa
12/03 | aaaa
14/03 | bbbb
14/03 | aaaa
15/03 | cccc

Table 2 :

Date  | Item2
11/03 | aaaa
12/03 | aaaa
13/03 | bbbb
14/03 | aaaa
15/03 | cccc   

I want to do a count to have this

Date  | Count(Item1) | Count(Item2)
11/03 | 0            | 1
12/03 | 2            | 1
13/03 | 0            | 1
14/03 | 2            | 1
15/03 | 1            | 1

I have tried this so far, but it doesn't seems to work, it only give me the commun dates :

SELECT F.DATE, COUNT(T1.Item1), COUNT(T2.Item2) FROM TABLE1 
T1 LEFT JOIN TABLE2 T2 ON T1.date=T2.date

Date  | Count(Item1) | Count(Item2)
12/03 | 2            | 1
14/03 | 2            | 1
15/03 | 1            | 1

Any help ?

Thanks

In the below SQL, the first inline view is computing all the counts at date level and same applies with second query. By combining the results of left join and right join between these two inline views we can combine all the common and uncommon results from both tables as you described.


        SELECT Date,
               Item1,
               Item2 
          FROM
             (  
                SELECT T1.Date AS Date,
                       COUNT(  T1.Item  ) AS Item1,                     
                       COUNT(  T2.Item  ) AS Item2  
                  FROM Table1 t1
                LEFT JOIN
                       Table2 t2  
                    ON t1.date = t2.date  
                GROUP BY t1.Date              
          UNION              
                SELECT T2.Date AS Date,
                       COUNT( T1.Item ) AS Item1,
                       COUNT( T2.Item ) AS Item2
                  FROM Table2 t2
                LEFT JOIN
                       Table1 t1   
                    ON t2.date = t1.date
                GROUP BY t2.Date
              ) A
         ORDER BY Date        
        ;

SQL Fiddle Implementation :-

http://sqlfiddle.com/#!9/5b872/14

I think this is what you need - you group each table on the date using a count and then you add values from both grouped tables:

DECLARE @tab1 TABLE
(
    [Date] VARCHAR(5),
    Item1 VARCHAR(100)
)

DECLARE @tab2 TABLE
(
    [Date] VARCHAR(5),
    Item2 VARCHAR(100)
)

INSERT INTO @tab1 ( Date, Item1 ) VALUES 
('12/03','aaaa'),
('12/03','aaaa'),
('14/03','bbbb'),
('14/03','aaaa'),
('15/03','cccc')

INSERT INTO @tab2 ( Date, Item2 ) VALUES 
('11/03','aaaa'),
('12/03','aaaa'),
('13/03','bbbb'),
('14/03','aaaa'),
('15/03','cccc')


;WITH grp AS (
SELECT [Date],COUNT(*) AS Item1, 0 AS Item2 FROM @tab1 
GROUP BY [Date]

UNION

SELECT [Date],0, COUNT(*) FROM @tab2 
GROUP BY [Date] )

SELECT grp.Date, SUM(grp.Item1) AS [CountItem1], SUM(grp.Item2) AS [CountItem2] FROM grp
GROUP BY grp.Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM