{"id":222,"date":"2017-05-18T16:45:28","date_gmt":"2017-05-18T20:45:28","guid":{"rendered":"http:\/\/techref.camellarry.com\/?p=222"},"modified":"2017-05-18T16:50:25","modified_gmt":"2017-05-18T20:50:25","slug":"sql-summing-daily-totals","status":"publish","type":"post","link":"http:\/\/techref.camellarry.com\/?p=222","title":{"rendered":"SQL Summing daily totals"},"content":{"rendered":"<p>This example illustrates how to sum values collected daily into monthly categories.<\/p>\n<pre><span style=\"color: #0000ff;\">SELECT DISTINCT<\/span>\r\n<span style=\"color: #0000ff;\">CONVERT(varchar(4), YEAR(Date)) + '-' + RIGHT('00' + CAST(DATEPART(mm, Date) AS varchar(2)), 2) AS PERIOD,<\/span>\r\n<span style=\"color: #0000ff;\"> \u00a0SUM(Steps) AS TotalSteps,<\/span>\r\n<span style=\"color: #0000ff;\"> \u00a0SUM(Floors) AS TotalFloors,<\/span>\r\n<span style=\"color: #0000ff;\"> \u00a0SUM(Distance_mi) AS TotalMiles,<\/span>\r\n<span style=\"color: #0000ff;\"> \u00a0SUM(Activity) AS ActiveMinutes,<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0SUM(Calories) AS TotalCalories<\/span>\r\n<span style=\"color: #0000ff;\"> FROM \u00a0dbo.FitBit<\/span>\r\n<span style=\"color: #0000ff;\"> GROUP BY CONVERT(varchar(4), YEAR(Date)) + '-' + RIGHT('00' + CAST(DATEPART(mm, Date) AS varchar(2)), 2)<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This example illustrates how to sum values collected daily into monthly categories. SELECT DISTINCT CONVERT(varchar(4), YEAR(Date)) + &#8216;-&#8216; + RIGHT(&#8217;00&#8217; + CAST(DATEPART(mm, Date) AS varchar(2)), 2) AS PERIOD, \u00a0SUM(Steps) AS TotalSteps, \u00a0SUM(Floors) AS TotalFloors, \u00a0SUM(Distance_mi) AS TotalMiles, \u00a0SUM(Activity) AS ActiveMinutes, \u00a0SUM(Calories) AS TotalCalories FROM \u00a0dbo.FitBit GROUP BY CONVERT(varchar(4), YEAR(Date)) + &#8216;-&#8216; + RIGHT(&#8217;00&#8217; + CAST(DATEPART(mm, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8],"tags":[29,70,74],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-sql","tag-example","tag-sql","tag-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9t3oE-3A","_links":{"self":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=222"}],"version-history":[{"count":3,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":225,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/222\/revisions\/225"}],"wp:attachment":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}