Note that there are some explanatory texts on larger screens.

plurals
  1. POMinimum and maximum values from different mysql tables
    primarykey
    data
    text
    <p>I have two tables: TableA and TableB. Both have "dates" and "rates" fields. I want to have minimum rates of TableA and their dates; and maximum rates of TableB and their dates. Also, I like to list them for each month and year. </p> <p>I use the query below to get minimum and maximum rates from <strong>one table</strong>. But I could not figure out how to get minimum rates from TableA maximum rates from TableB.</p> <pre><code>SELECT MIN(rate) AS minRate, (SELECT date FROM TableA WHERE rate = min(t2.rate) and month(date) = month(t2.date) and year(date) = year(t2.date) limit 1 ) as minDate, MONTHNAME(date) as MN, YEAR(date) as YN, MAX(rate) AS maxRate, (SELECT date FROM TableAs WHERE rate = max(t2.rate) and month(date) = month(t2.date) and year(date) = year(t2.date) limit 1) as maxDate FROM TableA t2 GROUP BY YEAR(date) , MONTH(date)"; </code></pre> <p><strong>EDIT 1:</strong> I ended up with this.</p> <pre><code>SELECT a.MinYear AS Year, a.MinMonth AS Month, a.MinRate, b.MaxRate, a.MinDate, b.MaxDate FROM (SELECT YEAR(date) AS MinYear, MONTH(date) AS MinMonth, MIN(rate) AS MinRate, (SELECT date FROM $TableA WHERE rate = MIN(t2.rate) AND YEAR(date) = YEAR(t2.date) AND MONTH(date) = MONTH(t2.date) limit 1) AS MinDate FROM $TableA t2 GROUP BY MinYear, MinMonth ) AS a JOIN (SELECT YEAR(date) AS MaxYear, MONTH(date) AS MaxMonth, MAX(rate) AS MaxRate, (SELECT date FROM $TableB WHERE rate = MAX(t3.rate) AND YEAR(date) = YEAR(t3.date) AND MONTH(date) = MONTH(t3.date) limit 1) AS MaxDate FROM $TableB t3 GROUP BY MaxYear, MaxMonth ) AS b ON a.MinYear = b.MaxYear AND a.MinMonth = b.MaxMonth ORDER BY Year, Month </code></pre> <p><strong>EDIT 2</strong> Jonathan Leffler's query (with minor changes after testing) performs better:</p> <pre><code>SELECT a.MinYear AS Year, a.MinMonth AS Month, a.MinDate, a.MinRate, b.MaxDate, b.MaxRate FROM (SELECT n.MinYear, n.MinMonth, a.Date AS MinDate, n.MinRate FROM $TableA AS a JOIN (SELECT YEAR(date) AS MinYear, MONTH(date) AS MinMonth, MIN(rate) AS MinRate FROM $TableA GROUP BY MinYear, MinMonth ) AS n ON a.Rate = n.MinRate AND YEAR(a.Date) = n.MinYear AND MONTH(a.Date) = n.MinMonth ) AS a JOIN (SELECT x.MaxYear, x.MaxMonth, b.Date AS MaxDate, x.MaxRate FROM $TableB AS b JOIN (SELECT YEAR(date) AS MaxYear, MONTH(date) AS MaxMonth, MAX(rate) AS MaxRate FROM $TableB GROUP BY MaxYear, MaxMonth ) AS x ON b.Rate = x.MaxRate AND YEAR(b.Date) = x.MaxYear AND MONTH(b.Date) = x.MaxMonth ) AS b ON a.MinYear = b.MaxYear AND a.MinMonth = b.MaxMonth ORDER BY Year, Month"; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload