Search code examples
mysqlsqlamazon-quicksight

Pulling the same column from multiple tables using SQL on Amazon Quick Sight


I'm working using Amazon Quick Sight pulling data from an MySQL database to generate visual data for my employer. I'm a student and have a little SQL experience but have never had to deal with a database this large before.

The tables I need to grab from are 001_leads to 024_leads. The tables are all designed the same way.

This is what I've currently been doing to no avail. Quick Sight doesn't give back great error alerts so I've been hacking at different solutions.

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'

I'm fairly positive I need use a join but I have no clue how to join 24 different tables.

Ideally when this query runs it will show every lead from every city.

Thanks for having a look :)

EDIT:

Here is some column names from 001_leads

Example of SQL in Quick Sight. I'd like the results of this query for all 24 different tables


Solution

  • I think Union is what you're after... Not completely sure, with the wording of your question.

    Eg.

    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'
    

    UPDATE

    The following should do what you're saying you want it to look like.

    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'