我有这个事实表:
CREATE TABLE [dbo].[FACT_TABLE]
(
[A_id] [int] NOT NULL,
[B_id] [int] NOT NULL,
[C_id] [int] NOT NULL,
[D_id] [int] NOT NULL,
[FACT_total] [float] NOT NULL,
[FACT_average] [float] NOT NULL,
CONSTRAINT [PK_FACT_TABLE]
PRIMARY KEY CLUSTERED ([A_id] ASC, [B_id] ASC,
[C_id] ASC, [D_id] ASC)
) ON [PRIMARY]
我有一个T-SQL脚本将数据加载到我的事实表中:
INSERT INTO [dbo].[FACT_TABLE] ([A_id], [B_id], [C_id], [company_id], [D_id],[FACT_total], [FACT_average])
SELECT
b.A_id, c.B_id, d.C_id, e.D_id,
AVG(FACT_total) AS FACT_total,
AVG(FACT_average) AS FACT_average
FROM
[dbo].[staging_Area] a
INNER JOIN
[dbo].[DIM_A] b ON a.A_id = b.A_id
INNER JOIN
[dbo].[DIM_B] c ON a.[B_id] = c.B_id
INNER JOIN
[dbo].[DIM_C] d ON a.[C_id] = d.C_id
INNER JOIN
[dbo].[DIM_D] e ON a.D_id = e.D_id
GROUP BY
b.A_id, c.B_id, d.C_id, e.D_id
如何保证我不会在事实表中插入重复值以避免引用参照完整性错误?
我在该查询之后尝试使用以下语句:
EXCEPT
SELECT *
FROM [dbo].[FACT_TABLE]
但我认为这不是最佳方法......
任何人都有另一种选择吗?对不起,但我不能把真正的列和表名称......
谢谢!
答案 0 :(得分:1)
csvListNew.clear()
到事实表的 LEFT OUTER JOIN
以确保您没有插入已存在的记录:
WHERE FT.Column IS NULL
答案 1 :(得分:0)
INSERT INTO [dbo]。[FACT_TABLE]
([A_id],[B_id],[C_id],[D_id],[FACT_total],[FACT_average])
选择a.A_id,a.B_id,a.C_id,a.D_id,a.FACT_total,a.FACT_average
来自
( 选择
b.A_id, c.B_id, d.C_id, e.D_id,
AVG(FACT_total) AS FACT_total,
AVG(FACT_average) AS FACT_average
FROM
[dbo].[staging_Area] a
INNER JOIN
[dbo].[DIM_A] b ON a.A_id = b.A_id
INNER JOIN
[dbo].[DIM_B] c ON a.[B_id] = c.B_id
INNER JOIN [dbo]。[DIM_C] d ON a。[C_id] = d.C_id
INNER JOIN
[dbo].[DIM_D] e ON a.D_id = e.D_id
GROUP BY
b.A_id, c.B_id, d.C_id, e.D_id
)a
其中
NOT EXISTS
( SELECT 1 FROM dbao.FACT_TABLE B
WHERE
B.A_id = A.A_id AND B.B_id = A.B_id和b.C_id = a.C_id和b.D_id = a.D_id
)