Hi,<br><br>   I'm trying to implement kind of SELECT COUNT(<a href="http://B.ID">B.ID</a>) C, A.* FROM A, B WHERE <a href="http://A.ID">A.ID</a> = B.F_ID GROUP BY <a href="http://A.ID">A.ID</a> HAVING C > 0<br><br>So I need to make join with count on details. The thing is, I want also to involve sorting and limiting with is but it seams qlc cursor don't like my<br>
approach. <b><span style="font-family: courier new,monospace;">as_with_bs() </span></b><span style="font-family: courier new,monospace;"><span style="font-family: arial,helvetica,sans-serif;">works fine but</span></span><b><span style="font-family: courier new,monospace;"> </span></b><b><span style="font-family: courier new,monospace;">top_as_with_some_bs(10) </span></b><span style="font-family: arial,helvetica,sans-serif;">returns<b>:</b></span><b><span style="font-family: courier new,monospace;"><br>
<br>{aborted,{badarg,[{ets,first,[106291236]},<br>                  {mnesia_tm,arrange,3},<br>                  {mnesia_tm,t_commit,1},<br>                  {mnesia_tm,apply_fun,3},<br>                  {mnesia_tm,execute_transaction,5},<br>
                  {erl_eval,do_apply,5},<br>                  {shell,exprs,6},<br>                  {shell,eval_exprs,6}]}}</span></b><br><br>QLC with nested QLC queries works fine as long not used with cursors.<br>Could anyone help me with it?<br>
<br>Here is what I wrote: (<a href="http://gist.github.com/109936">http://gist.github.com/109936</a>)<br clear="all"><br><b><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">bs_by_a_id(A_id) -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">  find(qlc:q([ B || B=#b{f_id=F_id} <- mnesia:table(b), F_id == A_id])).</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">as_with_bs() -></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  find(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    qlc:q([ {A,bs_by_a_id(A#<a href="http://a.id">a.id</a>)} || A <- mnesia:table(a)])</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  ).</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">top_as_with_some_bs(Limit) -></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  top(</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    qlc:q([ {A,bs_by_a_id(A#<a href="http://a.id">a.id</a>)} || A <- mnesia:table(a)]),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    Limit, </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    fun(A1,A2) -> A1 < A2  end</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  ). </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">% --- utils</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">find(Q) -></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  F = fun() -> qlc:e(Q) end,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">  transaction(F).</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">% --- it returns top Limit results from query Q ordered by Order sort function </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">top(Q, Limit, Order) -></span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  {atomic, Res} = mnesia:transaction(fun() -></span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    OQ = qlc:sort(Q, [{order,Order}]),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    QC = qlc:cursor(OQ),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    Res = qlc:next_answers(QC, Limit),</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    qlc:delete_cursor(QC),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    Res</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  end),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">  Res.</span></b><br><br>Regards,<br>Daniel<br>