cancel
Showing results for 
Search instead for 
Did you mean: 

CAP business services : use distinct with projection vs use key with variant as select

2,660

Hi,

I am trying to write a cds projection using reference as blog-example and documentation. i am not able to write "as projection" statement with distinct keyword. (or alternatively cds variant "as select" with key defined).

The below code works fine, but i am expecting distinct values in result.

using {aatp.risk as riskSchema} from '../db/risk-schema'; 
@path:'dashboard' service DashboardService
{
entity RiskInputData as projection on riskSchema.riskInputData;
entity Plant as projection on RiskInputData { key plant}; 
entity MrpController as projection on RiskInputData { key plant, key mrpController}; 
}

Alternatively, if ii write the same with "as select" statements below, i am getting a OData error >>

using {aatp.risk as riskSchema} from '../db/risk-schema'; 
@path:'dashboard' service DashboardService
{
entity RiskInputData as projection on riskSchema.riskInputData;
entity Plant as select distinct plant from RiskInputData; 
entity MrpController as select distinct a.plant, a.mrpController from RiskInputData as a;
}
[0] srv.json:931: Error: Entity "DashboardService.MrpController" does not 
have a key: ODATA entities must have a key
[0] srv.json:970: Error: Entity "DashboardService.Plant" does not have a key: ODATA entities must have a key

Regards

Accepted Solutions (1)

Accepted Solutions (1)

With SELECT, you were on the right track, but please also specify the keys there, too.
You might also want to use the CDS-like syntax (FROM first, which is better for code completion)

using {aatp.risk as riskSchema} from '../db/risk-schema'; 
@path:'dashboard' service DashboardService
{
entity RiskInputData as select from riskSchema.riskInputData;
entity Plant as select from RiskInputData distinct { key plant}; 
entity MrpController as select from RiskInputData distinct { key plant, key mrpController}; 
}

(OK, we should probably allow the DISTINCT also directly after the SELECT…, but this is how it works now and will continue to work.)

Answers (0)