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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:40:51+00:00 2026-06-12T00:40:51+00:00

I have an issue with this mysql query. basically I need to join one

  • 0

I have an issue with this mysql query. basically I need to join one table (CostiFissi, a table with costs) with a table with payments (Pagamenti), grouping them by cost id (CostiFissi_IdCostoFisso) and calculating the average between monthly sums of payments amount (not just AVG(Totale) ).

i.e.: the average between 1000+2000 on September and 3000 on October should return 2250 not 3000

this is what I did so far:

SELECT `cf`.`IdCostoFisso`, 
       `cf`.`Nome`, 
       `cf`.`Frequenza`, 
       `cf`.`Importo`, 
       `cf`.`DateFrom`, 
       `cf`.`DateTo`, 
        SUM( p.Totale ) PagamentiTotale, 
        COUNT( p.IdPagamento ) PagamentiNum, 
        AVG(p2.somma_mese) Media 
 FROM (`CostiFissi` cf) 
 LEFT JOIN `Pagamenti` p ON `p`.`CostiFissi_IdCostoFisso` = `cf`.`IdCostoFisso` 
 LEFT JOIN (
             SELECT MONTH(Data), 
                    YEAR(Data), 
                    CostiFissi_IdCostoFisso, 
                    SUM(Totale) somma_mese 
             FROM Pagamenti 
             GROUP BY YEAR(Data), 
                      MONTH(Data), 
                      CostiFissi_IdCostoFisso 
           ) AS p2 ON `p2`.`CostiFissi_IdCostoFisso` = `cf`.`IdCostoFisso` 
  WHERE `cf`.`DateTo` > '2012-09-27 09:46:14' 
         AND `p`.`Data` >= '2012-01-01 00:00:01' 
         AND `p`.`Data` <= '2012-12-31 23:59:59' 
  GROUP BY `cf`.`IdCostoFisso`

than when I run the query I get this issue: say I have 2 costs, cost_a (w/3 payments) and cost_b (w/1 payment), I get back the right mean for both (as I want it to be calculated) but COUNT( p.IdPagamento) returns 6 (and not 3) for cost_a and 1 for cost_b. the same for SUM (p.Totale), that’s doubled for cost_a and not for cost_b.

maybe is an issue with table p joining, dunno… it took a while for me to get to this point but now it’s a bit messy and I can’t get further >_<

ty!

  • 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-12T00:40:52+00:00Added an answer on June 12, 2026 at 12:40 am

    Use COUNT(DISTINCT) like so:

    COUNT(DISTINCT p.IdPagamento)
    

    Instead of COUNT(p.IdPagamento).

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

Sidebar

Related Questions

Bit confused with this one. Basically, I have a column in a table, and
I have this issue: first, I execute a SQL query using Java and then
I have an issue where I need to AS a column in a MySQL
I have a simple mysql query checking the database table 'all_users' for an e-mail
I have this issue that I can't seem to get my head around... Basically
So I have a MySQL table that looks something like this: id timestamp action
I have this MySQL query : while ($x <= 9) { $data_1 = SELECT
I have a table in my MySQL DB which basically contains cron-like tasks. Basically
I have this issue trying to get all the text nodes in an HTML
I have this issue with ComboBox that uses IEnumerable<Brush> as ItemsSource; the problem lies

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.