Skip to content
Advertisement

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 index scan is executed 46257 times.

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,C. For a single 10 minute

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 response

SeedTable.SomeColumn +1 in a recursive CTE not working as expected

I’m trying this query in Redshift: but I keep getting this error: (500310) Invalid operation: column “level” does not exist in child, parent; 1 statement failed. What am I doing wrong? According to the documentation I found here, Redshift accepts recursive and this is the way to go about it: https://docs.aws.amazon.com/redshift/latest/dg/r_WITH_clause.html#r_WITH_clause-recursive-cte Answer I believe that for recursive cte, the cte

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 Use a case

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 format than the reference.

Advertisement