report “Window query not supported” when using state_window in TDengine - tdengine

I'm using state_window syntax in TDengine database.
My SQL statement is:
select t.*
from (
select ts,
tbname as app_id,
tenant_name,
queue_code,
task_name,
sum(allocated) as allocated
from yarn
where ts > now - 6m
partition by ts,tbname,tenant_name,queue_code,task_name
) t
partition by ts, app_id, tenant_name, queue_code, task_name
state_window((CASE WHEN allocated > 100 THEN 1 ELSE 0 END));
Then I encountered an error report:
DB error: Window query not supported, since the result of subquery not
include valid timestamp column
I don't particularly understand its meaning, and how to fix the problem.

Related

Snowflake Recreate table in lab doesn't work

I am working through Snowflake workshop lab tutorial. I am up to 7.2.4/7.2.5.
The query in 7.2.4 executes fine:
set query_id = (
select query_id
from table(information_schema.query_history_by_session (result_limit=>5))
where query_text like 'update%' order by start_time limit 1
);
However the query in 7.2.5 gives an error:
create or replace table trips as (
select * from trips before (statement => $query_id)
);
The error given is:
SQL execution internal error: Processing aborted due to error 300002:1177671206; incident 8702198.
Any thoughts on what is going wrong here?

Translate SELECT DISTINCT t-sql query to DAX expression

I need to create calculate table for the report in PowerBI Desktop.
I know how to do that in t-sql but I am unable to interpret it to DAX.
So should I use t-sql and add this query using "Get Data"?
Or should I create calculate table using DAX?
Which one is more efficient?
select distinct PolicyNumber,
ReserveStatus,
case when ReserveStatus = 'Open' then 1 else 0 end as OpenStatus
from RockhillClaimsDataFeed_PBI
group by PolicyNumber,ReserveStatus
Result looks like that:
can somebody help?
This is achievable by creating a calculated table in Power BI, with similar syntax using SELECTCOLUMNS and DISTINCT.
RockhillClaimsSummary =
DISTINCT(
SELECTCOLUMNS(
RockhillClaims,
"PolicyNumber", RockhillClaims[PolicyNumber],
"ReserveStatus", RockhillClaims[ReserveStatus],
"OpenStatus", IF(RockhillClaims[ReserveStatus] = "Open", 1, 0)
)
)
Results:

SQL Server FOR/AFTER trigger

I have an issue with an assignment. I believe I have the code all worked out properly but I keep getting a syntax error saying that the column "Total" Does not exist. I was under the impression that when you used "AS" it renamed the column that you are trying to use.
Here is my code any pointers would be greatly appreciated.
Use [IST278EagleCorp13-1]
Go
Alter Trigger DPInvOrderTrigger
On InventoryPart
For insert
As
If exists ( Select Count(ReorderLevel * 4) ReorderLevel,Count(StockOnOrder + StockLevel) AS Total
From InventoryPart
Where Total > ReorderLevel)
Begin
RaisError('Inventory to low. Transaction failed.',16,1)
rollback tran
End
These are the directions for this assignment.
/* Create a FOR|AFTER trigger named xxInvOrderTrigger for updates
to the InventoryPart table. If an update produces a record where
the (stockOnOrder + stockLevel) > (4 * reorderLevel) raise an
error (display an error) and rollback the change. */
To my understanding you are using SQL Server as your RDBMS and if I am not wrong in your below query Total is a column alias and you can't use column alias in where clause. That's why the error is for.
See a sample fiddle here for your understanding http://sqlfiddle.com/#!3/f902c/1
Select Count(ReorderLevel * 4) ReorderLevel,
Count(StockOnOrder + StockLevel) AS Total
Rather you can do it using derived table like below
select ReorderLevel,
Total
from
(
Select Count(ReorderLevel * 4) ReorderLevel,
Count(StockOnOrder + StockLevel) AS Total,
ReorderLevel
From InventoryPart
) tab
Where Total > ReorderLevel

Quicker way of finding duplicates in SQL Server

I'm trying to find a better way of finding duplicates in SQL Server. This took over 20 minutes to run with just over 300 million records before results started showing in the results window within SSMS. Another 22 minutes elapsed before it crashed.
Then SSMS threw this error after displaying 16,777,216 records:
An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown.
Schema:
ENCOUNTER_NUM - numeric(22,0)
CONCEPT_CD - varchar(50)
PROVIDER_ID - varchar(50)
START_DATE - datetime
MODIFIER_CD - varchar(100)
INSTANCE_NUM - numeric(18,0)
SELECT
ROW_NUMBER() OVER (ORDER BY f1.[ENCOUNTER_NUM],f1.[CONCEPT_CD],f1.[PROVIDER_ID],f1.[START_DATE],f1.[MODIFIER_CD],f1.[INSTANCE_NUM]),
f1.[ENCOUNTER_NUM],
f1.[CONCEPT_CD],
f1.[PROVIDER_ID],
f1.[START_DATE],
f1.[MODIFIER_CD],
f1.[INSTANCE_NUM]
FROM
[dbo].[I2B2_OBSERVATION_FACT] f1
INNER JOIN [dbo].[I2B2_OBSERVATION_FACT] f2 ON
f1.[ENCOUNTER_NUM] = f2.[ENCOUNTER_NUM]
AND f1.[CONCEPT_CD] = f2.[CONCEPT_CD]
AND f1.[PROVIDER_ID] = f2.[PROVIDER_ID]
AND f1.[START_DATE] = f2.[START_DATE]
AND f1.[MODIFIER_CD] = f2.[MODIFIER_CD]
AND f1.[INSTANCE_NUM] = f2.[INSTANCE_NUM]
Not sure how much faster this is, but worth a try.
SELECT
COUNT(*) AS Dupes,
f1.[ENCOUNTER_NUM],
f1.[CONCEPT_CD],
f1.[PROVIDER_ID],
f1.[START_DATE],
f1.[MODIFIER_CD],
f1.[INSTANCE_NUM]
FROM
[dbo].[I2B2_OBSERVATION_FACT] f1
GROUP BY
f1.[ENCOUNTER_NUM],
f1.[CONCEPT_CD],
f1.[PROVIDER_ID],
f1.[START_DATE],
f1.[MODIFIER_CD],
f1.[INSTANCE_NUM]
HAVING
COUNT(*) > 1

PostgreSQL Select Query for Date

I have a table in Postgres database "logs" which holds the error logs with their creation date
sample query : Select creation_date from logs
returns
"2011-09-20 11:27:34.836"
"2011-09-20 11:27:49.799"
"2011-09-20 11:28:04.799"
"2011-09-20 11:28:19.802"
I can find out the latest error using the command
SELECT max(creation_date) from log;
which will return "2012-02-06 12:19:28.448"
Now what I am looking for a query which could return the errors occured in last 15 minutes.
Any help on this will be appreciated
This should do the trick:
SELECT * FROM logs WHERE creation_date >= NOW() - INTERVAL '15 minutes'

Resources