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 3608070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:27:20+00:00 2026-05-18T21:27:20+00:00

I have mysql table with data like this. record will have server with total

  • 0

I have mysql table with data like this. record will have server with total cpu and virtual server with cpu assinged

type, cpu
srv1,   10
vsrv11, 2
vsrv12,  3
srv2,   15
vsrv21, 6
vsrv22,  7
vsrv23,  1

from the above data, I want to create output like this.

server, total cpu, assigned cpu, free cpu
srv1, 10, 5, 5
srv2, 15, 14, 1

Can you help me on creating sql query for this report?


I have changed my table and data like this.

CREATE TABLE `cpuallocation` (
  `servertype` varchar(10) DEFAULT NULL,
  `servername` varchar(20) DEFAULT NULL,
  `hostname` varchar(20) DEFAULT NULL,
  `cpu_count` float DEFAULT NULL,
  UNIQUE KEY `server_uniq_idx` (`servertype`,`servername`,`hostname`)

insert into cpuallocation values('srv', 'server1', '',16);
insert into cpuallocation values('vir', 'server1', 'host1',5);
insert into cpuallocation values('vir', 'server1', 'host2',2.5);
insert into cpuallocation values('vir', 'server1', 'host3',4.5);
insert into cpuallocation values('srv', 'server2', '',8);
insert into cpuallocation values('vir', 'server2', 'host1',5);
insert into cpuallocation values('vir', 'server2', 'host2',2.5);
insert into cpuallocation values('srv', 'server3', '',24);
insert into cpuallocation values('vir', 'server3', 'host1',12);
insert into cpuallocation values('vir', 'server3', 'host2',2);
insert into cpuallocation values('srv', 'server4', '',12);

Update:

I created two view, now I getting the result I want.

create view v1 as 
select servername, sum(cpu_count) as cpu_allocated 
from cpuallocation where servertype='vir' group by servername;

create view v2 as 
select servername, cpu_count as total_cpu 
from cpuallocation where servertype='srv';

select a.servername, a.total_cpu, b.cpu_allocated 
from v2 as a left join v1 as b on a.servername=b.servername;

+------------+-----------+---------------+
| servername | total_cpu | cpu_allocated |
+------------+-----------+---------------+
| server1    |        16 |            12 |
| server2    |         8 |           7.5 |
| server3    |        24 |            14 |
| server4    |        12 |          NULL |
+------------+-----------+---------------+
4 rows in set (0.00 sec)

Is it possible to create a query with-out creating views?

  • 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-05-18T21:27:21+00:00Added an answer on May 18, 2026 at 9:27 pm

    This should be usable for the original table format

    select 
      a.type as server,
      a.cpu as total_cpu,
      sum(b.cpu) as assigned_cpu,
      a.cpu-sum(b.cpu) as free_cpu
    from cpu as a
    left join
      cpu as b on locate(concat('v',a.type), b.type)<>0
    where a.type in('srv1', 'srv2')
    group by a.type;
    

    Your table does not seems have a common natural key, you should assign one

    This should useful for the updated schema:

    select
      cpu1.servername as server,
        cpu1.cpu_count as total_cpu,
        ifnull(sum(cpu2.cpu_count), 0) as assigned_cpu,
        ifnull(cpu1.cpu_count-sum(cpu2.cpu_count),0) as free_cpu
    from 
      cpuallocation as cpu1
    left join
      cpuallocation as cpu2
      on cpu1.servername=cpu2.servername and cpu2.servertype='vir'
    where 
      cpu1.hostname = '' 
    group by cpu1.servername;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.