How to filter records between timestamps from information_schema.warehouse_load_history() - snowflake-cloud-data-platform

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'));

Related

Can I replace null values in returned rows in QuestDB?

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.

how do we specify Must clause(+) with local params

if my query is using local params e.g like below
q=\field:test11&
fq=+{!frange cost=200 l=NOW/DAY-10DAYS u=NOW/DAY+1DAY incl=true incu=false}date
How to i specify the must clause ?
So adding + at the beginning of the local param syntax is the correct way?
e.g in the first query, is the leading + correct or not?
+{!frange cost=200 l=NOW/DAY-10DAYS u=NOW/DAY+1DAY incl=true incu=false}date
If not then how do we specify must clause or do we even need must clause here ?
The intent of my query is to find all the documents which have value test11 in field and also the date is within last 10 days.
The query will work as its written if you remove the +. A filter query is always used to filter the current set of returned documents, so it has to match (i.e. it'll always work logically as an AND clause to the original query).
You can probably rewrite that query to just be a range as well:
fq=start_date:[NOW/DAY-10DAYS TO NOW]

Using calculated member as parameter in MDX function

I'm new to MDX but want to keep my code as clean as possible.
I have a query that looks at todays sales and compares them against LY and LY-1 using the ParallelPeriod function. It looks something like this..
With Member [Date].[SalesCalendar].[DateToday] as [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]
SELECT
{[Date].[SalesCalendar].[DateToday],
ParallelPeriod([Date].[SalesCalendar].[Year],1,[Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]),
ParallelPeriod([Date].[SalesCalendar].[Year],2,[Date].[SalesCalendar].[DateToday]}
*
{[Measures].[Total Sales],[Measures].[Units],[Measures].[Sales Target]}
ON Columns,
[Locations].[Location PK].[Location PK]
on Rows
From MyCube
I start by defining a member that points to today's date. I want to define it once and use it throughout this query (and other queries I write), the theory being I can change it in once place and the underlying query reacts.
The problem I have is that if I try and use this calculated member within the ParallelPeriod function I get no results. With the query above I get results for the first column and the first call to ParallelPeriod (for LY) works but the second call for LY-1, which uses the declared member, fails.
I'm guessing this is down to my lack of knowledge with MDX and so I guess I am missing something fundamental. However, banging my head against the wall isn't working so I need some help!
Any ideas what I am doing wrong?
Thanks
This cannot work because when your query is evaluated [Date].[SalesCalendar].[DateToday] is not replaced with [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00].
You created a member that will give the same numeric values than [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00] in the pivot table cells.
You can try this query:
With Member [Date].[SalesCalendar].[DateToday] as [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]
Member [Measures].[name] as [Date].[SalesCalendar].CurrentMember.Name
SELECT
{[Measures].[name], [Measures].[Total Sales]} ON Columns,
{[Date].[SalesCalendar].[DateToday], [Date].[SalesCalendar].[Date].&[2012-10-26T00:00:00]} on Rows
From MyCube
The Total Sales will be the same but not [Measures].[name].

SQL Server ISDATE In Indexed View

I have a indexed view where I basically need to do this
SELECT ...
CASE
WHEN ISDATE(ColumnName) = 1 THEN CONVERT(datetime, ColumnName, 103)
ELSE NULL
END AS ViewColumn
....
Trying to create the index yields:
Cannot create index on view
'....'. The function
'isdate' yields nondeterministic results. Use a deterministic system
function, or modify the user-defined function to return deterministic
results.
MSDN says
ISDATE is deterministic only if you use it with the CONVERT function,
if the CONVERT style parameter is specified, and style is
not equal to 0, 100, 9, or 109.
here http://msdn.microsoft.com/en-us/library/ms187347.aspx.
But I don't know what that means at all. As far as I can tell, I am using it with a CONVERT function....
Any way to work around this?
It should be, if at all:
SELECT ...
CASE
WHEN ISDATE(ColumnName) = 1 THEN CONVERT(datetime, ColumnName, 103)
ELSE NULL
END
....
but, you are not using ISDATE WITH CONVERT, since there is no expression like
ISDATE(CONVERT(varchar,ColumnName,112))
without the nested convert the return value is dependend on things like language settings, hence it's nondeterministic behaviour. Without "external" knowledge, it's not possible to predict the result one is getting, based on the input alone.
Reference
What are the requirements for Indexed views?
There are several requirements that you must take into consideration when using Indexed views.
1. View definition must always return the same results from the same underlying data.
2. Views cannot use non-deterministic functions.
3. The first index on a View must be a clustered, UNIQUE index.
4. If you use Group By, you must include the new COUNT_BIG(*) in the select list.
5. View definition cannot contain the following
(A) TOP
(B) Text, ntext or image columns
(C)DISTINCT
(d)MIN, MAX, COUNT, STDEV, VARIANCE, AVG
(E)SUM on a nullable expression
(F)A derived table
(G)Rowset function
(H)Another view
(I)UNION
(J)Subqueries, outer joins, self joins
(K)Full-text predicates like CONTAIN or FREETEXT
(L)COMPUTE or COMPUTE BY
(M)Cannot include order by in view definition

How do I use a computed column specification to persist a datetime value using getdate()?

Does anyone know if there is a way to achieve the same affect as a datetime column with a default binding of getdate() with computed columns?
I have tried setting the formula to getdate() and persist to Yes but I get an error
Computed column 'InsertDateTime' in table 'Tmp_Table' cannot be persisted because the column is non-deterministic.
forget the "computed column" and make it a regular not null column, with a default to GETDATE(), or use an INSTEAD OF UPDATE/INSERT trigger to set it.
you can't make a computed column use a function that constantly returns a different value (based on the same parameter values), it must return the same value each time (based on the same parameter values). Read this: Deterministic and Nondeterministic Functions
All functions are deterministic or nondeterministic:
Deterministic functions always return the same result any time
they are called with a specific set of input values.
Nondeterministic functions may return different results each time
they are called with a specific set of input values.
Whether a function is deterministic or nondeterministic is called the
determinism of the function.
For example, the DATEADD built-in function is deterministic because it
always returns the same result for any given set of argument values
for its three parameters. GETDATE is not deterministic because it is
always invoked with the same argument, yet the value it returns
changes each time it is executed.

Resources