Skip to content

Tag: sql

Count ROW type item Athena / Presto

I have an Athena query like this and the result is I would like to count the number of records per day per devices to have a result like this EDIT My dataset is actually like this Here the expected results would be : Answer You can cast your json to map and count number of keys: Output: device_id date

How can i store results of a query in an array?

I have to store last 5 employees_id’s from employees table into an array. I made the query correct and i have the array, but i can’t understand the syntax to store the results in that array. Here’s my code and here’s my query How can i store the results from the query in the array? As …

How to query cumulative sum period by month

I have an issue with table source like this: Available month in table is only as is from column month, no April, May, June etc. I need to create a view to create “Year to Date” table as shown here: I need to view all months from Year 2020 January – December with value based on table source. …

Search by best values for every minute

The problem I’m having is that I have a table of data that has a new row added every second (imagine the structure {id, timestamp(datetime), value}). I would like to do a single query for MSSQL to go through the table and output only the number of objects that have the top 2 values asc for each minute (…

Can “value in list or list is empty” be written shorter?

Given this SQL: Is there a way to write the “in list or list is empty” part shorter? Preferably in a way that contains the list only once (see the Don’t_repeat_yourself principle ) I’m interested for Oracle SQL or PL/SQL, but other information is also welcome. As requested, a MRE that …

T-SQL Logic (Where exists one column but not the other)

I need a bit of logical help in writing a query. Here is the set up. I need a query that returns all records from main_table where the id of main_table matches the id of sub_table, but seq of main_table does not match seq of sub_table. Expected result is id seq A1 3 A2 2 A2 3 My attempt What