{"id":5083,"date":"2012-02-13T15:00:56","date_gmt":"2012-02-13T09:30:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5083"},"modified":"2012-02-13T15:00:56","modified_gmt":"2012-02-13T09:30:56","slug":"using-aliases-in-aggregate-functions-of-criteria-queries","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-aliases-in-aggregate-functions-of-criteria-queries\/","title":{"rendered":"Using aliases in aggregate functions of criteria queries"},"content":{"rendered":"<p>When using criteria queries, many times we encounter a use case where we need to order our result not on any field of domain class but on  our result set, in these cases aliases can be used. <\/p>\n<pre>\r\n<\/pre>\n<p>In my project,  use case was to find most liked products from the domain:<br \/>\n[java]<br \/>\nProductStat{<br \/>\nDate dateCreated<br \/>\nProduct product<br \/>\n}<br \/>\n[\/java]<br \/>\nTo find the result, the query I used was :<\/p>\n<p>[java]<br \/>\nProductStat.createCriteria().list() {<br \/>\n            projections {<br \/>\n                groupProperty(&quot;product&quot;)<br \/>\n                count(&quot;product&quot;, &quot;totalProducts&quot;)  \/\/ alias totalProducts<br \/>\n            }<br \/>\n            order(&quot;totalProducts&quot;, &quot;desc&quot;)<br \/>\n}<br \/>\n[\/java]<\/p>\n<pre>\r\n<\/pre>\n<p>In this way using alias &#8220;totalProducts&#8221; I was able to order the result according to the count of products.<\/p>\n<pre>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When using criteria queries, many times we encounter a use case where we need to order our result not on any field of domain class but on our result set, in these cases aliases can be used. In my project, use case was to find most liked products from the domain: [java] ProductStat{ Date dateCreated [&hellip;]<\/p>\n","protected":false},"author":57,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[764,266,269,4840],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5083"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5083"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5083\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}