0

I have this query

 select d.ope_supervisorName,d.ope_categoriaName ,d.ope_categoria,d.ope_cuota,d.ope_cuotaventadetId  
 ,f.Ope_name,c.ope_name
 from ope_cuotaventadet d
 join ope_figura f on d.Ope_supervisor=f.ope_figuraid
 join ope_categoriaproducto c on d.ope_categoria=c.ope_categoriaproductoid
 join ope_cuotaventa cv on d.ope_cuotaventaid =cv.ope_cuotaventaId 
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'
 union
 select f.Ope_name,c.ope_name,c.ope_categoriaproductoId ,cuota=0,id=newid(),
 f.Ope_name,c.ope_name 
  from ope_figura f
cross join ope_categoriaproducto c 
 where convert(varchar(50),c.ope_categoriaproductoId)+CONVERT(varchar(50),f.Ope_figuraId )
 not in (select convert(varchar(50),d.ope_categoria)+CONVERT(varchar(50), d.ope_supervisor ) from ope_cuotaventa cv 
 join ope_cuotaventadet d
 on d.ope_cuotaventaid =cv.ope_cuotaventaId
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'

  )
 and f.Ope_cedisid ='9D641606-EE5B-E011-AF04-00505697471A'

I have read fetch does not support cross join then could work with this?

 join ope_categoriaproducto c on 1=1

then I have

select d.ope_supervisorName,d.ope_categoriaName ,d.ope_categoria,d.ope_cuota,d.ope_cuotaventadetId  
 ,f.Ope_name,c.ope_name
 from ope_cuotaventadet d
 join ope_figura f on d.Ope_supervisor=f.ope_figuraid
 join ope_categoriaproducto c on d.ope_categoria=c.ope_categoriaproductoid
 join ope_cuotaventa cv on d.ope_cuotaventaid =cv.ope_cuotaventaId 
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'
 union
 select f.Ope_name,c.ope_name,c.ope_categoriaproductoId ,cuota=0,id=newid(),
 f.Ope_name,c.ope_name 
  from ope_figura f
 join ope_categoriaproducto c on 1=1
 where convert(varchar(50),c.ope_categoriaproductoId)+CONVERT(varchar(50),f.Ope_figuraId )
 not in (select convert(varchar(50),d.ope_categoria)+CONVERT(varchar(50), d.ope_supervisor ) from ope_cuotaventa cv 
 join ope_cuotaventadet d
 on d.ope_cuotaventaid =cv.ope_cuotaventaId
 where cv.ope_cuotaventaId ='BEC262FA-23D2-E311-A801-00505697722F'

  )
 and f.Ope_cedisid ='9D641606-EE5B-E011-AF04-00505697471A'

I have this fetch until now, I dont know how to add the Union part and how to do the not in with a subquery

function querydet(Id) {
    var fetchXml = "";
    fetchXml += "   <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXml += "       <entity name='ope_cuotaventadet'>";
    fetchXml += "           <attribute name='ope_supervisorName'/>";
    fetchXml += "           <attribute name='ope_categoriaName'/>";
    fetchXml += "           <attribute name='ope_categoria'/>";
    fetchXml += "           <attribute name='ope_supervisor'/>";
    fetchXml += "           <attribute name='ope_cuota'/>";
    fetchXml += "           <attribute name='ope_cuotaventadetId'/>";
    fetchXml += "           <link-entity name='ope_cuotaventa' from='ope_cuotaventaId' to='ope_cuotaventaId' visible='false' link-type='outer'>";
    fetchXml += "               <attribute name='ope_cuotaventaId' />";
    fetchXml += "           <filter type='and'>";
    fetchXml += "               <condition attribute='ope_cuotaventaId' operator='eq' value='" + Id+ "'/>";
    fetchXml += "           </filter>";
    fetchXml += "           </link-entity>";
    fetchXml += "           <link-entity name='ope_figura' from='ope_figuraid' to='ope_supervisor' visible='false' link-type='outer'>";
    fetchXml += "               <attribute name='ope_name' />";
    fetchXml += "               <order attribute='ope_name' descending='false' />";
    fetchXml += "           </link-entity>";
    fetchXml += "           <link-entity name='ope_categoriaproducto' from='ope_categoriaproductoId' to='ope_categoria' visible='false' link-type='outer'>";
    fetchXml += "               <attribute name='ope_name' />";
    fetchXml += "               <order attribute='ope_name' descending='false' />";
    fetchXml += "           </link-entity>";
    fetchXml += "       </entity>";
    fetchXml += "   </fetch>";


    attribute = new Array();

    attribute[0] = new Object();
    attribute[0].ID = "ope_figuraid";
    attribute[0].Name = "ope_figuraid.ope_name";

    attribute[1] = new Object();
    attribute[1].ID = "ope_categoriaproductoid";
    attribute[1].Name = "ope_categoriaproductoid.ope_name";

    attribute[2] = new Object();
    attribute[2].Name = "ope_cuota";


 //    attribute[0].ID = "ope_inventableid";
 //    attribute[0].Name = "ope_inventableid.ope_name";

 //    attribute[1] = new Object();
 //    attribute[1].Name = "ope_codope";

 //    attribute[2] = new Object();
 //    attribute[2].Name = "ope_cantidad";

 //    attribute[3] = new Object();
 //    attribute[3].ID = "ope_ribe_prodcedisdetid";

 //    attribute[4] = new Object();
 //    attribute[4].Name = "ope_unidadid.ope_name"

    result = new Array();
    result = fetchWS(fetchXml, attribute);
    return result;
}

1 Answer 1

1

I use fetchxml all the time, but first I write QueryExpression which is easy to write and then

convert QueryExpressiontoFetchXml.

https://community.dynamics.com/crm/b/mileyja/archive/2011/07/07/convert-queryexpression-to-fetchxml-using-net-or-jscript-with-the-microsoft-dynamics-crm-2011-sdk.aspx

Also you can use Online Sql to FetchXml Converter:

http://www.sql2fetchxml.com/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.