【问题标题】:How to add a column to existing fact table and how to add data to existing fact table?如何向现有事实表添加列以及如何向现有事实表添加数据?
【发布时间】:2021-06-18 22:50:40
【问题描述】:
  • 问题 1:如何在现有的事实表中添加一列而不使用 重新创建它?

  • 问题 2:如何向现有事实添加新数据 桌子?

我正在使用 PostgreSQL。这是针对商业智能实施的特定用例。我的解决方案有效,但我无法将它们组合在一起。

我的网络应用程序跟踪客户和顾问之间的会话/交互 它们存储在 fact_sessions [具有数百万条记录和 session_id 的表中 作为主键],这个表是增量加载的,这意味着只加载增量。

我想在 fact_session 表中添加一个名为“session_count”的新字段 并使其在字段中持续存在并继续填充 未来的加载过程。

新字段按特定客户的时间顺序排列。 为每个客户端的会话编号

我想创建一个实现这个新字段的流程。并确保 新数据按照现有的 fact_sessions 表按时间顺序排列。

-- Example Fact table
with fact_sessions(session_id, client_id, advisor_id, session_date) as (
    values (22045400, 2305, 10004, '2021-01-02'::date),
        (22045401, 2307, 10001, '2021-01-04'::date),
        (22045401, 2305, 10000, '2021-01-07'::date),
        (22045402, 2305, 10002, '2021-01-08'::date),
        (22045402, 2307, 10002, '2021-01-09'::date),
        (22045403, 2306, 10001, '2021-01-11'::date),
        (22045403, 2306, 10000, '2021-01-14'::date),
        (22045404, 2306, 10004, '2021-01-16'::date),
        (22045404, 2307, 10001, '2021-01-19'::date),
        (22045405, 2305, 10001, '2021-01-20'::date)
),

-- Example New Data load Each session is mutally exculsive so there is no update 
-- of record
new_sessions(session_id, client_id, advisor_id, session_date) as (
    values (22060000, 2307, 10001, '2021-01-26'::date),
        (22060001, 2305, 10002, '2021-01-28'::date),
        (22060002, 2305, 10003, '2021-01-29'::date),
        (22060003, 2307, 10003, '2021-02-01'::date),
        (22060004, 2306, 10001, '2021-02-04'::date),
        (22060005, 2308, 10001, '2021-02-04'::date)
)

-- How do I make this persistent in the fact table? How to add a column to an existing table
-- I don't want to recreate the fact table I want to add the new field to existing
-- Fact table
data_ref as (
    SELECT *,
        row_number() over(
            PARTITION BY client_id
            ORDER BY session_date
        ) AS session_count
    FROM fact_sessions
    ORDER BY client_id,
        session_date
),

-- Then i use the latest record for the clients and join it with new data 
-- and then I can push it to the existing fact table
-- I am not sure how to add this data to the existing fact table
SELECT 
    ns.session_id,
    ns.client_id,
    ns.advisor_id,
    ns.session_date,
    row_number() over(
        PARTITION BY ns.client_id
        ORDER BY ns.session_date
    ) + COALESCE(l.max_csi, 0) AS session_count 
    --, l.client_id, 
    --COALESCE(l.max_csi,0)
FROM new_sessions AS ns
    LEFT JOIN (
        SELECT client_id,
            max(session_count) AS max_csi
        FROM data_ref
        GROUP BY 1
    ) AS l 
    ON ns.client_id = l.client_id

【问题讨论】:

  • 您使用的是哪个 DBMS?
  • @AmiraBedhiafi 我正在使用 PostgreSQL。这是针对商业智能实施的特定用例。我的解决方案有效,但我无法将其组合在一起。
  • 您是否在使用任何 ETL 工具?或者您只是使用 sql 查询填充您的表?
  • @AmiraBedhiafi 我只是在寻找通用 SQL 实现/工作流。
  • 如何解释这条记录 22060001 2305 10002 2021-01-28 5 当client_id 2305 advisor_id 10002 会话数可能是1。我需要了解计算会话数背后的魔力

标签: sql postgresql etl business-intelligence


【解决方案1】:
CREATE TABLE Fact_Sessions (Session_id VARCHAR(50),Client_id INT,Expert_id INT, SessionDate DATE)
INSERT INTO Fact_Sessions (Session_id,Client_id,Expert_id,SessionDate) VALUES 
('22045400',2305,450,'4/3/2021'),
('22045499',2305,160,'5/3/2021'),
('22045563',2305,150,'6/3/2021'),
('22045603',2305,334,'7/3/2021')
GO
WITH CTE AS (
SELECT *, 
ROW_NUMBER() OVER(PARTITION BY Client_id ORDER BY SessionDate ASC) AS [Client_Session_Index]
FROM Fact_Sessions 

)
SELECT * FROM CTE
会话ID |客户编号 | Expert_id |会议日期 | Client_Session_Index :--------- | --------: | --------: | :------------ | ------------------: 22045400 | 2305 | 450 | 2021-04-03 | 1 22045499 | 2305 | 160 | 2021-05-03 | 2 22045563 | 2305 | 150 | 2021-06-03 | 3 22045603 | 2305 |第334章2021-07-03 | 4

db小提琴here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2010-12-25
    • 2022-01-22
    相关资源
    最近更新 更多