Can I replace null values in returned rows in QuestDB? - analytics

Some aggregate queries are returning null values, is there a way to handle this?
select time, avg(temp_f) from sensor_data sample by 1h
If some records are missing, can the aggregate be set to something other than null?

If your dataset has gaps of time and is missing entire records for a certain duration, you can use FILL(). You can choose a fill strategy, such as LINEAR for interpolation, PREV for filling with the previous value, or you can specify constants. This will include new rows in the returned response where there were gaps:
SELECT time, avg(temp_f)
FROM sensor_data
SAMPLE BY 1h FILL(50);
If your dataset has records for a certain period, but a sensor was sending null instead of a value, you can use coalesce() to specify how null should be handled. No new rows are returned and a default is set for null values:
SELECT time, coalesce(avg(temp_f), 50)
FROM sensor_data
SAMPLE BY 1h;
For more information, see the FILL keyword documentation and for coalesce(), see the conditional functions documentation pages.

Related

How to filter records between timestamps from information_schema.warehouse_load_history()

I want to filter the records between timestamps from information_schema.warehouse_load_history() somehow below query is returning the empty result.
Query
select date_part(epoch_millisecond, convert_timezone('UTC', END_TIME)), WAREHOUSE_NAME, AVG_RUNNING, AVG_QUEUED_LOAD, AVG_QUEUED_PROVISIONING, AVG_BLOCKED from table(information_schema.warehouse_load_history()) where date_part(epoch_millisecond, convert_timezone('UTC', END_TIME)) >= 1668081337659 and date_part(epoch_millisecond, convert_timezone('UTC', END_TIME)) <= 1668083015000
The important point here is, the filters in the WHERE clause will be applied after the warehouse_load_history table function returns a result set. This rule is valid for any information schema table functions (ie query_history).
The function accepts DATE_RANGE_START, DATE_RANGE_END and WAREHOUSE_NAME parameters.
If an end date is not specified, then CURRENT_DATE is used as the end of the range.
If a start date is not specified, then the range starts 10 minutes prior to the start of DATE_RANGE_END
So your query only returns the last 10 minutes of data for all warehouses. Your WHERE filter is applied to this returning data.
In short, you should use the filters of the function first (as I said, it's the same for all information schema functions), and then you should use the WHERE clause for additional filters.
You might be using that wrong, the dates are a part of the date function itself, no need to add a where clause outside of the table function itself!
For reference: https://docs.snowflake.com/en/sql-reference/functions/warehouse_metering_history.html
Code from ref:
select *
from table(information_schema.warehouse_metering_history('2017-10-23', '2017-10-23', 'testingwh'));

Get Count of Shared DataSet when defined with a Parameter

I commonly display the number of rows of my datasets in SSRS e.g.
=CountRows("MyDataSet")
However this doesn't work when the dataset is a shared dataset with a parameter.
=CountRows("MySharedDatasetWithParameter")
Instead it throws an error:
The Value expression for the textrun 'Textbox25.Paragraphs[0].TextRuns[0]' contains an error: (processing): (null != aggregateObj)
How can I get the number of rows in this case?
The dataset "MySharedDatasetWithParameter" does work in normal circumstances, because I am using it to provide the available values to another parameter.
Example of shared dataset with parameter
select [Name], [Value]
from dbo.MyList
where MasterList = #MasterList
A workaround taken from this answer (Its not a duplicate question else I would flag it as such) is to create a hidden, multi-valued, parameter e.g. MyHiddenDataSetValues which stores the values from "MySharedDatasetWithParameter" and then
=Parameters!MyHiddenDataSetValues.Count
gives the number of rows.
Rather clunky, so still hoping for a way to use CountRows.

TSQL + MDX using OPENQUERY() - column names do not exist

I have a view against a OPENQUERY() which gets data from a SSAS cube.
MDX query looks like this:
WITH MEMBER [Measures].[Measure1] AS
(--calculation)
SELECT
{[Measures].[Measure1]}
ON 0,
NON EMPTY ([Dim1].[Dim_key].[Dim_key], [Dim2].[Dim_key].[Dim_key])
ON 1
FROM [Cube]
WHERE ([Dim3].[Hierarchy].[Level].[Member])
My problem is that when the WHERE filter results in 0 rows the view does not work, with error:
Invalid column name '[Dim1].[Dim_key].[Dim_key].[MEMBER_CAPTION]'.
Since its using the column name to have a GROUP BY
How can I force it return at least one row? Or always return the column names?
I cannot remove the NON EMPTY since whole set takes about 1 min to load.
So far I've tried these solutions:
MDX - Always return at least one row even if no data is available
Force mdx query to return column names
but it seems like it does not work since I have a where condition on another dimension.
Managed to figure it out. Added this measure which essentially replicates Dim1 members returned on the rows:
WITH MEMBER [Measures].[Measure1] AS
(--calculation)
MEMBER [Measures].[Dim_Key] AS
[Dim1].[Dim_key].CurrentMember.Member_Key
SELECT
{[Measures].[Measure1]
,[Measures].[Dim_Key]}
ON 0,
NON EMPTY ([Dim1].[Dim_key].[Dim_key], [Dim2].[Dim_key].[Dim_key])
ON 1
FROM [Cube]
WHERE ([Dim3].[Hierarchy].[Level].[Member])
So even if there are no rows I get the new measure back as one of the columns. If there are rows I get two additional columns (which have row tags) but I just don't use them in the view

I want to generate multiple NEXT VALUE for the sequence object I have created in the single select

We have a Sequence object created in our database, we are trying to pull multiple NEXT VALUEs from the single select statement, but it is not working as such. Here is the below query I am trying.
Select
1 as PracticeId,
NAME_1 as LocationN1Text,
NAME_2 as LocationN2Text,
federal_tax_id as FTaxId,
NULL as NameAbb,
NEXT VALUE for UPS.Seq_Communication AS CommunicationId1,
NEXT VALUE for UPS.Seq_Communication AS CommunicationId2,
NEXT VALUE for UPS.Seq_Communication AS CommunicationId3,
FROM
[Staging_ETR] A
But the above query is getting the same sequence number for CommunicationId1, CommunicationId2 and communicationId3. But we need different sequential numbers for them. Can anyone help in this? How can I generate distinct sequence numbers in the same select query out of same Sequence object?
You are querying the same sequence several times within the same row.
It is documented that:
When the NEXT VALUE FOR function is used in a query or default constraint, if the same sequence object is used more than once, or if the same sequence object is used both in the statement supplying the values, and in a default constraint being executed, the same value will be returned for all columns referencing the same sequence within a row in the result set.
To generate distinct numbers, you need to query each number on its own row.
If you want them in three columns, you can then apply the pivot clause.
You cannot just move NEXT VALUE FOR to a subquery, a CTE or an outer apply to trick it into generating distinct values. It is not supported there.

Default values in Column of type variable length array Postgresql

My question is how can I set the default values for a certain index in variable length array column. I am trying to set the value of one of the positions to default to string 'false'.
Reason being is I want to be able to have a where clause on a query to filter items where the position value is 'true' (I am setting specific ones to true manually) but without having the rest of my data default to false on that value my query is not fetching any data.
If this is confusing let me know and I will try to clear it up..Thanks in advance
Generally, a column default is for the whole column - for the whole array in your case.
You would have to write a trigger for that. But you may not need any of this. I quote your question:
... but without having the rest of my data default to false on that value
my query is not fetching any data.
You can probably avoid this if you query with:
(arr[5] = 'true') IS NOT TRUE
(which includes all cases there the flag is anything but 'true', even NULL) instead of:
arr[5] &lt&gt 'true'
And if you need this query fast you can support it with a partial index:
CREATE INDEX tbl_special_idx ON tbl (col1) -- potentially more columns
WHERE (arr[5] = 'true') IS NOT TRUE;
The important part is the WHERE clause. You can use columns in place of col1 that might cover the whole query (especially in v9.2) or that have additional conditions in your query ...

Resources