2012-01-13 61 views
0

嗨,我喜欢月智表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 
    } 

我看了一些地方,与其嵌套规定 - 我还可以在条件中使用别名。我是新来的.. 有没有人知道更进一步?

+0

我会首先对模式进行规范化,然后制作一个包含月份列的表格。这样一切都会容易得多。 – 2012-01-13 10:26:39

+0

我们已经将数据分为12个表格以进行性能和总结,它是一个巨大的表格,否则有5000万条记录。 – 2012-01-13 23:12:32

+0

使用月份列索引时,它可能比加入所有这些表快得多。对于一个好的数据库来说,5000万是没有那么多。 – 2012-01-14 09:00:57

回答

1

如果您的模型使用继承

abstract class Month 
{ 
    string location; 
    int actionCount; 
} 

class January extends Month 
{ 
} 

session.CreateCriteria(Month.class) 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty("Location")) 
     .Add(Projections.Sum("ActionCount"))) 

或接口

class Month implements HasActionCount 
{ 
    string location; 
    int actionCount; 
} 

session.CreateCriteria(HasActionCount.class) 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty("Location")) 
     .Add(Projections.Sum("ActionCount"))) 

更新:为NHibernate和SQLite的以下作品(也应该在Hibernate中工作)

class Month 
{ 
    public virtual int Id { get; set; } 
    public virtual string Location { get; set; } 
    public virtual int ActionCount { get; set; } 
} 

class January : Month 
{ 
} 

class February : Month 
{ 
} 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="ConsoleApplication1.January, ConsoleApplication1" table="`January`"> 
    <id name="Id"> 
     <generator class="identity" /> 
    </id> 
    <property name="Location" /> 
    <property name="ActionCount" /> 
    </class> 
    <class xmlns="urn:nhibernate-mapping-2.2" name="ConsoleApplication1.February, ConsoleApplication1" table="`February`"> 
    <id name="Id"> 
     <generator class="identity" /> 
    </id> 
    <property name="Location" /> 
    <property name="ActionCount" /> 
    </class> 
</hibernate-mapping> 

// works as expected 
IList<Month> months = session.CreateCriteria<Month>().List<Month>(); 

// returns the location and sum of each month though 
IList<Object[]> sums = (Object[])session.CreateCriteria<Month>() 
    .SetProjection(Projections.ProjectionList() 
     .Add(Projections.GroupProperty("Location")) 
     .Add(Projections.Sum("ActionCount"))) 
    .List(); 
+0

感谢您的提示..那么月份的域名类将会存储来自12个月度表的所有位置和行动计数是吗?因为12个表中的插入是通过在后台运行的夜间作业完成的,所以即使对于这个基本域,插入过程也必须完成? – 2012-01-13 23:10:23

+0

你必须独立映射所有月份,但hibernate足够聪明,知道当你查询基类时,它必须查询所有继承的类,并获得一个继承类(1月,...)的列表。生成的SQL应该看起来像你的查询。接口也一样,我不确定接口是否需要映射,可能不是。 – Firo 2012-01-14 22:51:57

+0

我也刚刚在一些相关的帖子上看到,课堂月份需要抽象吗? – 2012-01-15 18:16:04