-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Need help with group-by query. I'm stuck
PostPosted: Fri Aug 27, 2004 9:03 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I really need some help with this query. I tables A (RnsTransaction) and B (RnsTransactionHistory) and B has a boolean that I'd like to count all the A's that have every B is true, and all the A's that have a B with at least one false. Here is where I'm at:

Code:
                   "select new com.upfactor.rns.resource.hibernate.report.ReadTransactionCount( " +
                  "   to_char(history.date, 'YYYY-MM'), " +
                  "   count(distinct readTransaction), " +
                  "   count(distinct unreadTransaction) ) " +
                  "from RnsTransaction readTransaction, " +
                  "   RnsTransaction unreadTransaction, " +
                  "   RnsTransactionHistory history " +
                  "where " +
                  "   readTransaction in (" +
                  "      select rnsTransaction " +
                  "      from Administrator administrator " +
                  "         inner join administrator.businesses business " +
                  "         inner join business.rnsTransactions rnsTransaction " +
                  "         inner join rnsTransaction.histories history " +
                  "         inner join administrator.persons person " +
                  "      where " +
                  "         business.id = :businessId and " +
                  "         person.id = :personId and " +
                  "         history.isRead = true and " +
                  "         not exists ( " +
                  "            select history from RnsTransactionHistory " +
                  "            where history.rnsTransaction = rnsTransaction and " +
                  "            history.isRead = false " +
                  "         )" +
                  "   ) and " +
                  "   unreadTransaction in (" +
                  "      select rnsTransaction " +
                  "      from Administrator administrator " +
                  "         inner join administrator.businesses business " +
                  "         inner join business.rnsTransactions rnsTransaction " +
                  "         inner join rnsTransaction.histories history " +
                  "         inner join administrator.persons person " +
                  "      where " +
                  "         business.id = :businessId and " +
                  "         person.id = :personId and " +
                  "         history.isRead = false " +
                  "   ) " +
                  "group by to_char(history.date, 'YYYY-MM') " +
                  "having " +
                  "   count(distinct unreadTransaction) > 0 or " +
                  "   count(distinct readTransaction) > 0 " +
                  "order by to_char(history.date, 'YYYY-MM') desc"


This thing looks like a mess I'm sure there is a better way. Here are the two mappings that matter

Code:
   <class name="com.upfactor.rns.domain.core.RnsTransaction"
      table="rns_transaction">
      <id name="id" column="id" type="long" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">rns_transaction_id_seq</param>
         </generator>
      </id>
      <many-to-one name="business" column="business_id"
         class="com.upfactor.rns.domain.business.Business" />
      <property name="cargoControlNumber" column="cargo_control_number" />
      <property name="transactionNumber" column="transaction_number" />
      <many-to-one name="serviceOption" column="service_option_id" class="com.upfactor.rns.domain.core.ServiceOption" outer-join="false"  />
      <many-to-one name="releaseOffice" column="release_office_id" class="com.upfactor.rns.domain.core.ReleaseOffice" outer-join="false" />
      <many-to-one name="subLocation" column="sub_location_id" class="com.upfactor.rns.domain.core.SubLocation" outer-join="false" />
      <property name="containerNumber" column="container_number" />
      <bag name="histories" table="rns_transaction_history" inverse="true"
         order-by="date desc" cascade="save-update" outer-join="true" lazy="true">
         <key column="rns_transaction_id"/>
         <one-to-many class="com.upfactor.rns.domain.core.RnsTransactionHistory" />
      </bag>
   </class>

   <class
      name="com.upfactor.rns.domain.core.RnsTransactionHistory"
      table="rns_transaction_history"
   >
      <id name="id" column="id" type="long" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">rns_transaction_history_id_seq</param>
         </generator>
      </id>
      <property name="isRead" column="is_read"  />
      <many-to-one name="rnsTransaction" column="rns_transaction_id" class="com.upfactor.rns.domain.core.RnsTransaction"/>
      <property name="date" column="date" />
      <many-to-one name="releaseCode" column="release_code_id" class="com.upfactor.rns.domain.core.ReleaseCode"/>
      <property name="ediMessage" column="edi_message" />
      <property name="deliveryInstructions" column="delivery_instructions" />
   </class>


I really hope someone can help me without since my query just returns 8 read rows and 4 unread rows for every date group.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.