简体   繁体   English

使用SQL在一个表中联接两个表

[英]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 : 表格1 :

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

Table 2 : 表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 :- SQL Fiddle实现:-

http://sqlfiddle.com/#!9/5b872/14 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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