Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:52:02+00:00 2026-06-11T17:52:02+00:00

I would like to combine different results from the same table as one big

  • 0

I would like to combine different results from the same table as one big result.

SELECT host_name,stats_avgcpu,stats_avgmem,stats_avgswap,stats_avgiowait 
    FROM sar_stats,sar_hosts,sar_appgroups,sar_environments
    WHERE stats_host = host_id
    AND host_environment = env_id
    AND env_name = 'Staging 2'
    AND host_appgroup = group_id
    AND group_name = 'Pervasive'
    AND DATE(stats_report_time) =  DATE_SUB(curdate(), INTERVAL 1 DAY)

SELECT AVG(stats_avgcpu),AVG(stats_avgmem),AVG(stats_avgswap),AVG(stats_avgiowait)
 FROM sar_stats 
 WHERE  stats_id = "stat_id of the first query" and DATE(stats_report_time) 
   BETWEEN DATE_SUB(curdate(), INTERVAL 8 DAY) and DATE_SUB(curdate(), INTERVAL 1 DAY)

SELECT AVG(stats_avgcpu),AVG(stats_avgmem),AVG(stats_avgswap),AVG(stats_avgiowait)
 FROM sar_stats 
 WHERE  stats_id = "stat_id of the first query" and DATE(stats_report_time) 
   BETWEEN DATE_SUB(curdate(), INTERVAL 31 DAY) and DATE_SUB(curdate(), INTERVAL 1 DAY)

Desired output would be something like …

host_name|stats_avgcpu|stats_avgmem|stats_avgswap|stats_avgiowait|7daycpuavg|7daymemavg|7dayswapavg|7dayiowaitavg|30daycpuavg|30daymemavg|....etc

SQL Fiddle
http://sqlfiddle.com/#!8/4930b/3

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:52:03+00:00Added an answer on June 11, 2026 at 5:52 pm

    It seems like this is what you want. I updated the first query to use proper ANSI JOIN syntax and then for the additional two queries they were joined via a LEFT JOIN on the stats_host field:

    SELECT s.stats_host,
      h.host_name,
      s.stats_avgcpu,
      s.stats_avgmem,
      s.stats_avgswap,
      s.stats_avgiowait,
      s7.7dayavgcpu,
      s7.7dayavgmem,
      s7.7dayavgswap,
      s7.7dayavgiowait,
      s30.30dayavgcpu,
      s30.30dayavgmem,
      s30.30dayavgswap,
      s30.30dayavgiowait
    FROM sar_stats s
    INNER JOIN sar_hosts h
      on s.stats_host = h.host_id
    INNER JOIN sar_appgroups a
      on h.host_appgroup = a.group_id
      and a.group_name = 'Pervasive'
    INNER JOIN sar_environments e
      on h.host_environment = e.env_id
      and e.env_name = 'Staging 2'
    LEFT JOIN
    (
      SELECT s.stats_host,
        AVG(s.stats_avgcpu) AS '7dayavgcpu',
        AVG(s.stats_avgmem) AS '7dayavgmem',
        AVG(s.stats_avgswap) AS '7dayavgswap',
        AVG(s.stats_avgiowait) AS '7dayavgiowait'
      FROM sar_stats s
      WHERE DATE(stats_report_time) BETWEEN DATE_SUB(curdate(), INTERVAL 8 DAY) AND DATE_SUB(curdate(), INTERVAL 1 DAY)
      GROUP BY s.stats_host
    ) s7
      on s.stats_host = s7.stats_host
    LEFT JOIN
    (
      SELECT s.stats_host,
        AVG(s.stats_avgcpu) AS '30dayavgcpu',
        AVG(s.stats_avgmem) AS '30dayavgmem',
        AVG(s.stats_avgswap) AS '30dayavgswap',
        AVG(s.stats_avgiowait) AS '30dayavgiowait'
      FROM sar_stats s
      WHERE DATE(s.stats_report_time) BETWEEN DATE_SUB(curdate(), INTERVAL 31 DAY) AND DATE_SUB(curdate(), INTERVAL 1 DAY)
      GROUP BY s.stats_host
    ) s30
      on s.stats_host = s30.stats_host
    WHERE DATE(s.stats_report_time) =  DATE_SUB(curdate(), INTERVAL 1 DAY);
    

    see SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to combine these four queries into one: SELECT COUNT(ID) FROM conversations
I would like to combine the DisplayNames from two different ViewModels, but only IF
I have these 2 queries that I would like to combine into one big
I would like to combine two __m128 values to one __m256 . Something like
I have two views that I would like to combine into one. The first
While thinking about how to best combine search results from different resources on a
I would like to combine to linq queries into the same gridview. Though I
Let's suppose I have three different select statements, like this SELECT 'Final', ID FROM
I have multiple select statements from different tables on the same database. I was
I'd like to use convert (or whatever) from Imagemagick to combine two different sized

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.