微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何向现有事实表添加列以及如何向现有事实表添加数据?

如何解决如何向现有事实表添加列以及如何向现有事实表添加数据?

  • 问题 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),10000,'2021-01-07'::date),(22045402,10002,'2021-01-08'::date),'2021-01-09'::date),(22045403,2306,'2021-01-11'::date),'2021-01-14'::date),(22045404,'2021-01-16'::date),'2021-01-19'::date),(22045405,'2021-01-20'::date)
),-- Example New Data load Each session is mutally exculsive so there is no update 
-- of record
new_sessions(session_id,session_date) as (
    values (22060000,'2021-01-26'::date),(22060001,'2021-01-28'::date),(22060002,10003,'2021-01-29'::date),(22060003,'2021-02-01'::date),(22060004,'2021-02-04'::date),(22060005,2308,'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

解决方法

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',160,'5/3/2021'),('22045563',150,'6/3/2021'),('22045603',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
Session_id | Client_id | Expert_id | SessionDate | 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

dbfiddle here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。