0
SELECT    taxi.id as id,name,
  SUM(storico_pagamenti.importo) AS pagamentitotali,
  SUM(CASE WHEN storico_pagamenti.data <= '$timestop' AND storico_pagamenti.data >=     '$timestart' THEN storico_pagamenti.importo ELSE 0 END) AS pagamentimese,
  SUM(storico_fatture.importo) AS fatturetotali,
  SUM(CASE WHEN storico_fatture.data <= '$timestop' AND storico_fatture.data >= '$timestart' THEN storico_fatture.importo ELSE 0 END) AS fatturemese,
  COUNT(giornifermi.registrazione) as giornifermitotali,
  SUM(CASE WHEN giornifermi.data <= '$timestop' AND giornifermi.data >= '$timestart' THEN 1 ELSE 0 END) AS giornifermimese
FROM taxi 
  LEFT JOIN storico_pagamenti ON taxi.id = storico_pagamenti.id_taxi
  LEFT JOIN storico_fatture ON storico_pagamenti.id_taxi=storico_fatture.id_taxi
  LEFT JOIN giornifermi ON storico_fatture.id_taxi=giornifermi.id_taxi
WHERE taxi.categoria='cv'
GROUP BY  taxi.id;

My mysql skills aren't enough for this query... can't understand where is the problem. The part that is not working is this

SUM(CASE WHEN giornifermi.data <= '$timestop' AND giornifermi.data >= '$timestart' THEN 1
ELSE 0 END) AS giornifermimese

which I expected to give me the count of the rows of giornifermi.data between two dates submitted

and this other part:

COUNT(giornifermi.registrazione) as giornifermitotali

which I expected to give me the count of all the rows in giornifermi.registrazione column

Is this because of the join? Or is it a problem in the two parts of the query?

EDIT----------------------------------------- I've related now all the tables "ON" the same referrer taxi.id setting it equal to the variable id_taxi of the other tables and now I get the values but wrong. After the suggestion of @Twelf I've tried to comment the "GROUP BY taxi.id" and try select *. I've seen the problem now is that results are multiplied. Example If i have three rows in giornifermi, they became 6, if i have 2 rows in storico_pagamenti, It copies me again the three rows for each row in storico_pagamenti. in Don't have any idea of how to fix that but knowing the problem is better than nothing

EDIT------------------------------------------ Fixed by adding a subquery after the left join, instead of joining the whole table of giornifermi i've created there a brand new table to correct the data without duplicates

LEFT JOIN (SELECT 
taxi.id as i,
COUNT(giornifermi.registrazione) as giornifermitotali,
SUM(CASE
    WHEN giornifermi.data <= '$timestop' AND giornifermi.data >= '$timestart' THEN 1
        ELSE 0
    END) as giornifermimese
FROM taxi
LEFT JOIN giornifermi ON taxi.id=giornifermi.id_taxi
GROUP BY taxi.id
) AS NUOVATABELLA ON taxi.id=NUOVATABELLA.i
8
  • 2
    not working means? The syntax is correct. Commented Sep 12, 2013 at 16:49
  • What are the expected results and how do they compare with present results? Commented Sep 12, 2013 at 16:52
  • it doesn't give me the result expected, the count of the rows is always wrong Commented Sep 12, 2013 at 16:54
  • This is going to be tough to determine why your data is wrong since it is fine syntax-wise and we really don't know what your tables mean. Commented Sep 12, 2013 at 16:55
  • now the are all 0, before i was trying to relate all the join to the first table like LEFT JOIN storico_pagamenti ON taxi.id = storico_pagamenti.id_taxi LEFT JOIN storico_fatture ON taxi.id=storico_fatture.id_taxi LEFT JOIN giornifermi ON taxi.id=giornifermi.id_taxi and I got some numbers but wrong... Commented Sep 12, 2013 at 17:01

1 Answer 1

1

It's impossible to tell from your question, but basic trouble-shooting for 'wrong' counts:

  1. If counts are too high and you have JOIN's then it's likely insufficient JOIN criteria, IE Cartesian product.
  2. If counts are too low, you're likely excluding records you don't mean to through WHERE criteria or restrictive JOIN criteria.
  3. Select all fields for a small subset of records to help see where the discrepancy is coming from.

Update:

One or more of your tables has multiple entries for all/some taxi_ID values.

Run this for each table:

SELECT taxi_ID 
FROM storico_pagamenti 
GROUP BY taxi_ID HAVING COUNT(*) > 1

If any tables return records, look at them to see if there's another field you should be using in the JOIN to differentiate between the duplicates.

Sign up to request clarification or add additional context in comments.

5 Comments

I've related all the tables "ON" the same referrer taxi.id and now I get the values but wrong. After the suggestion of @Twelf I've tried to comment the "GROUP BY taxi.id" and try select *. I've seen the problem now is that results are multiplied. Example If i have three rows in giornifermi, they became 6, if i have 2 rows in storico_pagamenti, It copies me again the three rows for each row in storico_pagamenti. in Don't have any idea of how to fix that but knowing the problem is better than nothing
Updated answer, should be able to find out which table is problematic easily.
what do you mean? of course I have a unique key for each record in those tables, but how can I add them to the join? the unique key column is call 'registrazione'
You just want to isolate which tables are resulting in duplicates, one of your tables has multiple records for the same taxi_ID, you need to handle those, you can make a subquery for the offending table and group by taxi_ID, or you can add to your JOIN criteria so that only one record joins between each table. Without seeing data I can't know which you need to do.
i fixed it creating a subquery after the left join to create a brand new table to join. LEFT JOIN (SELECT taxi.id as i, COUNT(giornifermi.registrazione) as giornifermitotali, SUM(CASE WHEN giornifermi.data <= '$timestop' AND giornifermi.data >= '$timestart' THEN 1 ELSE 0 END) as giornifermimese FROM taxi LEFT JOIN giornifermi ON taxi.id=giornifermi.id_taxi GROUP BY taxi.id ) AS NUOVATABELLA ON taxi.id=NUOVATABELLA.i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.