# Adding QueryRange to datasource table

**URL:** <https://www.dynamicsuser.net/t/adding-queryrange-to-datasource-table/52634>\
**Category:** Developer Forum\
**Created:** [May 11, 2014, 7:15am UTC](https://www.dynamicsuser.net/t/adding-queryrange-to-datasource-table/52634 "2014-05-11T07:15:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wkhaksar](https://avatars.discourse-cdn.com/v4/letter/w/e68b1a/32.png) [@wkhaksar](https://www.dynamicsuser.net/u/wkhaksar)\
**Post date:** [May 11, 2014, 7:15am UTC](https://www.dynamicsuser.net/t/adding-queryrange-to-datasource-table/52634/1 "2014-05-11T07:15:28Z")

</div>

Dears,

Hope every one doing well,

I want to retrieve all the active positions from e.g **PositionTable** , and add some ranges to it, in this table every position is linked to an organization.

I want to retrieve all positions with other organizations but only specified employee’s from organization ( **10** ) for example those employees whose **positongroup** is ( **aboveManger** ) will come from the organization ( **10** ) but from other organizations rest of them. how should I do it?

Here is my code:

Query query;

PositionId positionId;

QueryBuildDataSource qbds1,qbds2;

;

query=new query();

qbds1 = query.addDataSource(tablenum(PositionTable));

qbds2 = qbds1.addDataSource(tableNum(PositionTable));

qbds2.relations(false);

qbds2.joinMode(JoinMode::ExistsJoin);

qbds2.addLink(fieldnum(PositionTable,PositionId),fieldnum(PositionTable,ReportsToPosition));

qbds1.addRange(fieldnum(PositionTable,ValidFromDateTime)).value(QueryRange(DateNull(),SystemDateGet()));

qbds1.addRange(fieldnum(PositionTable,ValidToDateTime)).value(QueryRange(SystemDateGet(),DateNull()));

qbds2.addRange(fieldnum(PositionTable,ValidFromDateTime)).value(QueryRange(DateNull(),SystemDateGet()));

qbds2.addRange(fieldnum(PositionTable,ValidToDateTime)).value(QueryRange(SystemDateGet(),DateNull()));

**I did the scenario like bellow but it is not working:**

_qbds2.addRange(fieldnum(PositionTable,OrganizationUnitId)).value(strFmt(’((%1.%2 == %5) && (%1.%3 == %4))’,_  
_query.dataSourceTable(tableNum(PositionTable)).name(),_  
_fieldStr(PositionTable, OrganizationUnitId),_  
_fieldStr(PositionTable,EmployeeGroup),_  
_any2int(PositonGroup ::MaangerAbvoe),10));_

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![MartinDrab](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@MartinDrab](https://www.dynamicsuser.net/u/MartinDrab)\
**Post date:** [May 12, 2014, 4:01pm UTC](https://www.dynamicsuser.net/t/adding-queryrange-to-datasource-table/52634/2 "2014-05-12T16:01:30Z")

</div>

What “is not working” exactly means? Do you get any error? If not, how the constructed SQL string differs from the expected SQL string?

Can you reproduce the problem on a standard table, so we can copy your code and run it?

---

<div class="post-metadata">

**Author:** ![wkhaksar](https://avatars.discourse-cdn.com/v4/letter/w/e68b1a/32.png) [@wkhaksar](https://www.dynamicsuser.net/u/wkhaksar)\
**Post date:** [May 18, 2014, 5:30am UTC](https://www.dynamicsuser.net/t/adding-queryrange-to-datasource-table/52634/3 "2014-05-18T05:30:07Z")

</div>

**Dear Martin Dráb,**

Thanks for your reply,

I get solved the problem by using **AOT Query** as bellow:

Queering **PositionTable** for rest of the organizations without organization (10), then queering the **PositionTable** only for the organization (10) for specified ranges, later on I applied **Union All** to the results and it worked.

Once again thanks from your reply,
