Query with Random results

Is there a way to query data and return and selected amount of results randomly on server side?

So I query a table with 100 locations and I return 5 random locations?

You can either:

  1. On serverside add a query, add a repeat step and for repeat expression select query>format>randomize>top 5. Then on the clientside create a repeat region out of the repeat step (not from the query)

  2. On clientside apply randomize>top 5 filters to the query.

4 Likes

I still can’t figure this out? If I set a repeat on my server side query, I can see how to randomize that. but what do I put in repeat section of the action script?

Personally I find it much easier to do on the client side using the App Connect Panel.

1 Like

I can’t find any randomize there?

it should be on the repeat right?

Yes, select your repeat. When you select your serverconnect recordset then click on the formatter icon.

1 Like

Yours shows as array, mine is just record set?

Click on the formatter icon … the magic wand

yes brad I know that much. LOL. its just that randomize isn’t there.

I’m repeating a column. maybe i need to repeat a div or something and put the column inside that?

What do you see there? When you click on the formatter?

Hmmmm, interesting. And you are applying it to an element that has a repeat region?

yes

<div class="row" id="id_featuredProducts">
		<div class="col rounded rounded-sm">
			<div class="row">
				
				<div class="mb-3 align-self-center rounded col-xl-3 col-md-5 col-lg-3 h-auto" id="id_featured" dmx-repeat:featuredrepeat="conn_featuredProds.data.query1.data">
					<a dmx-bind:href="'product-detail.asp?product_id='+product_id"><img dmx-bind:src="/assets/images/{{product_id}}/{{mainphoto}}" class="img-fluid rounded"></a>
					<p class="text-center bg-light mt-2 pt-2 pb-2 pl-2 pr-2 h-auto"><b>{{short_description}}</b><br>{{sku}}<br><%If Session("HitekSecurityId") Then %>{{price.toNumber().formatCurrency("$", ".", ",", "2")}}<% else %> [ <a
							href="my-account.asp">LOGIN TO SEE PRICE</a> ]<% end if %></p>
				</div>
			</div>
		</div>
	</div>

Interesting, in your value properties where it says Type --> text, try choosing Array

weird I got it to work. I just created a regular repeat and added randomize to that. which added .randomize(), I just copied and pasted that to the end of my existing repeat and that did the trick.

1 Like

I know this is quite an old thread, but I was looking for different ideas about returning random results. I decided to follow @Teodor’s suggestion (getting the random results server-side), to avoid returning results I didn’t need. @baub raised a good question in relation to this approach:

It seems you need to include some step in the repeat, but it can simply be a comment (even a disabled one).

It’s not possible to save an API with a repeat unless there’s at least one step in the repeat (albeit a redundant one). Perhaps it should be possible. It’s hardly worthy of a feature request - just a thought.

You can simply put this in a setvalue step, without a repeat on the server side:

Screenshot 2021-04-13 at 11.48.02

2 Likes

Thanks - even simpler.