0

我正在使用 Amazon Quick Sight 从 MySQL 数据库中提取数据来为我的雇主生成可视化数据。我是一名学生,有一点 SQL 经验,但以前从未处理过这么大的数据库。

我需要从中获取的表格是 001_leads 到 024_leads。桌子的设计方式都是一样的。

这是我目前一直在做的无济于事。Quick Sight 不会返回很好的错误警报,所以我一直在尝试不同的解决方案。

SELECT 001_leads.lead_received AS "City-A Leads Q1", 002_leads.lead_received AS "City-B Leads Q1" FROM 001_leads, 002_leads WHERE lead_received BETWEEN '2017-01-01' AND '2017-04-01'

我相当肯定我需要使用连接,但我不知道如何连接 24 个不同的表。

理想情况下,当此查询运行时,它将显示来自每个城市的每个潜在客户。

谢谢你看:)

编辑:

这是来自 001_leads 的一些列名

Quick Sight 中的 SQL 示例。 我想要所有 24 个不同表的查询结果

4

2 回答 2

0

除非您真的需要数据透视表,否则我建议您使用联合并为每个潜在客户获取一行,就像@Darby 建议的那样:

SELECT *
FROM (
    SELECT 
        'CityA' as `city`,
        a.*
    FROM `001_leads` a
    WHERE a.`lead_received` BETWEEN '2017-01-01' AND '2017-04-01'
    UNION
    SELECT 
        'CityB' as `city`,
        b.*
    FROM `001_leads` b
    WHERE b.`lead_received` BETWEEN '2017-01-01' AND '2017-04-01'
    UNION
    SELECT 
        'CityC' as `city`,
        c.*
    FROM `001_leads` c
    WHERE c.`lead_received` BETWEEN '2017-01-01' AND '2017-04-01'
    UNION
    SELECT 
        'CityD' as `city`,
        d.*
    FROM `001_leads` d
    WHERE d.`lead_received` BETWEEN '2017-01-01' AND '2017-04-01'
    UNION
    SELECT 
        'CityE' as `city`,
        e.*
    FROM `001_leads` e
    WHERE e.`lead_received` BETWEEN '2017-01-01' AND '2017-04-01'
) as `all`
ORDER BY all.`lead_received`,all.`city`;

如果这是一个将在未来不同时间段内完成的查询,我建议使用联合创建一个视图,然后对该视图进行选择。

于 2017-05-23T20:27:37.727 回答
0

我认为联盟是你所追求的......不完全确定,你的问题的措辞。

例如。

SELECT 001_leads.lead_received AS Leads
FROM 001_leads
WHERE lead_received BETWEEN '2017-01-01' AND '2017-04-01'

UNION 

SELECT 002_leads.lead_received
FROM 002_leads
WHERE lead_received BETWEEN '2017-01-01' AND '2017-04-01'

更新

以下应该做你所说的你想要它的样子。

SELECT l1.lead_received AS LeadsCityA, l2.lead_received AS LeadsCityA, l3.lead_received AS LeadsCityA, l4.lead_received AS LeadsCityA
FROM 001_leads l1
LEFT JOIN 002_leads l2
    ON lead_received BETWEEN '2017-01-01' AND '2017-04-01'
LEFT JOIN 003_leads l3
    ON lead_received BETWEEN '2017-01-01' AND '2017-04-01'
LEFT JOIN 004_leads l4
    ON lead_received BETWEEN '2017-01-01' AND '2017-04-01'
WHERE lead_received BETWEEN '2017-01-01' AND '2017-04-01'
于 2017-05-23T20:11:01.177 回答