Issue getting Last_Day(`Date`) working properly in mysql query

Hi,

 

I have a MySql dataflow that I'm having an issue with.  I have this query that I'm working with and I'm trying to pull the latest NumProjects value in each month. In the output, I'm seeing that its showing the latest set of values twice because the values are different (I assume). This winds up throwing my chart off and summing the two numbers together which misrepresents the intended value. Any ideas on how to fix this would be greatly appreciated.

 

SELECT `NumProjects`, Last_Day(`Date`) AS 'Date', `PhaseName`
FROM `pmat_historical_all_appended`
GROUP BY `PhaseName`, `Date`
ORDER BY `Date` desc

Screen Shot 2018-07-24 at 1.30.11 PM.png

Best Answer

  • Valiant
    Valiant Coach
    Answer ✓

    I think you need to change your grouping. Try this:

    SELECT `NumProjects`, Last_Day(`Date`) AS 'Date', `PhaseName`
    FROM `pmat_historical_all_appended`
    GROUP BY `PhaseName`, Last_Day(`Date`)
    ORDER BY `Date` desc

    Does that give you the result you're looking for?

     

    Or you might could try:

    SELECT `NumProjects`, `Date`, `PhaseName`
    FROM `pmat_historical_all_appended`
    WHERE `Date` IN (SELECT DISTINCT LAST_DAY(`Date`) FROM `pmat_historical_all_appended`)
    ORDER BY `Date` desc

     

    Sincerely,

    Valiant

     

    **Please mark "Accept as Solution" if this post solves your problem
    **Say "Thanks" by clicking the "heart" in the post that helped you.

Answers

  • Valiant
    Valiant Coach
    Answer ✓

    I think you need to change your grouping. Try this:

    SELECT `NumProjects`, Last_Day(`Date`) AS 'Date', `PhaseName`
    FROM `pmat_historical_all_appended`
    GROUP BY `PhaseName`, Last_Day(`Date`)
    ORDER BY `Date` desc

    Does that give you the result you're looking for?

     

    Or you might could try:

    SELECT `NumProjects`, `Date`, `PhaseName`
    FROM `pmat_historical_all_appended`
    WHERE `Date` IN (SELECT DISTINCT LAST_DAY(`Date`) FROM `pmat_historical_all_appended`)
    ORDER BY `Date` desc

     

    Sincerely,

    Valiant

     

    **Please mark "Accept as Solution" if this post solves your problem
    **Say "Thanks" by clicking the "heart" in the post that helped you.

  • Thank you. I'm not sure why I didn't try that myself but your first solution worked perfectly.

  • Glad to hear it! We all need a 2nd pair of eyes on our code sometimes. Smiley Very Happy