{"id":1348,"date":"2015-02-27T10:40:10","date_gmt":"2015-02-27T10:40:10","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=1348"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"group-by","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/databases\/sqlite\/queries\/select\/group-by","title":{"rendered":"GROUP BY"},"content":{"rendered":"<h4>\nIMPORTANT NOTE ABOUT GROUP BY<br \/>\n<\/h4>\n<p>\nThe fields you get returned are not necessarily the fields from the exact same row in a collection of group by rows. &nbsp;This is relevant when you for instance&nbsp;want to get the first occurrence of something within the group by, i.e. the row with the earliest datetime value (which wasn&#39;t used to group by). &nbsp;In this situation you can&#39;t rely on the GROUP BY to give you that. The ORDER BY you use will have no effect on the aggregation performed by the GROUP BY as it is only applied afterwards on the results of the GROUP BY. &nbsp;The values returned have to be assumed to be from a random row \/ rows within the group. &nbsp;There is a solution however &#8211; see below\n<\/p>\n<h4>\nHow To Get Sort Within A Group By<br \/>\n<\/h4>\n<p>\nExample of how to get the earliest datetime row value within a GROUP BY:\n<\/p>\n<pre>\r\n<code>\r\n\tCommand1-&gt;CommandText = &quot;SELECT LogMeltCode, datetime(MIN(LogDateTime)) as LogDateTime, LogLotNo, LogWorker, LogInstrumentNo \\\r\n\t\t\t\t\t\t\t\tFROM tblLogEvents \\\r\n\t\t\t\t\t\t\t\tGROUP BY LogMeltCode, LogLotNo, LogWorker, LogInstrumentNo \\\r\n\t\t\t\t\t\t\t\tORDER BY &quot; + SearchOrderBy;\r\n\t\/\/The MIN() above ensures we get the earliest occurance of that field\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IMPORTANT NOTE ABOUT GROUP BY The fields you get returned are not necessarily the fields from the exact same row in a collection of group by rows. &nbsp;This is relevant when you for instance&nbsp;want to get the first occurrence of something within the group by, i.e. the row with the earliest datetime value (which wasn&#39;t [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-1348","post","type-post","status-publish","format-standard","hentry","category-select"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=1348"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1348\/revisions"}],"predecessor-version":[{"id":1349,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1348\/revisions\/1349"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=1348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=1348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=1348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}