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.