【问题标题】:Combining data from two PostgreSQL tables into one将两个 PostgreSQL 表中的数据合并为一个
【发布时间】:2018-01-08 02:50:22
【问题描述】:

我想从两个不同的表中提取数据以获得以下信息 - 参见附图The data that I am after。我的目标是让存款人的数量独立于每天的活跃玩家。也就是说,如果玩家存款但没有玩,我仍然希望玩家被计算在内。

我正在使用以下代码,但结果不是我想要的 - 参见图片 The data that I am getting。每个国家/地区每行我只能得到一个 calendar_date (2017-01-01)。将两个日期的数据汇总到一个中,并且还有另一行不属于任何国家/地区的数据(在存款人和 Total_Deposit_Amount 下)。该数据应在各个国家之间分发。

--temporary table for start_date
WITH Temp_Start_Date as 
(
select CAST('2017-01-01' AS date)
),

--temporary table for end_date

Temp_End_Date as 
(
select CAST('2017-01-03' AS Date)
),

--temporary table for calendar date
Temp_Calendar_Date as 
(
    SELECT  1 as LinkDate
        ,calendar_date::date from generate_series('2017-01-01',
            CURRENT_DATE -212, '1 day'::interval) calendar_date

),

--temporary table for bet_transactions
Temp_bet_transactions AS
(
        SELECT  BT.account_id
        , P.username
        , CASE 
            WHEN P.country_code = '1' then '1' 
            WHEN P.country_code = '2' then '2'
            WHEN P.country_code = '3' then '3'
            WHEN P.country_code = '4' then '4'
            WHEN P.country_code = '5' then '5'
            WHEN P.country_code = '6' then '6'
            ELSE '7'
        END AS Country
        , 1 AS LinkDate
        , SUM(CAST(CAST(money_amount_cents AS DECIMAL (18,2))/100 AS DECIMAL (18,2))) AS Turnover
    FROM    accounts A
    JOIN    players P ON A.player_id = P.id
    JOIN    ONLY bet_transactions BT ON A.id = BT.account_id
    WHERE   BT.created_at >= (SELECT * FROM Temp_Start_Date) 
        AND BT.created_at < (SELECT * FROM Temp_End_Date)
        AND BT.status = 'accepted'
        AND amount_cents <> 0
    GROUP BY
        1, 2, 3
),

--temporary table for depositors
Temp_depositors AS
(
    SELECT  account_id
        , 1 AS LinkDate
        , SUM(CAST(CAST(money_amount_cents AS DECIMAL (18,2))/100 AS DECIMAL (18,2))) AS Total_Deposit_Amount
    FROM    deposit_transactions D
    WHERE   D.created_at >= (SELECT * FROM Temp_Start_Date) 
            AND D.created_at < (SELECT * FROM Temp_End_Date)
            AND status = 'accepted'
    GROUP BY
        1, 2
)

--get result
SELECT  TCD.calendar_date
    , BT.country
    , COUNT(DISTINCT BT.account_id) AS Active_Players
    , COUNT(DISTINCT DT.account_id) AS Depositors
    , SUM(DT.Total_Deposit_Amount) AS Total_Deposit_Amount
    , SUM(BT.Money_Bet) AS Turnover
FROM    Temp_Calendar_Date TCD
JOIN    Temp_bet_transactions BT ON TCD.LinkDate = BT.LinkDate
FULL OUTER JOIN Temp_depositors DT ON BT.account_id = DT.account_id
GROUP BY
    1,2

任何帮助将不胜感激。

谢谢 斯蒂芬

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    我做cmet的声望很低,所以我必须在这里问。

    这通常是因为Temp_Calendar_Date 仅包含一行(带有2017-01-01)并且您在此之后进行分组。

    如果你能提供一些插入来生成测试数据会容易得多。

    顺便说一句,LinkDate 是什么?为什么这总是等于1

    【讨论】:

    • Linkdate 被用作虚拟列以按日历日期对数据进行分组
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多