# Require Group Count in case of groupBy using QBselect

**URL:** <https://forum.servoy.com/t/require-group-count-in-case-of-groupby-using-qbselect/19737>\
**Category:** Classic Servoy\
**Created:** [July 2, 2018, 1:16pm UTC](https://forum.servoy.com/t/require-group-count-in-case-of-groupby-using-qbselect/19737 "2018-07-02T13:16:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![binkal.patel](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@binkal.patel](https://forum.servoy.com/u/binkal.patel)\
**Post date:** [July 2, 2018, 1:16pm UTC](https://forum.servoy.com/t/require-group-count-in-case-of-groupby-using-qbselect/19737/1 "2018-07-02T13:16:58Z")

</div>

Hello

Developed Custom Query at Web component level using reference of below document link

[https://wiki.servoy.com/display/DOCS/Fo … perty+type](https://wiki.servoy.com/display/DOCS/Foundset+property+type)  
[https://wiki.servoy.com/display/DOCS/QBSelect](https://wiki.servoy.com/display/DOCS/QBSelect)

**Code :**

var childFoundset = parentFoundset.duplicateFoundSet();  
var query = parentFoundset.getQuery();  
query.result.addPk();  
var pkColumns = query.result.getColumns();

query.result.clear();

for (var pkIndex = 0; pkIndex \< pkColumns.length; pkIndex++) {  
query.result.clear();  
query.result.add(pkColumns[pkIndex].min);  
query.result.add(query.columns.article\_id.count, ‘maximum\_items’)  
query.groupBy.add(groupColumn);  
query.sort.clear();  
}  
childFoundset.loadRecords(query);

Query Genereated:- select top 201 max(article\_id), count(article\_id) as maximum\_items from article group by articlestatus\_id order by articlestatus\_id asc

Error:- ERROR org.sablo.websocket.WebsocketEndpoint - Error: Wrapped java.lang.IllegalArgumentException: The query does not have the correct number of pks in the select

Please provide guild for find group count using QBselect query

---

<div class="post-metadata">

**Author:** ![sdevlin](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/sdevlin/32/4488_2.png) [@sdevlin](https://forum.servoy.com/u/sdevlin)\
**Post date:** [July 2, 2018, 7:41pm UTC](https://forum.servoy.com/t/require-group-count-in-case-of-groupby-using-qbselect/19737/2 "2018-07-02T19:41:37Z")

</div>

Hi Binkal,

It’s not clear from your post which problem exactly you are trying to solve. Maybe a better explanation about what you want to achieve?  
The code example is confusing. Why do you add and remove the columns?  
Are you just trying to find out which are the PK columns?

Can be done using [JSTable.getRowIdentifierColumnNames](https://wiki.servoy.com/display/DOCS/JSTable#JSTable-getRowIdentifierColumnNames())  
i.e.

```auto
var pks = databaseManager.getTable(foundset).getRowIdentifierColumnNames;

```

The error you get is because you are trying to load the results back into a **foundset** and it wants only the PKs for that.

You could instead use [databaseManager.getDataSetByQuery](https://wiki.servoy.com/display/DOCS/Database+Manager#DatabaseManager-getDataSetByQuery(query,max_returned_rows))(q)

Here is a webinar which shows a bit more about QueryBuilder API  
[https://www.youtube.com/watch?v=WxPqhJZtr4A](https://www.youtube.com/watch?v=WxPqhJZtr4A)

---

<div class="post-metadata">

**Author:** ![binkal.patel](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@binkal.patel](https://forum.servoy.com/u/binkal.patel)\
**Post date:** [July 3, 2018, 12:23pm UTC](https://forum.servoy.com/t/require-group-count-in-case-of-groupby-using-qbselect/19737/3 "2018-07-03T12:23:38Z")

</div>

Hi sean,

Thanks for your reply, let me elaborate more details  
I have developed code into NGClient-\> Webcomponents-\> Server-side .js as show in attached screen (code.png)  
I have developed API on DataGridServer.Js file as (getGroupFoundSet) and this JS file code as below:  
**Note:** component JS file isn’t supported databaseManager.getDataSetByQuery(q)

**My Code** :

$scope.getGroupedFoundset = function(idForFoundsets)  
{  
var parentFoundset = $scope.model.foundset.foundset;  
var query = parentFoundset.getQuery();  
var childFoundset = parentFoundset.duplicateFoundSet();  
query.result.addPk();  
var pkColumns = query.result.getColumns();  
query.result.clear();  
query.result.add(pkColumns[0].min);  
query.groupBy.add(query.columns.articletype\_id);  
query.sort.clear();  
query.sort.add(query.columns.articletype\_id.desc);

var dps = {};  
for (var idx = 0; idx \< $scope.model.columns.length; idx++) {  
dps[“dp” + idx] = $scope.model.columns[idx].dataprovider;  
}  
childFoundset.loadRecords(query);  
$scope.model.filterfoundset.push({  
foundset: {  
foundset: childFoundset,  
dataproviders: dps,  
sendSelectionViewportInitially: false  
},  
filterType:“main”,  
foundsetUUID: childFoundset,  
key: groupKeys  
});  
return childFoundset;  
}

Using this custom query I get result of group data as show in screen (result.png)

Every group have their child data as show in (GroupChildData.png)

Now, I want to extend this Custom query code (this method code $scope.getGroupedFoundset ) so will get group count along with group data, For example in viewport one more column dp3 having data as 7, 25, 1276, 27145.  
Please provide your guild line regarding,What kind of modification require for above custom query, to archive group count data along with group

 ![GroupChildData.png](https://canada1.discourse-cdn.com/flex010/uploads/servoy/original/1X/c160fd2e518afba863639a31a9b73a8c2df8f0ba.png)

 ![code.png](https://canada1.discourse-cdn.com/flex010/uploads/servoy/original/2X/a/a9fd6c7ab79fe2baccde49300aebc7b1a15f1a5e.png)

 ![Result.png](https://canada1.discourse-cdn.com/flex010/uploads/servoy/original/2X/9/9dd1d69f585eb71051aa937789fc6de138280660.png)

---

<div class="post-metadata">

**Author:** ![sdevlin](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/sdevlin/32/4488_2.png) [@sdevlin](https://forum.servoy.com/u/sdevlin)\
**Post date:** [July 3, 2018, 12:49pm UTC](https://forum.servoy.com/t/require-group-count-in-case-of-groupby-using-qbselect/19737/4 "2018-07-03T12:49:17Z")

</div>

Hi Binkal,

I see you are doing this in a component js file. I missed that.

Can you talk about the component you are building and why you need a custom component.  
(Building a grid component is no small task)

Is it not possible to create a dataset by query and convert it to a mem data source?  
Then you can bind it to existing grid components.  
What features do you miss from existing grid components? (i.e. table-view forms, servoy extra grid, ag grouping grid)

Also, we plan to announce the next version of the AG Grid component next week.
