Not a single-group group function in TDengine - sql

I'm writing a sql with partition by in TDengine database.
My statement is:
select app_id, task_name, sum(allocated) as allocated
from (
select tbname as app_id,
task_name,
apercentile(allocated, 90) as allocated
from yarn
where ts > now -5m
and tenant_name = 'xxxx'
partition by tbname,
task_name
) t
partition by task_name;
It reports an error:
DB error: Not a single-group group function
Why is it working correctly?

Related

H2 database : Error when using window function inside CTE

Using H2 database (version 1.4.200) I've encountered really strange error with usage of window functions inside CTE. When I include window function field into CTE and OVER() clause is empty it works correctly, but when I try to add ORDER BY/PARTITION BY inside the OVER() clause I'm encountering following error : '[42000][42000] Syntax error in SQL statement "WITH statement supports only SELECT, TABLE, VALUES, CREATE TABLE, INSERT, UPDATE, MERGE or DELETE statements" '
Non-working code sample:
WITH cte AS(
SELECT ROW_NUMBER() OVER (PARTITION BY CUST_NAME ORDER BY ORDER_DATE) AS rn
FROM TEST.HOLDING
)
SELECT *
FROM cte
One clarification: when I'm running statements using windows functions with PARTITION BY\ORDER BY parts not inside CTE it works well.
Working code sample:
SELECT ROW_NUMBER() OVER (PARTITION BY CUST_NAME ORDER BY ORDER_DATE) AS rn
FROM TEST.HOLDING
Test case for the issue:
CREATE TABLE PUBLIC.HOLDING(
CUST_NAME VARCHAR(50),
ORDER_DATE DATE
);
INSERT INTO PUBLIC.HOLDING(CUST_NAME, ORDER_DATE)
VALUES('Customer1',TO_DATE('20200201','YYYYMMDD')),
('Customer1',TO_DATE('20200202','YYYYMMDD')),
('Customer2',TO_DATE('20200201','YYYYMMDD')),
('Customer2',TO_DATE('20200202','YYYYMMDD'));
WITH cte AS(
SELECT CUST_NAME,
ORDER_DATE,
ROW_NUMBER() OVER (PARTITION BY CUST_NAME ORDER BY ORDER_DATE) AS rn
FROM PUBLIC.HOLDING
)
SELECT *
FROM cte;
SELECT CUST_NAME,
ORDER_DATE,
ROW_NUMBER() OVER (PARTITION BY CUST_NAME ORDER BY ORDER_DATE) AS rn
FROM PUBLIC.HOLDING;
you should tell us which database you uesd.if you use sql server,please check you code ,i gusse in front of 'with',whether not have ';'.
;WITH cte AS(
SELECT ROW_NUMBER() OVER (PARTITION BY CUST_NAME ORDER BY ORDER_DATE) AS rn
FROM TEST.HOLDING
)
SELECT *
FROM cte;

temp table from alias relation does not exist

I am trying to get total row number from tempTable2, but the error says
"relation "temptable2" does not exist"
select
(
select count(*) from tempTable2
where (EXTRACT(EPOCH FROM (maxdailylog - mindailylog))/3600)>4.5
) as totalHadir,
(
select count(*) from tempTable2
where (EXTRACT(EPOCH FROM (maxdailylog - mindailylog))/3600)<4.5
) as absenTidakKomplit
from
(
select LogHari, mindailylog, maxdailylog, count(LogHari) as jumlahLog from(
select waktuabsen::date as LogHari,
min(waktuabsen) over (partition by userid, waktuabsen::date) as mindailylog,
max(waktuabsen) over (partition by userid, waktuabsen::date) as maxdailylog,
dense_rank() over (order by waktuabsen::date) as grouplistno
from sdm.tabsen
where userid=866 and waktuabsen >= '2020-03-01T00:00:00' and waktuabsen < '2020-03-31T00:00:00'
) as tempTable
group by grouplistno, LogHari, mindailylog, maxdailylog
) as tempTable2
Temporary tables can be accessed from the same session from which they are created. For any other session they won't be available. Try running the query in the same window or same session where you have created temporary table

hive throwing error when I give alias name to my sub query

I am very new to Hive, so have a very basic question. In a Hive, can a sub query be given an alias and used outside in main query?
Basically when I try:
(SELECT *,
row_number() over(PARTITION BY ID, source_name
ORDER BY TIME DESC) rn
FROM x_table) temp_name
I get the following error:
AnalysisException: syntax error in line 1: undefined: .....)) temp_name^ Encountered: Identifier expected limit,order,union caused by : Exception :Syntax error
Try this..
Select
*
from
(select
*
,row_number() over(partition by ID,source_name order by time desc) rn
from x_table
) t;

Converting Code from Teradata to HIVE Rank Order

I need help with converting the below query from syntax appropriate for teradata to HIVE.
I've tried a copy and past for the subquery but I'm not able to get the qualify clause to work.
CREATE MULTISET VOLATILE TABLE Month_Shifts AS (
SELECT "Month"
, Emp_ID
, Emp_NM
, MAX(ending_team) OVER (PARTITION BY Emp_ID ORDER BY "Month" ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS Starting_team
, ending_team
FROM
(
SELECT "Month"
, Emp_id
, current_team AS Ending_team
, COUNT(DISTINCT call_key) AS CallVolume
FROM data
GROUP BY 1,2,3
QUALIFY ROW_NUMBER() OVER (PARTITION BY "month", Emp_ID, Emp_NM ORDER BY CallVolume DESC) = 1
) a
) WITH DATA NO PRIMARY INDEX ON COMMIT PRESERVE ROWS;
It should be able to run without issue. Currently seeing this error message:
FAILED: ParseException line 1:260 missing EOF at 'QUALIFY' near '4'
In Hive, you can just move the condition to the outer query:
SELECT "Month", Emp_ID, Emp_NM,
LAG(ending_team) OVER (PARTITION BY Emp_ID ORDER BY "Month") AS Starting_team,
ending_team
FROM (SELECT d."month", d.Emp_ID, d.Emp_NM,
current_team AS Ending_team,
COUNT(DISTINCT call_key) AS CallVolume,
ROW_NUMBER() OVER (PARTITION BY "month", Emp_ID, Emp_NM ORDER BY COUNT(DISTINCT call_key) DESC) as seqnum
FROM data d
GROUP d."month", d.Emp_ID, d.Emp_NM
) d
WHERE seqnum = 1;
Notes:
The QUALIFY is replaced by the WHERE in the outer query.
Do not use SELECT * with GROUP BY. List the columns. Regardless of database.
Hive supports LAG(), which is more appropriate for the outer SELECT.

Why does limiting a working query by WHERE ROWNUM <=10 result in an error from the EXPLAIN PLAN "ORA-12899: value too large for column"?

I am executing this query and explain plan inside Responsys.
The explain plan for this query works fine and returns about 70 rows of count data:
select * from ( Select LOCATION, count(LOCATION) COUNT_LOCATION From TABLE Group By LOCATION Order By COUNT_LOCATION Desc )
But add the row limiting WHERE ROWNUM <= 10 clause at the end:
select * from ( Select LOCATION, count(LOCATION) COUNT_LOCATION From TABLE Group By LOCATION Order By COUNT_LOCATION Desc ) WHERE ROWNUM <= 10
And this error in the explain plan results:
Error: java.sql.SQLException: ORA-00604: error occurred at recursive SQL level 1 ORA-12899: value too large for column "ACME_CUST"."PLAN_TABLE"."OPTIONS" (actual: 33, maximum: 30) : explain plan set statement_id ='ACME_CUST:1550184818627' into acme_CUST.PLAN_TABLE for select * from ( Select LOCATION, count(LOCATION) COUNT_LOCATION From TABLE Group By LOCATION Order By COUNT_LOCATION Desc ) WHERE ROWNUM <= 10
The results I am looking for is just the top 10 row counts for LOCATION.
Try using ROW_NUMBER() over the sort. I think it's because of the GROUP BY/ORDER BY combination in conjunction with ROWNUM:
select * from ( Select LOCATION, count(LOCATION), COUNT_LOCATION, ROW_NUMBER() OVER (ORDER BY COUNT_LOCATION GROUP BY LOCATION Desc) RowNumbers From TABLE Group By LOCATION Order By COUNT_LOCATION Desc) WHERE RowNumbers <= 10
See this article for other forms: How to use Oracle ORDER BY and ROWNUM correctly?