Skip to content

Tag: sql

SQL Select random rows partitioned by a column

I have a dataset looks like this and i need a query which returns 2 random values from where country in (‘a’, ‘c’): Answer This should work: Replace rand() with random() if you’re using Postgres or newid() in SQL Server.

Why is this Index Scan so slow?

From what I can see, the index is not entirely loaded up in memory, and that’s what causing it to be slow? Am I correct? obs: I changed the “Output” section a little bit for privacy concerns Answer The index scan is fast (1.774 milliseconds on average). The execution is so slow because the i…

Get distinct values within time chunks

I have a table with different species and their occurences (timestamp) from field survey: Now I want to count them, not in total but in chunks of 10 minutes each, where only the first occurence is counted. That means between 12:31 and 12:40 there are species A,B,C. Between 12:41 and 12:50 there are species B,…

Mongo Group By query

I have data stored in a Mongo collection that is structured like this: I wish to construct a query that would let me sum up each count in each entry and group by requestingSupplier. For example, I would like to answer the question, for the month of January ’22, what was the sum of each entity and get a …

SQL check if Int is null select NVarchar

I want to check if an INT is null, and if so select a different name for the value. But I keep getting a Conversion failed when converting the varchar value ‘NAME’ to data type int I want something like.. Simply, if null select the second name, otherwise if it has a value, select the first Answer …

How to check TIMESTAMP format in SQL?

What’s the best way to check if the VARIANT TYPE with JSONs with value similar to TIMESTAMP_NTZ has correct format? For example, this is the correct format that I would like to have but there are times when it looks different in database, like this I would like to detect records which are in a different…