Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Association on CDS Views reading data from different views in a XOR meaning

matthias_fischer
Explorer
0 Kudos

I have a view ViewA with 3 colums: ID, ID_B, ID_C

  • Either ID_B has a value or ID_C

I have 2 further views B and C with fields:

  • ViewB: ID_B, Value
  • ViewC: ID_C, Value

Now I want to get a new view ViewD with fields: ID, ID_B, ID_C, Value joining the ViewA together with ViewB and ViewC in the meaning that

  • if ID_B is filled, then get the value from ViewB
  • if ID_C is filled, then get the value from ViewC

Any ideas how the Join/Associations-condition has to lool like?

Example Data:
ViewA
ID	ID_B	ID_C
01	B1
02	B2
03		C1
04		C2

ViewB
ID_B	Value
B1	TextB1
B2	TextB2	

ViewC
ID_C	Value
C1	TextC1
C2	TextC2	

Expected result for ViewD:
ID	ID_B	ID_C	Value
01	B1		ValueB1
02	B2		ValueB2
03		C1	ValueC1
04		C2	ValueC2
3 REPLIES 3

pfefferf
Active Contributor
0 Kudos

HANA CDS or ABAP CDS?

matthias_fischer
Explorer
0 Kudos

ABAP CDS

I've to add some more text, otherwise my answer cannot be submitted...

pfefferf
Active Contributor
0 Kudos

Hm, I think you can combine associations to ViewB and ViewC with a coalesce. Something like that (not syntax checked 🙂 😞

define view ViewD
  as select from ViewA 
  association [0..1] to ViewB as _ViewB on $projection.ID_B = _ViewB.ID_B
  association [0..1] to ViewC as _ViewC on $projection.ID_C = _ViewC.ID_C
{
  ID,
  ID_B,
  ID_C,
  coalesce(_ViewB.Value, _ViewC.Value) as Value
}