this is the query how can i write a expression rule for this and the result will be like below
Discussion posts and replies are publicly visible
You can use query entity function and provide the required filters using query filter. In case if you want to use joins, then you can use views on DB and use that view for query entity.