按日期联接两个表,并在一个表缺少日期时填充缺少的数据

时间:2020-05-24 04:39:48

标签: sql sql-server

在SQL Server中,我有两个表(tbl1tbl2),如下所示。 tbl2包含所有日期的列表。在tbl1中,某些日期丢失了。

tbl1

name    nameid  count   date
-----------------------------------------------
apple     1      77     2019-07-29 00:00:00.000
orange    2     129     2019-07-29 00:00:00.000
apple     1     399     2019-08-05 00:00:00.000
orange    2      27     2019-08-05 00:00:00.000
apple     1     122     2019-08-12 00:00:00.000
orange    2       5     2019-08-19 00:00:00.000

tbl2

    timeid     date
5   2019-07-29 00:00:00.000
4   2019-08-05 00:00:00.000
3   2019-08-12 00:00:00.000
2   2019-08-19 00:00:00.000
1   2019-08-26 00:00:00.000

我需要的是下表。我需要加入两个表,填充缺少日期的名称和nameid,并将这些计数设置为0。

date                    name nameid count   timeid
---------------------------------------------------
2019-07-29 00:00:00.000 apple   1     77    5
2019-07-29 00:00:00.000 orange  2     129   5
2019-08-05 00:00:00.000 apple   1     399   4
2019-08-05 00:00:00.000 orange  2     27    4
2019-08-12 00:00:00.000 apple   1     122   3
2019-08-12 00:00:00.000 orange  2     0     3
2019-08-19 00:00:00.000 apple   1     0     2
2019-08-19 00:00:00.000 orange  2     5     2
2019-08-26 00:00:00.000 apple   1     0     1
2019-08-26 00:00:00.000 orange  2     0     1

我做了以下事情:

SELECT t1.date, name, nameid, count, timeid 
FROM tbl2 t1
LEFT JOIN tbl1 t2 ON t1.date = t2.date

并获得以下输出:

date                    name     nameid    count    timeid
----------------------------------------------------------
2019-07-29 00:00:00.000 apple       1        77      5
2019-07-29 00:00:00.000 orange      2       129      5
2019-08-05 00:00:00.000 apple       1       399      4
2019-08-05 00:00:00.000 orange      2        27      4
2019-08-12 00:00:00.000 apple       1       122      3
2019-08-19 00:00:00.000 orange      2         5      2
2019-08-26 00:00:00.000 NULL        NULL    NULL     1

似乎不正确。有谁知道我如何生成所需的联接表?

下面是生成tbl1tbl2

的代码
DROP TABLE IF EXISTS tbl1

CREATE TABLE tbl1 
(
    name VARCHAR (50) NOT NULL,
    nameid INT NOT NULL,
    count INT NOT NULL,
    date DATETIME
);

INSERT INTO tbl1(name, nameid, count, date)
VALUES ('apple', 1, 77, '2019-07-29'),
        ('orange', 2, 129, '2019-07-29'),
        ('apple', 1, 399, '2019-08-05'),
         ('orange', 2, 27, '2019-08-05'),
         ('apple', 1, 122, '2019-08-12'),
         ('orange', 2, 5, '2019-08-19');

DROP TABLE IF EXISTS tbl2

CREATE TABLE tbl2 
(
    timeid INT NOT NULL,
    date DATETIME
);

INSERT INTO tbl2(timeid, date)
VALUES (5,'2019-07-29'),
        (4,'2019-08-05'),
         (3,'2019-08-12'),
         (2,'2019-08-19'),
         (1,'2019-08-26');

1 个答案:

答案 0 :(得分:2)

要获得所需的结果,您需要使用以下命令将tbl1的不同名称值与tbl2的日期交叉连接,然后再将LEFT JOIN的名称交叉命名为tbl1 COALESCENULL的{​​{1}}值替换为0:

count

输出:

SELECT t2.date, n.name, n.nameid, COALESCE(t1.count, 0) AS count, t2.timeid
FROM tbl2 t2
CROSS APPLY (SELECT DISTINCT name, nameid FROM tbl1) n
LEFT JOIN tbl1 t1 ON t1.date = t2.date AND t1.name = n.name
ORDER BY t2.date, n.name

Demo on dbfiddle