# AQL teaser 2 for implementers **Category:** [AQL](https://discourse.openehr.org/c/aql/43) **Created:** 2020-02-28 15:57 UTC **Views:** 377 **Replies:** 10 **URL:** https://discourse.openehr.org/t/aql-teaser-2-for-implementers/388 --- ## Post #1 by @Seref Dear implementers: @bna @matijap @ian.mcnicoll Given the composition on the left, and the AQL query after that, what should the result set for the select clause on the right be? Same as before: think on it whenever you have some free time ![image|690x275](upload://qyV4Vk0RsO0E65CUHZRbdWsODKa.png) SELECT c,s,i_1,i_2 FROM COMPOSITION C c CONTAINS SECTION s CONTAINS (INSTRUCTION i_1 AND INSTRUCTION i_2) (i) on the leftmost tree is composition|section|instruction(x3) instances, i.e. a single composition instance. --- ## Post #2 by @matijap If I argued collapsing results into as few rows as possible (while still having a characteristic of a cartesian product), I will with even more confidence proclaim that is the correct way in case of conjunction. This is exactly like SQL inner join with a condition that i_1 != i_2. --- ## Post #3 by @Seref sorry @matijap I understand your point but can't understand how you suggest that point could be applied to this example. do you mean you'd collapse rows that are permutations of the same set ([i1,i2], [i2,i1]) ? --- ## Post #4 by @matijap Yes, and I'll edit my previous message comparing this to inner join: there is a subtle difference that the instructions are never the exact same object; there need to be two instructions. --- ## Post #5 by @matijap Oh, I'm sorry, I didn't read the question clearly enough; I see you are asking about a result on a composition with 3 instructions, and in this case you'll get 6 rows (3x3=9, minus the 3 cases where both variables bind to the same object). --- ## Post #6 by @Seref [quote="matijap, post:5, topic:388"] where both variables bind to the same object [/quote] My explanation for removing those is "an object cannot be in two columns of a row at the same time", since merely having two variables implies having two different instances of data :) Glad to agree on yet another one. --- ## Post #7 by @matijap [quote="Seref, post:6, topic:388"] My explanation for removing those is “an object cannot be in two columns of a row at the same time”, since merely having two variables implies having two different instances of data [/quote] We agree on what having two variables implies. You could, however, use the same variable multiple times in the projection in a way that would cause having "an object in two rows". Or indeed you could have different variables between which there is containment, and it's actually what bothers Pablo, but I say you can have the same field in the projection as many times you like. :) --- ## Post #8 by @Seref [quote="matijap, post:7, topic:388"] in a way that would cause having “an object in two rows” [/quote] absolutely. My statement is based on "a row". Completely agree with the rest of your statement --- ## Post #9 by @matijap [quote="matijap, post:7, topic:388"] having “an object in two rows” [/quote] I meant columns, of course, but I think you understood that. --- ## Post #10 by @Seref no , I did not read it carefully it appears :) but I think you're referring to SELECT reusing an alias/variable from alias multiple times, in which case, I'd certainly agree. --- ## Post #11 by @bna This is the permutation problem - again :-) The result-set will have 3 x 3 = 9 rows. ![image|172x272](upload://bDrc18voL9qNkQNPz2wvq4hKqlj.png) The executed AQL was: SELECT c/name/value as C1, s/name/value as S1, i_1/name/value as I1, i_2/name/value as I2 FROM COMPOSITION c CONTAINS SECTION s CONTAINS ( INSTRUCTION i_1 AND INSTRUCTION i_2 ) --- **Canonical:** https://discourse.openehr.org/t/aql-teaser-2-for-implementers/388 **Original content:** https://discourse.openehr.org/t/aql-teaser-2-for-implementers/388