{"id":313,"date":"2017-05-03T13:58:14","date_gmt":"2017-05-03T05:58:14","guid":{"rendered":"http:\/\/dingqs.imwork.net\/?p=313"},"modified":"2017-05-03T13:58:14","modified_gmt":"2017-05-03T05:58:14","slug":"jpa-%e7%9a%84%e8%81%9a%e5%90%88%e6%9f%a5%e8%af%a2-countmaxminavg","status":"publish","type":"post","link":"https:\/\/www.dd-home.top\/?p=313","title":{"rendered":"JPA \u7684\u805a\u5408\u67e5\u8be2 count,max,min,avg"},"content":{"rendered":"<p><!--more--><\/p>\n<p>1.Monitor\u5bf9\u8c61<\/p>\n<hr \/>\n<p>private String name;<br \/>\nprivate String max=&#8221;0&#8243;;<br \/>\nprivate String min=&#8221;0&#8243;;<br \/>\nprivate Double avage;<br \/>\n\/** 0-1 **\/<br \/>\nprivate Long section1;<br \/>\n\/** 1-2 **\/<br \/>\nprivate Long section2;<br \/>\n\/** 2-3 **\/<br \/>\nprivate Long section3;<br \/>\n\/** 3-4 **\/<br \/>\nprivate Long section4;<br \/>\n\/** 4-5 **\/<br \/>\nprivate Long section5;<br \/>\n\/** 5-6 **\/<br \/>\nprivate Long section6;<br \/>\n\/** 6-7 **\/<br \/>\nprivate Long section7;<br \/>\n\/** 7-8 **\/<br \/>\nprivate Long section8;<br \/>\n\/** 8-9 **\/<br \/>\nprivate Long section9;<br \/>\n\/** 9-10 **\/<br \/>\nprivate Long section10;<\/p>\n<p>private Set&lt;BeanMItem&gt; items=new HashSet&lt;BeanMItem&gt;();<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>2.BeanMItem\u5bf9\u8c61<\/p>\n<hr \/>\n<p>private String companyName;<br \/>\nprivate String failureRate;<\/p>\n<p>private BeanMonitor monitor;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>3.\u83b7\u53d6\u5f53\u524d\u6700\u65b0\u7684Monitor\u5bf9\u8c61\u5e76\u83b7\u53d6\u5176\u4e2d\u7684\u89c2\u6d4b\u6761\u76ee\u7684\u6700\u5927\u503c,\u6700\u5c0f\u503c\u548c\u5e73\u5747\u503c,\u4ee5\u53ca\u5bf9\u5e94\u7684\u533a\u95f4\u6570\u91cf<\/p>\n<p>a.left join<\/p>\n<p>b.group by<\/p>\n<p>c.MAX,MIN,AVG<\/p>\n<p>d.sum(case when boolean then 1 else 0 end)<\/p>\n<hr \/>\n<p>QueryResult qr = new QueryResult&lt;BeanMonitor&gt;();<\/p>\n<p><code> String entityname = getEntityName(this.entityClass);<br \/>\nQuery query = em.createQuery(\"select new BeanMonitor(o.id,o.name,<span style=\"color: #ff0000;\">MAX(items.failureRate),MIN(items.failureRate),avg(items.failureRate)<\/span>,\"<br \/>\n+ \"<span style=\"color: #ff0000;\">sum(case when items.failureRate &lt; 10 then 1 else 0 end)<\/span>,\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 10 and items.failureRate &lt; 20) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 20 and items.failureRate &lt; 30) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 30 and items.failureRate &lt; 40) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 40 and items.failureRate &lt; 50) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 50 and items.failureRate &lt; 60) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 60 and items.failureRate &lt; 70) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 70 and items.failureRate &lt; 80) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 80 and items.failureRate &lt; 90) then 1 else 0 end),\"<br \/>\n+ \"sum(case when (items.failureRate &gt;= 90) then 1 else 0 end)) from \" + entityname + \" o<span style=\"color: #ff0000;\"> left join o.items as items<\/span> \" + (wherejpql == null || \"\".equals(wherejpql.trim()) ? \"\" : \"where \" + wherejpql) +\" <span style=\"color: #ff0000;\">group by<\/span> o.id \" +buildOrderby(orderby));<br \/>\nsetQueryParams(query, queryParams);<br \/>\nif (firstResult != -1 &amp;&amp; maxresult != -1)<br \/>\nquery.setFirstResult(firstResult).setMaxResults(maxresult);<br \/>\nqr.setResultlist(query.getResultList());<\/code><\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,22,31],"tags":[],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-hibernate","category-jpa","category-sql"],"_links":{"self":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=313"}],"version-history":[{"count":0,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/posts\/313\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}