Tuesday 7 January 2014

Complex Dynamic Query with text file in ax 2012 through X++

private void createQuery()

{
    Query                       query = new Query();
    QueryBuildDataSource        qbdsCUstTable;
    QueryBuildDataSource        qbdsAccType;
    QueryBuildDataSource        qbdsConRes;
    QueryBuildDataSource        qbdsConnection;
    QueryBuildDataSource        qbdsConStatus;
    QueryBuildDataSource        qbdsDevice;
    QueryBuildDataSource        qbdsConfigHist;
    QueryBuildDataSource        qbdsDeviceStatus;
    QueryBuildDataSource        qbdsFreqHist;
    QueryRun                    queryRn;
    Name                        tmpName;
    FcsDateTime                 tmpDateTime = FcsDateTimeAPI::dateNow();
    McsConfigurationHistory     configHistLocal;
    FcsDateTime                 startDateLocal,endDateLocal;
    str                         seprator = McsParameters::find().FedSBMDownloadSeperator;
    McsFedOMDivision            division;
    McsFedOMSubDivision         subDivision;
    McsFedOMSection             section;
    McsEmCalcCalculatedConsumption  calCOnsumption;
    McsConnectionMember         conMember;

    ;

    qbdsCUstTable = query.addDataSource(tableNum(CustTable));
    //.............added by aslam
    if(DivisionId)
    {
          qbdsCUstTable.addRange(fieldNum(CustTable,FedDivisionId)).value(DivisionId);
    }
    if(SubDivId)
    {
          qbdsCUstTable.addRange(fieldNum(CustTable,FedSubDivId)).value(SubDivId);
    }
    if(SectionId)
    {
          qbdsCUstTable.addRange(fieldNum(CustTable,FedSectionId)).value(SectionId);
    }
    if(cycleCode)
    {
          qbdsCUstTable.addRange(fieldNum(CustTable,FedCycleCode)).value(cycleCode);
    }
    //...............end by aslam
    qbdsAccType = qbdsCUstTable.addDataSource(tableNum(McsAccountTypeHistory));
    qbdsAccType.addLink(fieldNum(custTable,AccountNum),fieldNum(McsAccountTypeHistory,CustomerId));
   // qbdsAccType.joinMode(JoinMode::InnerJoin);
    qbdsAccType.addRange(fieldNum(McsAccountTypeHistory,AccountType)).value(queryValue(McsGlobal::getDefaultAccountTypeMainRelation()));
    qbdsAccType.addRange(fieldNum(McsAccountTypeHistory,ValidUntilDate)).value(queryValue(FcsDateTimeAPI::DateNull()));

    qbdsConRes = qbdsAccType.addDataSource(tableNum(McsConnectionResponsible));
    qbdsConRes.addLink(fieldNum(McsAccountTypeHistory,Id),fieldNum(McsConnectionResponsible,AccountTypeHistoryId));
    qbdsConRes.addRange(fieldNum(McsConnectionResponsible,ValidUntilDate)).value(queryValue(FcsDateTimeAPI::DateNull()));
    //qbdsConRes.joinMode(JoinMode::InnerJoin);

    qbdsConnection = qbdsConRes.addDataSource(tableNum(McsConnection));
    qbdsConnection.addLink(fieldNum(McsConnectionResponsible,ConnectionId),fieldNum(McsConnection,Id));
    //qbdsConnection.addOrderByField(fieldNum(McsConnection,Id),SortOrder::Ascending);
    //qbdsConnection.joinMode(JoinMode::InnerJoin);

    qbdsConStatus = qbdsConnection.addDataSource(tableNum(McsConnectionStatusHistory));
    qbdsConStatus.addLink(fieldNum(McsConnection,Id),fieldNum(McsConnectionStatusHistory,ConnectionId));
    qbdsConStatus.addRange(fieldNum(McsConnectionStatusHistory,ConnectionStatus)).value(queryValue(McsStatuses::Active));
    qbdsConStatus.addRange(fieldNum(McsConnectionStatusHistory,ValidUntilDate)).value(queryValue(FcsDateTimeAPI::DateNull()));
    //qbdsConStatus.joinMode(JoinMode::InnerJoin);

    /*qbdsFreqHist = qbdsConnection.addDataSource(tableNum(McsCmBilBilFrequencyHistory));
    qbdsFreqHist.addLink(fieldNum(McsConnection,Id),fieldNum(McsCmBilBilFrequencyHistory,Connectionid));
    qbdsFreqHist.addRange(fieldNum(McsCmBilBilFrequencyHistory,ValidUntilDate)).value(queryValue(FcsDateTimeAPI::DateNull()));
    qbdsFreqHist.addRange(fieldNum(McsCmBilBilFrequencyHistory,DataAreaId)).value(queryValue('((LastBillingDate < EndDate) || (EndDate == 0))'));
    //qbdsFreqHist.addRange(fieldNum(McsCmBilBilFrequencyHistory,NextBillingDate)).value(queryValue(FcsDateTimeAPI::addDayLocal(this.parmEndDate(),1)));
    qbdsFreqHist.addSortField(fieldNum(McsCmBilBilFrequencyHistory,StartDate),SortOrder::Ascending);
    qbdsFreqHist.joinMode(JoinMode::InnerJoin);*/

    qbdsConfigHist = qbdsConStatus.addDataSource(tableNum(McsConfigurationHistory));
    qbdsConfigHist.addLink(fieldNum(McsConnectionStatusHistory,ConnectionId),fieldNum(McsConfigurationHistory,ConnectionId));
    qbdsConfigHist.addRange(fieldNum(McsConfigurationHistory,ValidUntilDate)).value(queryValue(FcsDateTimeAPI::DateNull()));
    //qbdsConfigHist.joinMode(JoinMode::InnerJoin);

 qbdsDevice = qbdsConfigHist.addDataSource(tableNum(McsMeterDevice));
    qbdsDevice.addLink(fieldNum(McsConfigurationHistory,MeterDeviceId),fieldNum(McsMeterDevice,Id));
    qbdsDevice.addRange(fieldNum(McsMeterDevice,FedPhase)).value(queryValue(McsFedPhases::ThreePhase));

    //qbdsDevice.joinMode(JoinMode::InnerJoin);

    qbdsDeviceStatus = qbdsDevice.addDataSource(tableNum(McsMeterDeviceStatusHistory));
    qbdsDeviceStatus.addLink(fieldNum(McsMeterDevice,Id),fieldNum(McsMeterDeviceStatusHistory,   MeterDeviceId));
    qbdsDeviceStatus.addRange(fieldNum(McsMeterDeviceStatusHistory,MeterDeviceStatus)).value(queryValue(McsStatuses::Active));
    qbdsDeviceStatus.addRange(fieldNum(McsMeterDeviceStatusHistory,ValidUntilDate)).value(queryValue(FcsDateTimeAPI::DateNull()));

    //qbdsDeviceStatus.joinMode(JoinMode::InnerJoin);




    queryRn = new QueryRun(query);

    totalRecords = SysQuery::countLoops(queryRn);
    progress.setCaption("Downlaod Customer");

    progress.setAnimation(#aviUpdate);

    progress.setTotal(totalRecords);


    progress.update(true);
    totalRecords = 0;
    while (queryRn.next())
    {
        //custTable.clear();
        //meterDevice.clear();
        //Connection.clear();
        //configHistLocal.clear();

        custTable = queryRn.get(tableNum(CustTable));
        meterDevice = queryRn.get(tableNum(McsMeterDevice));
        Connection  = queryRn.get(tableNum(McsConnection));
        configHistLocal = queryRn.get(tableNum(McsConfigurationHistory));
        progress.setText("Exporting Customer " + custTable.name());
        progress.update(true);
        //Consumer No. ---1
        tb.setText(tb.getText() + strFmt("%1%2",custTable.McsExternalID1,seprator));
        //Old K Number----2
        tb.setText(tb.getText() + strfmt("%1%2",custTable.McsExternalId3,seprator));
        //Tariff Code ----3
        tb.setText(tb.getText() + strfmt("%1%2",custTable.CustGroup,seprator));
        //Meter Serial Number---4
        tb.setText(tb.getText() + strfmt("%1%2",meterDevice.MeterDeviceSerialNumber,seprator));
        //Consumer Name---5
        tb.setText(tb.getText() + strfmt("%1%2",subStr(custTable.name(),1,20),seprator));
        //Address---6
        tb.setText(tb.getText() + strfmt("%1%2",subStr(custTable.address(),1,20),seprator));
        //Cycle Display Code----7
        tb.setText(tb.getText() + strfmt("%1%2",custTable.FedCycleCode,seprator));
        //Route Nbr---8
        tb.setText(tb.getText() + strfmt("%1%2",custTable.FedRouteNbr,seprator));

        //Division---9
        division.clear();
        select DisplayCode from division
            where division.DivisionId == custTable.FedDivisionId;

        tb.setText(tb.getText() + strfmt("%1%2",division.DisplayCode,seprator));

        //SubDivision---10
        subDivision.clear();
        select DisplayCode from subDivision
            where subDivision.SubDivId == custTable.FedSubDivId;
        tb.setText(tb.getText() + strfmt("%1%2",subDivision.DisplayCode,seprator));

        //section---11
        section.clear();
        select DisplayCode from section
            where section.SectionId == custTable.FedSectionId;
        tb.setText(tb.getText() + strfmt("%1%2",section.DisplayCode,seprator));

        //BillMonth---12
        select EndDate from calCOnsumption
        order by EndDate desc
        exists join conMember
            where conMember.Id == calCOnsumption.ConnectionMemberId
            && calCOnsumption.ValidUntilDate == 0
            && conMember.ValidUntilDate == 0
            && conMember.ConnectionId == Connection.Id
            && conMember.ConnectionMemberType == McsConnectionMemberTypes::mmrcons;

        tb.setText(tb.getText() + strfmt("%1",date2str(FcsDateTimeAPI::convertToDateLocal(    calCOnsumption.EndDate),123,0,0,3,3,2)));
       tb.setText(tb.getText() + strfmt("\n"));
        progress.incCount();
        progress.update(true);
    }

}

No comments:

Post a Comment

SQL/SSRS Interview questions I thought of blogging some SQL/SSRS interview questions.

Below are some. I will add more, when I complete the compilation 1. What is OLTP(Online Transaction Processing)? OLTP stands ...