如何连接两个 SELECT,其中一个需要另一个 SELECT 的计算输出 - SQLite

问题描述 投票:0回答:1

--这两部分需要连接起来。任何帮助表示赞赏。 --第一个生成单列 calc_date

SELECT DATE(account_creation,'weekday 0','-6 days') as calc_date FROM "customers_tbl"
WHERE DATE(account_creation) >= DATE('now','localtime','-364 days')
GROUP BY calc_date
ORDER BY account_creation ASC
LIMIT 50000

--第二个计算单个值保留百分比作为 calc_date 的函数,该函数在第一部分中生成。
--需要两列,calc_date和retention$

SELECT 
 100 * CAST((
 SELECT
   COUNT(*) AS retainedcustcount
   FROM customers_tbl
     WHERE DATE (account_creation) BETWEEN DATE(calc_date,'-21 days') AND DATE(calc_date,'localtime','-14 days')
     AND  DATE (last_ordr_date) BETWEEN DATE(calc_date,'-20 days') AND DATE (calc_date)
     ) AS FLOAT) / (
    SELECT COUNT(*) AS newcustcount
    FROM customers_tbl
    WHERE DATE (account_creation) BETWEEN DATE(calc_date,'-21 days') AND DATE(calc_date,'-14 days')
) AS "Retention %"

--可用于测试的表格值

CREATE TABLE customers_tbl (
    cust_id INTEGER PRIMARY KEY, 
    account_creation DATE, 
    last_ordr_date DATE
);

INSERT INTO customers_tbl(cust_id,account_creation,last_ordr_date)
VALUES
("9258266601","12/29/2023","1/8/2023"),
("9739199739","12/29/2023","12/31/2023"),
("4086661710","12/19/2023","12/21/2024"),
("8059106540","12/26/2023","12/26/2023"),
("4087182471","12/27/2023","12/27/2023"),
("9257255250","12/21/2023","1/1/2024"),
("2145378403","12/31/2023","1/2/2024"),
("9258092593","12/21/2023","12/21/2023"),
("6507735098","12/21/2023","1/9/2024"),
("5104070728","12/21/2023","12/21/2023"),
("2245002039","12/22/2023","12/22/2023"),
("5105794996","12/22/2023","12/22/2023"),
("9256678370","12/22/2023","12/22/2023"),
("9254905027","12/22/2023","12/22/2023"),
("9256993084","12/29/2023","1/9/2024"),
("9517751555","12/23/2023","12/23/2023"),
("9255205775","12/23/2023","12/23/2023"),
("5104702809","12/23/2023","12/23/2023"),
("5102400977","12/23/2023","12/23/2023"),
("9257898817","12/26/2023","12/26/2023"),
("9257855978","12/26/2023","1/2/2024"),
("2096092929","12/26/2023","12/26/2023"),
("9257194811","12/26/2023","12/26/2023"),
("9256058676","12/27/2023","12/27/2023");
sql sqlite left-join
1个回答
0
投票

我更改了日期格式,使其适用于我正在运行的 sqlite3。

我添加了两个新客户,以便今天的查询实际上找到了人。

('8888888888','2023-11-30','2024-01-12'),
('8888888889','2023-11-30','2023-12-10'),

然后您可以使用WITH来命名第一个查询并在FROM部分中使用它:

WITH calcdate AS (
    SELECT DATE(account_creation,'weekday 0','-6 days') AS calc_date
    FROM customers_tbl
    WHERE DATE(account_creation) >= DATE('now','localtime','-364 days')
    GROUP BY calc_date
    ORDER BY account_creation ASC
)
SELECT 100 * CAST((
    SELECT COUNT(*) AS retainedcustcount
    FROM calcdate, customers_tbl
    WHERE DATE (account_creation) BETWEEN DATE(calc_date,'-21 days') AND DATE(calc_date,'localtime','-14 days')
     AND  DATE (last_ordr_date) BETWEEN DATE(calc_date,'-20 days') AND DATE (calc_date)
    ) AS FLOAT
) / (
    SELECT COUNT(*) AS newcustcount
    FROM customers_tbl, calcdate
    WHERE DATE (account_creation) BETWEEN DATE(calc_date,'-21 days') AND DATE(calc_date,'-14 days')
) AS "Retention %";
© www.soinside.com 2019 - 2024. All rights reserved.