Message Boards

How to use dynamic query and extract week from create date

aakash bansal, modified 7 Years ago.

How to use dynamic query and extract week from create date

New Member Posts: 6 Join Date: 8/11/16 Recent Posts
How to use dyanmic query and extract week from create date
If we have create date in mysql database , how can we fetch weeks from create dates through dynamic query in liferay 6.2?
thumbnail
David H Nebinger, modified 7 Years ago.

RE: How to use dynamic query and extract week from create date

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
You can't. This is typically not going to be a SQL-92 sort of call, at best it may be a special database-supported option.

If MySQL supports it (which I'm not really sure that it does), you'd have to use a custom SQL to make this happen.

Alternatively, I'd actually handle this at store time. Create an additional column in the DB to store the week number and, when you are persisting the data, calculate and store the value. This way you can use DQ on it and it will work with any database.
aakash bansal, modified 7 Years ago.

RE: How to use dynamic query and extract week from create date

New Member Posts: 6 Join Date: 8/11/16 Recent Posts
Can we use date functions like extract and year month in dynamic query in liferay ?
thumbnail
David H Nebinger, modified 7 Years ago.

RE: How to use dynamic query and extract week from create date

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
DQ is just a java-based way for defining an HQL statement for eventual processing by Hibernate. It doesn't really offer much beyond what is supported by HQL.

Whatever HQL supports, DQ will support.

Again, you're thinking about this all wrong. If your table had 5 million rows, any kind of function like this is going to be a table scan action to pull out the data followed by X amount of time to process the value on each row. Regardless how small X is, when multiplied by 5 million you're looking at a huge performance impact on your database and a slow performing query.

Even if you respond with "Well I will only have Y rows, not 5 million" you must still agree that X will basically be a constant and that constant will be multiplied by Y for the performance impact.

Then consider how many times the query might be called, so you have another multiplier based on how often people are using the portlet which needs the data.

Save yourself some time and headache now. Flatten your data model so you can get to the week using a simple indexed query. You're going to save yourself and your DBA a lot of time and problems.