Tdengine syntax order issue - tdengine

TDengine sql error when executing aggregate query:
select avg(value) as gauge from table1 where ts >= now - 30d and ts <= now group by cluster interval(30d)
DB error: syntax error near "interval(30d);" (0.000552s)
What are the syntax errors in this sql?

Finally found in TDengine documentation: https://www.taosdata.com/docs/en/v2.0/taos-sql
Listed the order of select conditional statements.
SELECT select_expr [, select_expr ...]
FROM {tb_name_list}
[WHERE where_condition]
[SESSION(ts_col, tol_val)]
[STATE_WINDOW(col)]
[INTERVAL(interval_val [, interval_offset]) [SLIDING sliding_val]]
[FILL(fill_mod_and_val)]
[GROUP BY col_list]
[ORDER BY col_list { DESC | ASC }]
[SLIMIT limit_val [SOFFSET offset_val]]
[LIMIT limit_val [OFFSET offset_val]]
[>> export_file];
So the Group by should be put after Interval, here is the correct sql:
select avg(value) as gauge from table1 where ts >= now - 30d and ts <= now interval(30d) group by cluster

Related

sql server: An expression of non-boolean type specified in context where a condition is expected

I execute this code in postgresql and it runs fine.
SELECT t.anonymous_id AS anonym_id,
(select event_name
from tracks t1
where t1.anonymous_id = t.anonymous_id and user_id is null order by received_at desc limit 1) as last_null,
(select event_name
from tracks t2
where t2.anonymous_id = t.anonymous_id and user_id is not null ) as first_notnull
FROM tracks t
GROUP BY anonym_id
ORDER BY anonym_id;
Then I write it again in sql server and got some errors. After trying to fix, my code look like this:
SELECT t.anonymous_id AS anonym_id
FROM tracks t
JOIN
(select event_name
from tracks t1
where t1.anonymous_id = anonymous_id and user_id is null order by received_at desc offset 0 rows) as last_null,
(select event_name
from tracks t2
where t2.anonymous_id = anonymous_id and user_id is not null) as first_notnull
GROUP BY anonym_id
ORDER BY anonym_id;
Currently, I got an error
An expression of non-boolean type specified in context where a condition is expected near ','
Why? And how to fix that?

Internal SQL error (000603 (XX000)) due to 300010:2077141494

I am getting the following error in dbt, using snowflake and I can't figure out what the issue is.
Database Error in model stg_bank_balances2 (models/staging/cas/vpapay/finance/stg_bank_balances.sql)
000603 (XX000): SQL execution internal error:
Processing aborted due to error 300010:2077141494; incident 5570604.
compiled SQL at target/run/cas_datawarehouse/staging/cas/vpapay/finance/stg_bank_balances.sql
I have a staging table that is running 100% when I open the file and run it manually.
However when I run it with
dbt run --models +stg_bank_balances
then I get this error... any ideas?
Compiled SQL code:
with
__dbt__CTE__dw_bank_balance_base as (
with
source as (select * from CAS_RAW.BANK_BALANCE_INFORMATION_FOR_DATAWAREHOUSE.FACILITY_DATA),
renamed as (
select
to_date(date) as date
,FACILITY_BALANCE as facility_balance
,FACILITY_LIMIT as facility_limit
,LVR as loan_to_value_ratio_expected
,UNENCUMBERED_CASH as unencumbered_cash
from source
)
select *
from renamed
),data_sheet as ( select *
,row_number() over (order by date) as row_num
from __dbt__CTE__dw_bank_balance_base
),
calendar as ( select *
from ANALYTICS.dev_avanwyk.stg_calendar
where date >= (select min(date) from data_sheet)
and date <= current_date()
),
creating_leads as (
select a.*
,a.date as date_from
,case
when b.date is null then current_date()
else b.date
end as date_to
from data_sheet a
left join data_sheet b on a.row_num = b.row_num-1
),
renamed as (
select cal.date as cal_date
,ds.date_from, ds.date_to
,ds.facility_balance
,ds.facility_limit
,ds.loan_to_value_ratio_expected
,ds.unencumbered_cash
from calendar cal
left join creating_leads ds on
ds.date_from <= cal.date
and
cal.date < ds.date_to
)
select *
from renamed
Your cte names are the same, try using in your models unique cte (common table expression) names. You can see you are referencing twice a cte called "renamed". Try changing this and write back what is Snowflake spitting out.
I think Mincho is right.
The first thing to note is that this is a Database Error (docs) — this means that Snowflake is returning the error, and dbt is just passing it on.
Here, Snowflake is having difficulty because you have two CTEs (common table expressions) with the same name — renamed. It looks like you have an upstream model named dw_bank_balance_base that is ephemeral, so it's being injected as a CTE.
You can:
Rename one of your renamed CTEs to something else
Make dw_bank_balance_base a view or table by changing the materialized config
Let me know if that fixes it!
Found the issue - dbt doesn't want me joining a table to itself.
Hence I created another CTE with the prev_row_num = row_num -1 to facilitate this.
with
__dbt__CTE__dw_bank_balance_base as (
with
source as (select * from CAS_RAW.BANK_BALANCE_INFORMATION_FOR_DATAWAREHOUSE.FACILITY_DATA),
renamed as (
select
to_date(date) as date
,FACILITY_BALANCE as facility_balance
,FACILITY_LIMIT as facility_limit
,LVR as loan_to_value_ratio_expected
,UNENCUMBERED_CASH as unencumbered_cash
from source
)
select *
from renamed
),data_sheet as ( select *
,row_number() over (order by date) as row_num
,(row_number() over (order by date))-1 as prev_row_num
from __dbt__CTE__dw_bank_balance_base
),
data_sheet1 as ( select *
,(row_number() over (order by date))-1 as prev_row_num
from __dbt__CTE__dw_bank_balance_base
),
calendar as ( select *
from ANALYTICS.dev_avanwyk.stg_calendar
where date >= (select min(date) from data_sheet)
and date <= current_date()
),
creating_leads as (
select
a.date as date_from
,a.facility_balance
,a.facility_limit
,a.loan_to_value_ratio_expected
,a.unencumbered_cash
,case
when b.date is null then current_date()
else b.date
end as date_to
from data_sheet a
left join data_sheet1 b on a.row_num = b.prev_row_num
),
staging as (
select cal.date as cal_date
,ds.date_from
, ds.date_to
,ds.facility_balance
,ds.facility_limit
,ds.loan_to_value_ratio_expected
,ds.unencumbered_cash
from calendar cal
left join creating_leads ds on
ds.date_from <= cal.date
and
cal.date < ds.date_to
)
select *
from staging

SQL compilation error: Unsupported subquery type cannot be evaluated in Snowflake

I am facing SQL compilation error: Unsupported subquery type cannot be evaluated in one of the Snowflake instance used for QA for following query:
Select
CASE WHEN (Select count(USERSOURCE) From QA_DW.PRDODB.User Where
USERSOURCE=3) = 0 OR RECID not in (Select UD_SRC_KEY From QA_DW.PRDODB.User
Where
USERSOURCE=3)
THEN CreatedDateTime
ELSE CONVERT_TIMEZONE('US/Pacific','UTC',cast(CURRENT_TIMESTAMP() as
TIMESTAMP_NTZ))
END as FROM_DTIMEUTC
FROm QA_STAGING.HISTODB.STG_User_TBL
The same query runs in DEV instance.
use this Query instead of the above query :
SELECT
CASE WHEN c.COUNT_USERSOURCE = 0 OR RECID NOT IN (SELECT UD_SRC_KEY FROM
QA_DW.PRDODB.USER WHERE USERSOURCE=3)
THEN CreatedDateTime
ELSE CONVERT_TIMEZONE('US/Pacific','UTC',CAST(CURRENT_TIMESTAMP() AS TIMESTAMP_NTZ))
END AS FROM_DTIMEUTC
FROM QA_STAGING.HISTODB.STG_User_TBL
OUTER APPLY (
SELECT
COUNT(USERSOURCE) AS COUNT_USERSOURCE
FROM QA_DW.PRDODB.USER WHERE USERSOURCE=3
) AS C

How to convert SQL Server CTE query to Oracle 10G

I have problem, because I get error unsupported column aliasing and I don't know where is the cause. Does anyone know the answer and can help?
I have tried it but I get an error:
ORA-32033: unsupported column aliasing.
I am just trying to start using CTEs in Oracle (I am coming from T-SQL).
;with cte as
(
select
ITP_NO, FROM_DATE,
DATEADD(YEAR,cast(BILL_AMOUNT/ 58 as int)-1, FROM_DATE) as lastfromdate,
BILL_AMOUNT
from
test123
union all
select
c.ITP_NO, DATEADD(YEAR,1, c.FROM_DATE) as fromdate,
c.lastfromdate,
t.BILL_AMOUNT
from
cte c, test123 t
where
c.ITP_NO = t.ITP_NO
and DATEADD(YEAR,1, c.FROM_DATE) <= c.lastfromdate
)
,ctee as
(
select CAST(t.ITP_NO as varchar(15)) ITP_NO,
c.FROM_DATE, CAST( DATEADD(d,-1, DATEADD(YEAR, 1,c.FROM_DATE)) as date) as todate
,t.BILL_AMOUNT
from test123 t, cte c
where t.ITP_NO = c.ITP_NO
)
select ITP_NO,FROM_DATE,todate,
left(datename(month, FROM_DATE),3)+' '+CAST(year(FROM_DATE)as varchar(10))+' - '+left(datename(month, todate),3)+' '+CAST(year(todate)as varchar(10)) ExpiryDate
,BILL_AMOUNT
from ctee
order by 1 desc
I have tried below code but I get an error :
ORA-32033: unsupported column aliasing.
;with cte
as
(
select ITP_NO,
FROM_DATE,
add_months(FROM_DATE, (cast(BILL_AMOUNT/ 58 as int)-1)* 12) as lastfromdate
,BILL_AMOUNT
from test123
union all
select c.ITP_NO,
add_months(c.FROM_DATE,12) as lastfromdate
,c.lastfromdate
,t.BILL_AMOUNT
from cte c,test123 t
where c.ITP_NO = t.ITP_NO and add_months(c.FROM_DATE,12) <= c.lastfromdate
)
,ctee
as
(
select CAST(t.ITP_NO as varchar(15)) ITP_NO,
c.FROM_DATE,
(add_months(c.FROM_DATE,12)-1)
,t.BILL_AMOUNT
from test123 t, cte c
where t.ITP_NO = c.ITP_NO
)
select *
--ITP_NO,FROM_DATE,todate,
--left(datename(month, FROM_DATE),3)+' '+CAST(year(FROM_DATE)as varchar(10))+' - '+left(datename(month, todate),3)+' '+CAST(year(todate)as varchar(10)) ExpiryDate
--,BILL_AMOUNT
from ctee
order by 1 desc

GROUP BY error in SQL Server 2012

I'm getting this error
Column 'tbl_user.u_id' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
When I'm doing this query
SELECT *
FROM
tbl_user
JOIN
tbl_assign_role ON tbl_user.u_id = tbl_assign_role.tar_owner_id
WHERE
is_active = 1
AND u_id != 1
AND tar_is_deleted = 0
GROUP BY
tbl_assign_role.tar_owner_id
ORDER BY
tbl_user.u_updated_date DESC
Strongly agreed with #Dai
According to me, you should read first the role of "Group By". Here in your query there is no any aggregate function used, so there is no case here to use "Group By".
Do this, to continue removing duplicates while not needing the group by:
SELECT DISTINCT --[INSERT NEEDED COLUMNS HERE]
FROM
tbl_user
JOIN
tbl_assign_role ON tbl_user.u_id = tbl_assign_role.tar_owner_id
WHERE
is_active = 1
AND u_id != 1
AND tar_is_deleted = 0
ORDER BY
tbl_user.u_updated_date DESC

Resources