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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:08:29+00:00 2026-06-03T15:08:29+00:00

Please click here for sample tables and description . I have two tables ‘VEHICLE’

  • 0

Please click here for sample tables and description.

I have two tables ‘VEHICLE’ and ‘VEHICLE_CLASS’ as per attached images.

I need a query to get the result rows with how many VEHICLES each VEHICLE_CLASS has grouped by status as shown in attached image.

The query should be generic so that it can be used in any database (MySQL/Oracle/MSSQL/DB2)

Please help.

CREATE TABLE VEHICLE 
(
    VEHICLE_ID varchar(20), 
    VEHICLE_CLASS_ID varchar(30),
    STATUS int
);

CREATE TABLE VEHICLE_CLASS
(
    VEHICLE_CLASS_ID varchar(30),
    VEHICLE_CLASS_NAME varchar(30)
);

INSERT INTO VEHICLE
   (VEHICLE_ID, VEHICLE_CLASS_ID, STATUS)
VALUES
   ('vehicle_001', 'vehicle_class_001', 0),
   ('vehicle_002', 'vehicle_class_002', 1),
   ('vehicle_003', 'vehicle_class_003', 2),
   ('vehicle_004', 'vehicle_class_001', 0),
   ('vehicle_005', 'vehicle_class_002', 2),
   ('vehicle_006', 'vehicle_class_001', 0),
   ('vehicle_007', NULL, 1);


INSERT INTO VEHICLE_CLASS
   (VEHICLE_CLASS_ID, VEHICLE_CLASS_NAME)
VALUES
   ('vehicle_class_001', 'ABC'),
   ('vehicle_class_002', 'BCD'),
   ('vehicle_class_003', 'EFG'); 

Here is the query that I tried but didn’t able to get desired result:

SELECT veh.VEHICLE_CLASS_ID, vehclass.VEHICLE_CLASS_NAME, 
    SUM( CASE WHEN veh.STATUS=2 THEN COUNT(veh.VEHICLE_ID) end) Completed,
    SUM( CASE WHEN veh.STATUS!=2 THEN COUNT(veh.VEHICLE_ID) end) not_completed
FROM VEHICLE veh LEFT JOIN VEHICLE_CLASS vehclass on veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID
    GROUP BY veh.VEHICLE_CLASS_ID having veh.VEHICLE_CLASS_ID is not null;
  • 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-03T15:08:32+00:00Added an answer on June 3, 2026 at 3:08 pm

    You where pretty close. Instead of SUM( CASE WHEN veh.STATUS=2 THEN COUNT(veh.VEHICLE_ID) its, then 1 and also you need to group by VEHICLE_CLASS_NAME (except in MySQL but you wanted it to work in the others).

    SELECT veh.VEHICLE_CLASS_ID, 
           vehclass.VEHICLE_CLASS_NAME, 
           Sum(CASE 
                 WHEN veh.STATUS = 2 THEN 1 
               END) Completed, 
           Sum(CASE 
                 WHEN veh.STATUS <> 2 THEN 1 
               END) not_completed 
    FROM   VEHICLE veh 
           LEFT JOIN VEHICLE_CLASS vehclass 
             ON veh.VEHICLE_CLASS_ID = vehclass.VEHICLE_CLASS_ID 
    GROUP  BY veh.VEHICLE_CLASS_ID, 
              vehclass.VEHICLE_CLASS_NAME 
    HAVING veh.VEHICLE_CLASS_ID IS NOT NULL; 
    

    DEMO SQL Server

    DEMO MySQL

    DEMO Oracle

    Notes

    • As Lamak pointed out <> is typically used but it seems != to be well supported but you’ll need to test
    • I didn’t add Else 0 Because the image included a blank space in the SUM_not_completed. You may choose to do this or add a COALACE if you do want zeros
    • If you know you’re in MySQL you can remove the CASE and do Sum(veh.STATUS = 2) Completed
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please click here for sample tables and description. I have three tables ‘PROJECTS’, 'PROJECTMANAGER'
Please click here for sample tables and description. I have three tables ‘PROJECTS’, 'PROJECTMANAGER'
http://www.leandrovieira.com/projects/jquery/lightbox/ sample so i have get images in while loop here is my code
In my view i have ImageView.If i click ,it should open nextview. Please help
Please see the jsfiddle sample output: offset based on svg x:12 y:34 mouse click
Please visit this link Click on first Image. Now is showing the Modal page
please how will i position the(Click to Register now and place your orders) to
Please check out: http://gherkin.co.nz/refresh/ and click on the purple box that says 'Membership chest'
Please check the codes.. $(.editable).live(click,function(){ CurrentOBJhtml = $(this).text(); nextHtml = <input type='text' class='hoverable' value='+CurrentOBJhtml+'
light box closing automatically with in seconds. Could you help me please $(document).ready(function(){ $('.lightbox').click(function(){

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.