tags:

views:

33

answers:

1

I have a table where I'm trying to pull some trend analysis from where the columns are Date(timestamp),RiskCategory(text) and Point(int). What I'm trying to return is for every date SUM the points and group by RiskCategory. I can get the latest via the following:

SELECT Date,RiskCategory,SUM(Point) AS Total 
FROM risktrend WHERE DATE(Date) >= (SELECT MAX(DATE(Date)) FROM risktrend) 
GROUP BY RiskCategory;

but am struggling with the returning the same for ALL dates. Am using MySQL.

I should further elaborate, that any date can have multiple entries, but the RiskCategory can only be Administrative,Availability, or Capacity. So, for every date I should see a SUM of points for the latter three. For example,

2010-10-06 Capacity 508
2010-10-06 Administrative 113
2010-10-06 Availability 243
2010-10-07 Capacity 493
2010-10-07 Administrative 257
2010-10-07 Availability 324
+2  A: 

You need to add the date to your group by clause:

SELECT Date,
       RiskCategory,
       SUM(Point) AS Total 
FROM risktrend 
WHERE DATE(Date) >= (SELECT MAX(DATE(Date)) FROM risktrend) 
GROUP BY Date, RiskCategory;
akf
You pointed me in the right direction. The SQL ended up being the following: SELECT Date, RiskCategory, SUM(Point) AS Total FROM risktrend GROUP BY DATE(Date),RiskCategory;