Date issues with INNER JOIN
Experiencing a date issue when using INNER JOIN with zero results appearing. My table looks like the following and with a sub-query to find the latest "entry_date", then grouped by year and month and finally joined to the table to display the latest_date, type and amount.
Original table
entry_date (Date)
| amount (Currency)
| type (String)
|
2020-01-31
| $1000
| Income
|
2020-02-04
| $2500
| Income
|
2020-02-29
| $3200
| Income
|
2020-03-10
| $2600
| Income
|
2020-03-31
| $4400
| Income
|
2020-04-10
| $2100
| Income
|
Expected output
latest_date (Date)
| amount (Currency)
| type (String)
|
2020-01-31
| $1000
| Income
|
2020-02-29
| $3200
| Income
|
2020-03-31
| $4400
| Income
|
2020-04-10
| $2100
| Income
|
...
| ...
| ...
|
Works fine in MySQL
- SELECT entry_date as latest_entry, amount, type
- from rents
- INNER JOIN (
- SELECT MAX(entry_date) AS maxdate
- FROM rents
- WHERE "type" = 'Income'
- GROUP BY YEAR(entry_date), MONTH(entry_date)
- ) x ON rents. entry_date = maxdate
However, I'm struggling to get this working with Analytics. I've tried various ideas with no luck.
Query runs with no results but the Main + Sub-query work fine individually. FYI: the sub-query looks for the latest date by year and month, this is by design, so LAST_DAY function will not work in my case.
- SELECT
- DATE_FORMAT("t1"."entry_date", '%Y-%m-%d') as latest_date,
- "t1"."amount" as amount
- FROM "rents" t1
- JOIN( SELECT date_format(max("t2"."entry_date"), '%Y-%m-%d') as maxdate
- FROM "rents" t2
- WHERE "t2"."type" = 'Income'
- GROUP BY YEAR("t2"."entry_date"),
- MONTH("t2"."entry_date")
- ) x ON DATE_FORMAT("t1"."entry_date", '%Y-%m-%d') = 'maxdate'
I've done various other options in attempts to make this work and even changed the date format to sting from the original table and still no luck. I'm missing something with Analytics.
Thanks in advance.