我正在使用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个不同的表。
理想情况下,当此查询运行时,它将显示每个城市的每个潜在客户。
感谢您一看:)
编辑:
答案 0 :(得分:0)
我认为Union就是你所追求的......不完全确定,你的问题的措辞。
例如
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'
答案 1 :(得分: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`;
如果这是将来会在不同时间段内完成的查询,我建议您使用联盟创建一个视图,然后对视图进行选择。