FetchXml查询distinct无效:重复获取cic_disponibilitesid问题求助
FetchXml 去重失效问题解决方法
问题出在:你虽然给fetch节点加了distinct="true",但因为查询里包含了关联表cic_engagements的cic_name属性,系统会把主实体所有属性+关联实体属性的组合作为去重依据。只要关联的cic_engagements行cic_name不同,哪怕主实体cic_disponibilitesid相同,也会被判定为不同行返回。
方案1:不需要关联实体数据时
直接删掉关联的link-entity节点,这样distinct会基于主实体cic_disponibilites的属性做去重,就能得到唯一的cic_disponibilites行:
<fetch distinct="true"> <entity name="cic_disponibilites"> <attribute name="cic_date" /> <attribute name="cic_disponibleam" /> <attribute name="cic_disponiblepm" /> <attribute name="cic_disponibilitesid" /> <filter type="and"> <condition attribute="cic_date" operator="ge" value="2024-01-01" /> <condition attribute="cic_date" operator="le" value="2024-02-28" /> <condition attribute="cic_nameofcustomer" operator="eq" value="Brad Copper" /> </filter> </entity> </fetch>
方案2:需要关联实体数据,但要主实体唯一行
用聚合查询,通过group by主实体的所有属性来确保每行对应唯一的cic_disponibilites记录,同时对关联实体的属性用聚合函数(比如取第一条、最大值等)处理:
<fetch aggregate="true"> <entity name="cic_disponibilites"> <attribute name="cic_date" groupby="true" alias="date" /> <attribute name="cic_disponibleam" groupby="true" alias="available_am" /> <attribute name="cic_disponiblepm" groupby="true" alias="available_pm" /> <attribute name="cic_disponibilitesid" groupby="true" alias="disponibilite_id" /> <filter type="and"> <condition attribute="cic_date" operator="ge" value="2024-01-01" /> <condition attribute="cic_date" operator="le" value="2024-02-28" /> <condition attribute="cic_nameofcustomer" operator="eq" value="Brad Copper" /> </filter> <link-entity name="cic_engagements" from="cic_disponibilite" to="cic_disponibilitesid" link-type="outer" alias="Engagements"> <attribute name="cic_name" aggregate="max" alias="engagement_name" /> </link-entity> </entity> </fetch>
这里用了max聚合函数取关联的cic_name最大值,你也可以根据需求换成min或者first(取决于你的数据源支持情况)。
内容的提问来源于stack exchange,提问作者David B.
相关产品推荐
相关产品推荐

