简体   繁体   中英

(Presto) SQL cross join two tables but only for a specific right column

Let's say I have two tables: "calendar" and "sales", where the calendar table contains a date for every row (in yyyy-mm-dd format) and the sales table contains 3 columns, one with date, one with store name, one with quantity; like this:

calendar:

   date    
==========
2021-03-01
2021-03-02
2021-03-03
...


sales:

   date         store     quantity
==================================
2021-03-01        A           10
2021-03-01        B            6
2021-03-02        C           15
2021-03-04        A            8
...

As you can see from the 2nd table, if a store has 0 sales on a specific day, that row isn't present at all. What I'm trying to achieve, is a joint table that fills the gaps, eg:

sales:

   date         store     quantity
==================================
2021-03-01        A           10
2021-03-01        B            6
2021-03-01        C            0
2021-03-02        A            0
2021-03-02        B            0
2021-03-02        C           15
2021-03-03        A            0
2021-03-03        B            0
2021-03-03        C            0
...

The way I managed to do it, was like this:

SELECT c.date,
       s.store,
       t.sales
  FROM calendar c
  
       CROSS JOIN (SELECT DISTINCT store FROM sales) s
         
       LEFT JOIN sales t
       ON c.date = t.date
       AND s.store = t.store

However, this query performs a double read on table "sales", which I would like to avoid, since the data being scanned is relatively large.

Is there any way I can achieve this same result by performing just a single read on table "sales"?

The solution to your problem is to have a separate table called stores . Then use this table for the query:

SELECT c.date, s.store, t.sales
FROM calendar c CROSS JOIN
     stores s LEFT JOIN 
     sales t
     ON c.date = t.date AND s.store = t.store;

With a single table this is tricky. I can think of some optimizations, but none that would eliminate the scan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM