Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Query - replace hardcoded values... (MySQL)
    primarykey
    data
    text
    <p>We use a monitoring solution (zabbix). It stores trends data (MySQL 5.X) using a table called trends_uint with the following structure (simplified):</p> <pre><code>itemid clock value_avg 1 1238774400 100 1 1250773900 70 1 1250773200 50 </code></pre> <p>The clock field stores datetime values using Unix time.</p> <p>I'm trying to extract a report to see the consumption of a monitored item. I thought doing this by selecting the oldest record and the newest record. I achieved it doing the following:</p> <pre><code>SELECT (SELECT t.clock FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MIN(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS minClock, (SELECT t.value_avg FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MIN(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS minValueAvg, (SELECT t.clock FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MAX(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS maxClock, (SELECT t.value_avg FROM trends_uint t WHERE t.itemid = 1 AND t.clock IN (SELECT MAX(t.clock) FROM trends_uint t WHERE t.itemid = 1)) AS maxValueAvg, (SELECT maxClock - minClock) AS timeGap, (SELECT minValueAvg - maxValueAvg) AS valueGap, (SELECT timeGap/86400) AS daysGap, (SELECT valueGap/daysGap/1024/1024) AS consumeMB </code></pre> <p>My problem is that I have lot's servers from which I want to calculate the consumption. I don't want to change the query for which one (changing the itemid).</p> <p>The system has another table (items) from which I could extract the items that I need to put into the report. Something like <code>SELECT itemid FROM items WHERE ...</code></p> <p>So this is my question: instead of using a hardcoded itemid, is it possible to make it "dynamic", for instance using the returned codes from another query (ex.: <code>SELECT ... FROM ... WHERE itemid IN (SELECT itemid FROM items WHERE ...</code>)?</p> <p>TIA,</p> <p>Bob</p>
    singulars
    1. This table or related slice is empty.
    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.
    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