嗨,我喜欢月智表jan,feb..dec和我有locatin和actioncount字段中每个表中重复的位置。问题与SQL查询转换为冬眠查询
我有这个查询大致写在SQL中,我有月域对象,现在我必须将其转换为Hibernate查询(HQL或标准api或其他任何..)。我如何转换它? 月的计数是作为一个列表提供的,并且是如此的变量 下面的sql是从这个列表monthsToQuery = [oct,nov,dec,jan]
..这也是变量列表。 它可以是[二月,三月,四月]或[月]
select loc, sum(tcount) from (
(select location as loc, sum(actioncount) as tcount from oct group by location) left-join
(select location as loc, sum(actioncount) as tcount from nov group by location) left-join
(select location as loc, sum(actioncount) as tcount from dec group by location) left-join
(select location as loc, sum(actioncount) as tcount from jan group by location)
) group by loc
我做左加入,因为我不想失去不同月份之间的任何位置。
此外:我还有一个日期范围作为输入。到目前为止,我正在从范围中获得一个月的列表,并获得每月分离的结果。我需要编写查询以在1个查询中提供最终的所需结果。 这里是我有什么至今:
// sTblList - list of all month domains in the date range..
def getSummary(sTblList,SfromDate,StoDate,res_id, groupCol,sumCol){
try{
Date fromDate = new Date().parse("yyyy-MM-dd", SfromDate);
Date toDate = new Date().parse("yyyy-MM-dd", StoDate);
def resourceInstance=Resources.get(res_id);
sTblList.each{
def OnemonthList=it.createCriteria().get {
eq('graresource',resourceInstance)
between('currentdate', fromDate, toDate)
projections {
sum(sumCol,'tcount')
groupProperty(groupCol)
}
}
return sumMap // sumMap should have all months results combined
}
我看了一些地方,与其嵌套规定 - 我还可以在条件中使用别名。我是新来的.. 有没有人知道更进一步?
我会首先对模式进行规范化,然后制作一个包含月份列的表格。这样一切都会容易得多。 – 2012-01-13 10:26:39
我们已经将数据分为12个表格以进行性能和总结,它是一个巨大的表格,否则有5000万条记录。 – 2012-01-13 23:12:32
使用月份列索引时,它可能比加入所有这些表快得多。对于一个好的数据库来说,5000万是没有那么多。 – 2012-01-14 09:00:57