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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:10:57+00:00 2026-06-08T23:10:57+00:00

I am having an issue with a query which returns results that are very

  • 0

I am having an issue with a query which returns results that are very far from reality (not only does it not make sense at all but I can also calculate the correct answer using filters).

I am building a KPI db for work and this query returns KPIs by employee by period. I have a very similar query from which this one is derived which returns KPIs by sector by period which gives the exact results I have calculated using a spreadsheet. I really have no idea what happens here. Basically, I want to sum a few measures that are in the maintenances table like temps_requete_min, temps_analyse_min, temps_maj_min and temps_rap_min and then create a subtotal AND present these measures as hours (measures are presented in minutes, thus the divide by 60).

SELECT 
    [anal].[prenom] & " " & [anal].[nom] AS Analyste, 
    maint.periode, maint.annee, 
    Round(Sum(maint.temps_requete_min)/60,2) AS REQ, 
    Round(Sum(maint.temps_analyse_min)/60,2) AS ANA, 
    Round(Sum(maint.temps_maj_min)/60,2) AS MAJ, 
    Round(Sum(maint.temps_rap_min)/60,2) AS RAP,       
    Round((Sum(maint.temps_requete_min)+Sum(maint.temps_analyse_min)+Sum(maint.temps_maj_min)+Sum(maint.temps_rap_min))/60,2) AS STOTAL, 
    Count(maint.periode) AS Nombre, 
    a.description

FROM 
    rapports AS rap, 
    analyste AS anal, 
    maintenances AS maint, 
    per_annuelle, 
    annees AS a

WHERE 
    (((rap.id_anal_maint)=anal.id_analyste) And
    ((maint.id_fichier)=rap.id_rapport) And 
    ((maint.maint_effectuee)=True) And 
    ((maint.annee)=per_annuelle.annee) And
    ((per_annuelle.annee)=a.annees))

GROUP BY 
    [anal].[prenom] & " " & [anal].[nom], 
    maint.periode, 
    maint.annee, 
    a.description, 
    anal.id_analyste

ORDER BY 
    maint.annee, maint.periode;

All measures are many orders of magnitude higher than what they should be. I suspect that my Count() is wrong, but I can’t see what would be wrong with the sums 😐

Edit: Finally I have come up with this query which shows the same measures I have calculated using Excel from the advice given in the comments and the answer provided. Many thanks to everyone. What I would like to know however, is why it makes a difference to use explicit joins rather than implicit joins (WHERE clause on PKs).

SELECT
    maintenances.periode,
    [analyste].[prenom] & " " & analyste.nom,
    Round(Sum(maintenances.temps_requete_min)/60,2) AS REQ,
    Round(Sum(maintenances.temps_analyse_min)/60,2) AS ANA, 
    Round(Sum(maintenances.temps_maj_min)/60,2) AS MAJ, 
    Round(Sum(maintenances.temps_rap_min)/60,2) AS RAP,
    Round((Sum(maintenances.temps_requete_min)+Sum(maintenances.temps_analyse_min)+Sum(maintenances.temps_maj_min)+Sum(maintenances.temps_rap_min))/60,2) AS STOTAL, 
    Count(maintenances.periode) AS Nombre

FROM 
    (maintenances INNER JOIN rapports ON maintenances.id_fichier = rapports.id_rapport)
    INNER JOIN analyste ON rapports.id_anal_maint = analyste.id_analyste

GROUP BY analyste.prenom, maintenances.periode
  • 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-08T23:10:59+00:00Added an answer on June 8, 2026 at 11:10 pm

    In this case, the problem is typically that your joins are bringing together multiple dimensions. You end up doing a cross product across two or more categories.

    The fix is to do the summaries independently along each dimension. That means that the “from” clause contains subqueries with group bys, and these are then joined together. The group by would disappear from the outer query.

    This would suggest having a subquery such as:

    from (select maint.periode, maint.annee,
                 Round(Sum(maint.temps_requete_min)/60,2) AS REQ, 
                 Round(Sum(maint.temps_analyse_min)/60,2) AS ANA, 
                 Round(Sum(maint.temps_maj_min)/60,2) AS MAJ, 
                 Round(Sum(maint.temps_rap_min)/60,2) AS RAP,       
                 Round((Sum(maint.temps_requete_min)+Sum(maint.temps_analyse_min) +Sum(maint.temps_maj_min)+Sum(maint.temps_rap_min))/60,2) AS STOTAL, 
                 Count(maint.periode) AS Nombre, 
          from maintenances maint
          group by maint.periode, maint.annee
         ) m
    

    I say “such as” because without a layout of the tables, it is difficult to see exactly where the problem is and what the exact solution is.

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

Sidebar

Related Questions

Hi guys I am having issue I have this query: SELECT * FROM useraccount
Hi I am having an issue with a MySQL query not passing a string
I'm having an issue extracting a substring in SQL query results. Here's the situation:
I'm having a bit of an issue with a query and scaling that query
I am having an issue executing this query. SELECT * FROM gob_attachment WHERE CONTAINS
I'm having an issue trying to get my SQL query which works fine in
I keep having the same issue revolving a particular query which has the following
Here is the issue I am having: I have a large query that needs
I'm having an issue with a query I wrote where for some reason the
I'm having an issue in my mongo group query: (this is PHP code) $conditions

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.