Fórum

How to get distinct row in Dynamic Query

Karthik Charles, modificado 13 Anos atrás.

How to get distinct row in Dynamic Query

New Member Postagens: 7 Data de Entrada: 20/11/10 Postagens Recentes
Hi guys,

I need to get distinct row from table using DynamicQuery. Is it possible to get a distinct row?
For eg..

id title pid sid uid
----- --------- -------- ------- -------
1 Q1 1 1 1
1 Q1 1 1 1
1 Q1 1 1 1
1 Q2 1 1 1
1 Q2 1 1 1
1 Q3 1 1 1
----------------------------------------

I want to get the results as

id title pid sid uid
----------------------------------------
1 Q1 1 1 1
1 Q2 1 1 1

Pl help guys...
thumbnail
mohammed azam, modificado 13 Anos atrás.

RE: How to get distinct row in Dynamic Query

Regular Member Postagens: 159 Data de Entrada: 06/11/09 Postagens Recentes
Hi karthik,

I have tried this piece of code it worked for me. Hope this works for you as well. I have tried in jsp file.

<%@page import="com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil"%> 
<%@ page import="com.liferay.portal.kernel.dao.orm.DynamicQuery" %>
<%@ page import="com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil" %>

<%
	List results1 = new ArrayList();
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(AzamTest.class);
	
	dynamicQuery.setProjection(ProjectionFactoryUtil.distinct(ProjectionFactoryUtil.rowCount()));
	
	results1.addAll(AzamTestLocalServiceUtil.dynamicQuery(dynamicQuery));

%>

<%=results1.size() %>
Karthik Charles, modificado 13 Anos atrás.

RE: How to get distinct row in Dynamic Query

New Member Postagens: 7 Data de Entrada: 20/11/10 Postagens Recentes
Hi Azam,

Thanks for ur response..

The above query you mentioned returns the row-count of distinct values. But I need the whole rows which are distinct. Give me a suggestion please..
thumbnail
Leonid Zverugo, modificado 10 Anos atrás.

RE: How to get distinct row in Dynamic Query

New Member Mensagem: 1 Data de Entrada: 11/01/14 Postagens Recentes
Very old topic, but I found it only now, however, I hope this would be useful for somebody. Well, I had the same issue in my project (I am very new to Liferay emoticon ) - to select distinct column value using DynamicQuery:

    DynamicQuery query = DynamicQueryFactoryUtil.forClass(RequestForProposal.class)
                 .setProjection(
                     ProjectionFactoryUtil.distinct(
                         ProjectionFactoryUtil.property(StringConstant.PRIORITY_FIELD.toString())
                     )
                 );



Hope, this helps someone emoticon