SQL - 如何交叉连接两个 table 以重复值
SQL - How to cross-join two table to repeat values
我有 2 个 table 看起来像这样:
MonthEndDate
2016-06-30 00:00:00.000
2016-07-31 00:00:00.000
2016-08-31 00:00:00.000
2016-09-30 00:00:00.000
2016-10-31 00:00:00.000
2016-11-30 00:00:00.000
2016-12-31 00:00:00.000
和
MonthEndDate CustomerId Flag
2016-06-30 00:00:00.000 123 1
2016-07-31 00:00:00.000 123 1
2016-08-31 00:00:00.000 123 1
2016-09-30 00:00:00.000 123 1
我想要这样的输出:
MonthEndDate CustomerId Flag
2016-06-30 00:00:00.000 123 1
2016-07-31 00:00:00.000 123 1
2016-08-31 00:00:00.000 123 1
2016-09-30 00:00:00.000 123 1
2016-10-31 00:00:00.000 123 0
2016-11-30 00:00:00.000 123 0
2016-12-31 00:00:00.000 123 0
Table 1 是具有月末日期的 DimDate
table。
Table
2 是 CustomerInfo
table.
每当该客户对给定的月末有价值时,每个客户都会将 Flag
设置为 1
。
我想获得每个月结束日期的输出(这就是我起诉 DimDate
table 的原因),当客户没有月末值时,我希望标志显示0.
我正在使用 SQL Server 2005
这是我使用的一些示例代码:
DECLARE @table1 TABLE
(
MonthEndDate DATETIME
)
INSERT INTO @table1
VALUES('2016-06-30 00:00:00.000')
INSERT INTO @table1
VALUES('2016-07-31 00:00:00.000')
INSERT INTO @table1
VALUES('2016-08-31 00:00:00.000')
INSERT INTO @table1
VALUES('2016-09-30 00:00:00.000')
INSERT INTO @table1
VALUES('2016-10-31 00:00:00.000')
INSERT INTO @table1
VALUES('2016-11-30 00:00:00.000')
INSERT INTO @table1
VALUES('2016-12-31 00:00:00.000')
DECLARE @table2 TABLE
(
MonthEndDate DATETIME
,CustomerId INT
,Flag INT
)
INSERT INTO @table2
VALUES('2016-06-30 00:00:00.000',123,1)
INSERT INTO @table2
VALUES('2016-07-31 00:00:00.000',123,1)
INSERT INTO @table2
VALUES('2016-08-31 00:00:00.000',123,1)
INSERT INTO @table2
VALUES('2016-09-30 00:00:00.000',123,1)
SELECt * FROM @table1
SELECt * FROM @table2
我想你只是想要一个 left join
:
select t1.*, coalesce(t2.flag, 0) as flag
from @table1 t1 left join
@table2 t2
on t1.MonthEndDate = t2.MonthEndDate;
您需要执行 CROSS JOIN
以获得 MonthEndDate
和 CustomerId
的所有组合。当你拥有它时,在 table2
上执行 LEFT JOIN
以获得 Flag
:
SELECT
t1.MonthEndDate,
c.CustomerId,
Flag = ISNULL(t2.Flag, 0)
FROM @table1 t1
CROSS JOIN (SELECT DISTINCT CustomerId FROM @table2) c
LEFT JOIN @table2 t2
ON t1.MonthEndDate = t2.MonthEndDate
AND c.CustomerId = t2.CustomerId
我有 2 个 table 看起来像这样:
MonthEndDate
2016-06-30 00:00:00.000
2016-07-31 00:00:00.000
2016-08-31 00:00:00.000
2016-09-30 00:00:00.000
2016-10-31 00:00:00.000
2016-11-30 00:00:00.000
2016-12-31 00:00:00.000
和
MonthEndDate CustomerId Flag
2016-06-30 00:00:00.000 123 1
2016-07-31 00:00:00.000 123 1
2016-08-31 00:00:00.000 123 1
2016-09-30 00:00:00.000 123 1
我想要这样的输出:
MonthEndDate CustomerId Flag
2016-06-30 00:00:00.000 123 1
2016-07-31 00:00:00.000 123 1
2016-08-31 00:00:00.000 123 1
2016-09-30 00:00:00.000 123 1
2016-10-31 00:00:00.000 123 0
2016-11-30 00:00:00.000 123 0
2016-12-31 00:00:00.000 123 0
Table 1 是具有月末日期的 DimDate
table。
Table
2 是 CustomerInfo
table.
每当该客户对给定的月末有价值时,每个客户都会将 Flag
设置为 1
。
我想获得每个月结束日期的输出(这就是我起诉 DimDate
table 的原因),当客户没有月末值时,我希望标志显示0.
我正在使用 SQL Server 2005
这是我使用的一些示例代码:
DECLARE @table1 TABLE
(
MonthEndDate DATETIME
)
INSERT INTO @table1
VALUES('2016-06-30 00:00:00.000')
INSERT INTO @table1
VALUES('2016-07-31 00:00:00.000')
INSERT INTO @table1
VALUES('2016-08-31 00:00:00.000')
INSERT INTO @table1
VALUES('2016-09-30 00:00:00.000')
INSERT INTO @table1
VALUES('2016-10-31 00:00:00.000')
INSERT INTO @table1
VALUES('2016-11-30 00:00:00.000')
INSERT INTO @table1
VALUES('2016-12-31 00:00:00.000')
DECLARE @table2 TABLE
(
MonthEndDate DATETIME
,CustomerId INT
,Flag INT
)
INSERT INTO @table2
VALUES('2016-06-30 00:00:00.000',123,1)
INSERT INTO @table2
VALUES('2016-07-31 00:00:00.000',123,1)
INSERT INTO @table2
VALUES('2016-08-31 00:00:00.000',123,1)
INSERT INTO @table2
VALUES('2016-09-30 00:00:00.000',123,1)
SELECt * FROM @table1
SELECt * FROM @table2
我想你只是想要一个 left join
:
select t1.*, coalesce(t2.flag, 0) as flag
from @table1 t1 left join
@table2 t2
on t1.MonthEndDate = t2.MonthEndDate;
您需要执行 CROSS JOIN
以获得 MonthEndDate
和 CustomerId
的所有组合。当你拥有它时,在 table2
上执行 LEFT JOIN
以获得 Flag
:
SELECT
t1.MonthEndDate,
c.CustomerId,
Flag = ISNULL(t2.Flag, 0)
FROM @table1 t1
CROSS JOIN (SELECT DISTINCT CustomerId FROM @table2) c
LEFT JOIN @table2 t2
ON t1.MonthEndDate = t2.MonthEndDate
AND c.CustomerId = t2.CustomerId