Search code examples
mysqlsqllogparser

GROUP BY in sql to get monthly breakdown of data


Say I have a data that describes different items sold and when they were sold. I want to breakdown this data and count different items sold on monthly basis. So here is what I have so far:

SELECT 
 ItemDescription
,OrderReceivedData 
,COUNT(*) AS ItemCount 
INTO 'C:\Users\whatever.csv' 
FROM 'C:\user\inputFileHere.csv' 
GROUP BY ItemDescription, OrderReceivedDate 
HAVING OrderReceivedDate LIKE '%2011%'

Now the thing is that my dates are in a bad format. So what the query above does is that it shows count for an item on 01JAN2011, 02JAN2011, ... , 10FEB2011, ...and so on. But what I want is the count for JAN2011, FEB2011, MAR2011... and so on. So basically I dont wanna GROUP BY OrderReceivedData but I want to Group by these specific 7 characters in OrderReceivedDate so I can ignore the dates. I hope it makes sense. So how do I do this?


Solution

  • SELECT 
     ItemDescription
    ,SUBSTR(OrderReceivedDate,2,7) AS OrderReceivedDateUpdated 
    ,COUNT(*) AS ItemCount 
    INTO 'C:\Users\whatever.csv' 
    FROM 'C:\user\inputFileHere.csv' 
    GROUP BY ItemDescription, OrderReceivedDateUpdated 
    HAVING OrderReceivedDate LIKE '%2011%'