{"id":9,"date":"2008-02-28T17:30:00","date_gmt":"2008-02-28T23:30:00","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=9"},"modified":"2016-01-01T23:53:31","modified_gmt":"2016-01-02T05:53:31","slug":"group-by-and-case-statements-or-other-sql-functions","status":"publish","type":"post","link":"https:\/\/jackdonnell.com\/?p=9","title":{"rendered":"Group By and Case Statements or Other SQL Functions"},"content":{"rendered":"<p>One thing I forget to do often is to include the actual item I&#8217;m returning in the Select Statement in the Group By. Meaning , if I use a case statement of some function to change the value that I&#8217;m returning, then I should include that and not the base column name in the Group By part of the select statement<\/p>\n<p><code><br \/>\n\/*<br \/>\nIf you write something like this and the Col2 has multiple 'Unique' Values<br \/>\nthen you will get a row for each value distinct Col2 value that says 'Other'<br \/>\n*\/<\/code><code>SELECT [Col1]<br \/>\n,Case When [Col2] = 10 then 'Ten'<br \/>\nElse 'Other' End as [LameExample]<br \/>\n,Count(*) as CNT<br \/>\nFROM [dbo].[lameTableExample]<br \/>\nGROUP BY [Col1], [Col2]<\/p>\n<p>-- This Example will return Only two rows<\/p>\n<p>SELECT [Col1]<br \/>\n,Case When [Col2] = 10 then 'Ten'<br \/>\nElse 'Other' End as [LameExample]<br \/>\n,Count(*) as CNT<br \/>\nFROM [dbo].[lameTableExample]<br \/>\nGROUP BY [Col1]<br \/>\n, Case When [Col2] = 10 then 'Ten'<br \/>\nElse 'Other' End<\/p>\n<p><\/code><a href=\"https:\/\/jackdonnell.com\/?p=467\">Select Case<\/a> has a simple overview of using CASE in a SQL Query with the SUM() function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing I forget to do often is to include the actual item I&#8217;m returning in the Select Statement in the Group By. Meaning , if I use a case statement of some function to change the value that I&#8217;m &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/jackdonnell.com\/?p=9\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[22,351,5],"tags":[33,34,39,35,52,291],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-ssms","category-t-sql","tag-case","tag-group-by","tag-microsoft","tag-select","tag-ssms","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9"}],"version-history":[{"count":2,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}