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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:05:55+00:00 2026-06-12T21:05:55+00:00

Please review my tables below… Is it possible to build a single query capable

  • 0

Please review my tables below… Is it possible to build a single query capable of

1) calculating the SUM of total_time for all vehicles that have class_id 1 (regardless of feature_id)
(result would be 6:35)
2) calculating the SUM of total_time for all vehicles that have class_id 1 AND have feature_id 2
(result would be 5:35 based on vehicle_id 22 and 24)

I’m able to get the results in two seperate queries, but I was hoping to retrieve them in one single query…. something like:

SELECT 
    SUM((CASE WHEN (VEHICLE_TABLE.class_id = 1) then LOG_TABLE.total_time else 0 end)) **AS TOTAL_ALL**,
    ...here goes statement for 2)... AS TOTAL_DIESEL...
FROM LOG_TABLE, VEHICLE_TABLE .....
WHERE VEHICLE_TABLE.vehicle_id = LOG_TABLE.vehicle_id ......

TABLE 1: LOG_TABLE (vehicle_id is NOT unique)
vehicle_id  |  total_time
————–|————–
      22               2:00
      22               0:30
      23               1:00
      24               2:20
      24               0:45

TABLE 2: VEHICLE_TABLE (vehicle_id is unique)
vehicle_id  |  class_id
————–|————–
      22                1
      23                3
      24                1

TABLE 3: VEHICLE_FEATURES_TABLE (vehicle_id is NOT unique but feature_id is unique per vehicle_id)
vehicle_id  |  feature_id
————–|————–
      22                1
      22                2
      23                1
      23                2
      23                6
      24                2
      24                6

  • 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-12T21:05:56+00:00Added an answer on June 12, 2026 at 9:05 pm
    SELECT  SUM(lt.total_time) AS TOTAL_ALL,
            SUM(CASE WHEN (vft.feature_id IS NOT NULL) then LOG_TABLE.total_time else 0 end) AS FEATURE_TOTAL
    
    FROM    VEHICLE_TABLE vt
    
            JOIN LOG_TABLE lt
            ON vt.vehicle_id = lt.vehicle_id
    
            LEFT JOIN VEHICLE_FEATURES_TABLE vft
            ON vt.vehicle_id = vft.vehicle_id AND vft.feature_id = 2
    
    WHERE   vt.class_id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please review my sample queries (given below), and let me know which query would
Please i need help, this code below works fine on my localhost, php5.3+ but
Please look the following query: SELECT ID, START, END FROM TABLEA the result is:
Please help me with this update query. I COMPLETELY understand how redundant this is
Below is my query which is in Storedprocedure(SQL Server 2008) will return list of
I am arranging a dynamic table of textboxes using a loop. Please review the
Please review my code and give me your advise on this: XML file: content.xml
I have 2 tables as below and want to have select both of them
I have 2 tables: user_tb.username user_tb.point review_tb.username review_tb.review I am coding with PHP(CodeIgniter). So
For a possible solution using Active Directory and Exchange see my post below. We

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.