Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.1
Name and version of the database you are using: Sql Server
I need to obtain sum of two date columns (using group by) as the number of days.
In HQL i would use this:
Code:
Query q = session.createQuery( ""
+ "select new map( "
+ " period.username as username,"
+ " period.travelCountry as country,"
+ " SUM(period.endDate-period.startDate)"
+ " )"
+ " from CalendarPeriod period"
+ " where period.endDate >= :startDate"
+ " and period.startDate <= :endDate"
+ " group by period.username, period.travelCountry"
);
Hoverver I cannot obtain the difference between two dates using
SUM(period.endDate-period.startDate)... because of using SqlServer i would use native query (via session.createSqlQuery()) using the SqlServer native date calculation functions:
SUM( DATEDIFF( Day, start_date, end_date ))
but unfortunaltelly i'm not able to produce correct code.. Can anyone lend me a hand and show how to rewrite this using native sql query?