-->
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.  [ 2 posts ] 
Author Message
 Post subject: SubQuery in Select Clause
PostPosted: Wed May 25, 2005 1:58 pm 
Newbie

Joined: Mon May 24, 2004 8:34 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Name and version of the database you are using: DB2 v8.2

The generated SQL (show_sql=true):
Hibernate: select year(contentite0_.PUBLISHDATE) as col_0_0_, month(contentite0_.PUBLISHDATE) as col_1_0_, count(contentite0_.PUBLISHDATE) as col_2_0_ from KC.CONTENTITEM contentite0_ group by year(contentite0_.PUBLISHDATE) , month(contentite0_.PUBLISHDATE)



I am a little unsure regarding the subselect within the select clause. Is this supposed to be supported in hibernate3.0.5?

I am attempting to return four columns from a table containing document details. The first column is the year the document was published, the second column is the month it was published, the third column is the total number of documents published that month, and the fourth column is the number of those published documents which have sinced expired. This can be achieved in SQL by have a nested subselect in the select clause. The SQL would look a little like this:
select
year(publishdate) as y,
month(publishdate) as m,
count(*),
(
select
count(*)
from
contentitem b
where
b.expirydate<current timestamp
and
year(b.publishdate)=year(i.publishdate)
and
month(b.publishdate)=month(i.publishdate)
) as expired
from
contentitem i
group by
year(publishdate),
month(publishdate);


When applying this same method in HQL, when converting to SQL, the subselect is just ignored.

From the logs, you can see that the select is definitely mapped in the HQL
18:53:28,397 DEBUG QueryTranslatorImpl:207 - parse() - HQL: select year(item.publishDate) as yearasdf ,month(item.publishDate) as monthasdf ,count(item.publishDate) ,(select count(nestedItem.id) from com.ibm.swg.kc.hibernate.ContentItem nestedItem where nestedItem.expiryDate<current_timestamp() and year(nestedItem.publishDate)=year(item.publishDate) and month(nestedItem.publishDate)=month(item.publishDate) ) from com.ibm.swg.kc.hibernate.ContentItem item group by year(item.publishDate),month(item.publishDate)
18:53:28,437 DEBUG AST:223 - --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| +-[FROM] 'from'
| | \-[RANGE] 'RANGE'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[IDENT] 'com'
| | | | | | | \-[IDENT] 'ibm'
| | | | | | \-[IDENT] 'swg'
| | | | | \-[IDENT] 'kc'
| | | | \-[IDENT] 'hibernate'
| | | \-[IDENT] 'ContentItem'
| | \-[ALIAS] 'item'
| \-[SELECT] 'select'
| +-[AS] 'as'
| | +-[METHOD_CALL] '('
| | | +-[IDENT] 'year'
| | | \-[EXPR_LIST] 'exprList'
| | | \-[DOT] '.'
| | | +-[IDENT] 'item'
| | | \-[IDENT] 'publishDate'
| | \-[IDENT] 'yearasdf'
| +-[AS] 'as'
| | +-[METHOD_CALL] '('
| | | +-[IDENT] 'month'
| | | \-[EXPR_LIST] 'exprList'
| | | \-[DOT] '.'
| | | +-[IDENT] 'item'
| | | \-[IDENT] 'publishDate'
| | \-[IDENT] 'monthasdf'
| +-[COUNT] 'count'
| | \-[DOT] '.'
| | +-[IDENT] 'item'
| | \-[IDENT] 'publishDate'
| \-[QUERY] 'query'
| +-[SELECT_FROM] 'SELECT_FROM'
| | +-[FROM] 'from'
| | | \-[RANGE] 'RANGE'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[DOT] '.'
| | | | | | | | +-[IDENT] 'com'
| | | | | | | | \-[IDENT] 'ibm'
| | | | | | | \-[IDENT] 'swg'
| | | | | | \-[IDENT] 'kc'
| | | | | \-[IDENT] 'hibernate'
| | | | \-[IDENT] 'ContentItem'
| | | \-[ALIAS] 'nestedItem'
| | \-[SELECT] 'select'
| | \-[COUNT] 'count'
| | \-[DOT] '.'
| | +-[IDENT] 'nestedItem'
| | \-[IDENT] 'id'
| \-[WHERE] 'where'
| \-[AND] 'and'
| +-[AND] 'and'
| | +-[LT] '<'
| | | +-[DOT] '.'
| | | | +-[IDENT] 'nestedItem'
| | | | \-[IDENT] 'expiryDate'
| | | \-[METHOD_CALL] '('
| | | +-[IDENT] 'current_timestamp'
| | | \-[EXPR_LIST] 'exprList'
| | \-[EQ] '='
| | +-[METHOD_CALL] '('
| | | +-[IDENT] 'year'
| | | \-[EXPR_LIST] 'exprList'
| | | \-[DOT] '.'
| | | +-[IDENT] 'nestedItem'
| | | \-[IDENT] 'publishDate'
| | \-[METHOD_CALL] '('
| | +-[IDENT] 'year'
| | \-[EXPR_LIST] 'exprList'
| | \-[DOT] '.'
| | +-[IDENT] 'item'
| | \-[IDENT] 'publishDate'
| \-[EQ] '='
| +-[METHOD_CALL] '('
| | +-[IDENT] 'month'
| | \-[EXPR_LIST] 'exprList'
| | \-[DOT] '.'
| | +-[IDENT] 'nestedItem'
| | \-[IDENT] 'publishDate'
| \-[METHOD_CALL] '('
| +-[IDENT] 'month'
| \-[EXPR_LIST] 'exprList'
| \-[DOT] '.'
| +-[IDENT] 'item'
| \-[IDENT] 'publishDate'
\-[GROUP] 'group'
+-[METHOD_CALL] '('
| +-[IDENT] 'year'
| \-[EXPR_LIST] 'exprList'
| \-[DOT] '.'
| +-[IDENT] 'item'
| \-[IDENT] 'publishDate'
\-[METHOD_CALL] '('
+-[IDENT] 'month'
\-[EXPR_LIST] 'exprList'
\-[DOT] '.'
+-[IDENT] 'item'
\-[IDENT] 'publishDate'

18:53:28,437 DEBUG ErrorCounter:72 - throwQueryException() : no errors
18:53:28,467 DEBUG HqlSqlBaseWalker:120 - query() << begin, level = 1
18:53:28,507 DEBUG FromElement:88 - FromClause{level=1} : com.ibm.swg.kc.hibernate.ContentItem (item) -> contentite0_
18:53:28,507 DEBUG FromReferenceNode:48 - Resolved : item -> contentite0_.IDENTITY
18:53:28,507 DEBUG DotNode:476 - getDataType() : publishDate -> org.hibernate.type.TimestampType@2db892e7
18:53:28,507 DEBUG FromReferenceNode:48 - Resolved : item.publishDate -> contentite0_.PUBLISHDATE
18:53:28,517 DEBUG FromReferenceNode:48 - Resolved : item -> contentite0_.IDENTITY
18:53:28,517 DEBUG DotNode:476 - getDataType() : publishDate -> org.hibernate.type.TimestampType@2db892e7
18:53:28,517 DEBUG FromReferenceNode:48 - Resolved : item.publishDate -> contentite0_.PUBLISHDATE
18:53:28,517 DEBUG FromReferenceNode:48 - Resolved : item -> contentite0_.IDENTITY
18:53:28,517 DEBUG DotNode:476 - getDataType() : publishDate -> org.hibernate.type.TimestampType@2db892e7
18:53:28,517 DEBUG FromReferenceNode:48 - Resolved : item.publishDate -> contentite0_.PUBLISHDATE
18:53:28,527 DEBUG FromReferenceNode:48 - Resolved : item -> contentite0_.IDENTITY
18:53:28,527 DEBUG DotNode:476 - getDataType() : publishDate -> org.hibernate.type.TimestampType@2db892e7
18:53:28,527 DEBUG FromReferenceNode:48 - Resolved : item.publishDate -> contentite0_.PUBLISHDATE
18:53:28,527 DEBUG FromReferenceNode:48 - Resolved : item -> contentite0_.IDENTITY
18:53:28,527 DEBUG DotNode:476 - getDataType() : publishDate -> org.hibernate.type.TimestampType@2db892e7
18:53:28,527 DEBUG FromReferenceNode:48 - Resolved : item.publishDate -> contentite0_.PUBLISHDATE
18:53:28,527 DEBUG HqlSqlBaseWalker:125 - query() : finishing up , level = 1
18:53:28,527 DEBUG HqlSqlWalker:331 - processQuery() : ( SELECT ( {select clause} ( ( year ( exprList ( contentite0_.PUBLISHDATE contentite0_.IDENTITY publishDate ) ) ) ( ( month ( exprList ( contentite0_.PUBLISHDATE contentite0_.IDENTITY publishDate ) ) ) ( count ( contentite0_.PUBLISHDATE contentite0_.IDENTITY publishDate ) ) ) ( FromClause{level=1} KC.CONTENTITEM contentite0_ ) ( group ( ( year ( exprList ( contentite0_.PUBLISHDATE contentite0_.IDENTITY publishDate ) ) ) ( ( month ( exprList ( contentite0_.PUBLISHDATE contentite0_.IDENTITY publishDate ) ) ) ) )
18:53:28,547 DEBUG JoinProcessor:112 - Using FROM fragment [KC.CONTENTITEM contentite0_]
18:53:28,547 DEBUG HqlSqlBaseWalker:128 - query() >> end, level = 1


but the final SQL doesn't reflect this:

Hibernate: select year(contentite0_.PUBLISHDATE) as col_0_0_, month(contentite0_.PUBLISHDATE) as col_1_0_, count(contentite0_.PUBLISHDATE) as col_2_0_ from KC.CONTENTITEM contentite0_ group by year(contentite0_.PUBLISHDATE) , month(contentite0_.PUBLISHDATE)


Any ideas?

Regards

Matt


Top
 Profile  
 
 Post subject: Solution Found
PostPosted: Fri May 27, 2005 7:13 am 
Newbie

Joined: Mon May 24, 2004 8:34 am
Posts: 5
Hi,

After much working, I came up with a solution to this problem such that an embedded SQL didn't need to be used. I utilised the sum aggregate function in combination with a case statement, so the HQL ends up looking as follows

select
year(item.publishDate)as year ,
month(item.publishDate) as month ,
count(item.publishDate) as numDocs ,
sum(
case
when
item.expiryDate is not null and item.expiryDate<current_timestamp()
then
1
else
0
end
) as expired ,
sum(
case
when
item.expiryDate is null or item.expiryDate>current_timestamp()
then
1
else
0
end
)as active
from
ContentItem item
group by
year(item.publishDate),
month(item.publishDate)


OR IF YOU PREFER ALL ON ONE LINE


select year(item.publishDate)as year ,month(item.publishDate) as month ,count(item.publishDate) as numDocs ,sum(case when item.expiryDate is not null and item.expiryDate<current_timestamp() then 1 else 0 end) as expired ,sum(case when item.expiryDate is null or item.expiryDate>current_timestamp() then 1 else 0 end) as active from ContentItem item group by year(item.publishDate),month(item.publishDate)


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

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.