The following query is working fine:
WITH
(
SELECT 1
) AS test_val
SELECT
test_val,
count()
FROM system.tables
┌─test_val─┬─count()─┐
│ 1 │ 93 │
└──────────┴─────────┘
But if I'm rewriting the same with subquery:
WITH
(
SELECT 1
) AS test_val
SELECT *
FROM
(
SELECT
test_val,
count()
FROM system.tables
)
There is an error:
Code: 47. DB::Exception: Received from clickhouse-server:9000. DB::Exception: Missing columns: 'test_val' while processing query: 'SELECT test_val, count() FROM system.tables', required columns: 'test_val', source columns: 'total_bytes' ...
(I know that It's useless query, I just post it here for an example)
How can I use test_val inside a subquery ?
Clickhouse version: 20.11
Found out that WITH can be moved into the nested query:
SELECT *
FROM
(
WITH
(
SELECT 1
) AS test_val
SELECT
test_val,
count()
FROM system.tables
)
Related
I have this query where a user defined function is added in the select and group by statement.
The inner select query without the WITH clause runs fine and doesn't give any error. But after adding WITH clause it gives the following error -
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
*Cause:
*Action: Error at Line: 3 Column: 29
I need the WITH clause to return only a subset of the entire result set based on input ranges.
Query is as follows:
WITH INFO AS (
SELECT
GET_EVAULATED_VALUE(T.C_IMP, T.IMP) AS IMPORTANCE,
count(*) AS NO_OF_PC_AFFECTED
FROM TABLE_NAME T
WHERE T.ACNT_REL_ID = 16
GROUP BY
(GET_EVAULATED_VALUE(T.C_IMP, T.IMP))
ORDER BY IMPORTANCE desc
)
SELECT * FROM
(
SELECT ROWNUM AS RN,
(SELECT COUNT(*) FROM INFO) COUNTS,
IMPORTANCE
FROM INFO
)
WHERE RN > 0 AND RN <= 10;
I am not sure how to use CTE with group by on user defined function. But I realized that I can rewrite the query to remove sub-query and CTE and make it simpler as following (and it works):
select * from (
select a.*, ROWNUM rnum from
(SELECT
count(*) over() as COUNTS,
GET_EVAULATED_VALUE(T.C_IMP, T.IMP) AS IMPORTANCE,
count(*) AS NO_OF_PC_AFFECTED
FROM TABLE_NAME T
WHERE T.ACNT_RELATION_ID = 16
GROUP BY
(GET_EVAULATED_VALUE(T.C_IMP, T.IMP))
ORDER BY importance desc) a
where ROWNUM <= 10 )
where rnum >= 0;
Same issue here, I created a table "TABLE_CTE" instead of using a CTE and it worked.
CREATE TABLE TABLE_CTE
AS
SELECT
USER_DEFINED_FUNCTION(date_1),
COUNT(*)
FROM
TABLE_NAME
GROUP BY
USER_DEFINED_FUNCTION(date_1)
;
SELECT * FROM TABLE_CTE
I want to select duplicate data in column 'egno' and not duplicate data in 'dsver'.
My source MySQL query is:
select * from test
where egno in (
select egno
from test
group by egno
having count (*) >1
)
group by dsver
having count (*) = 1
But it doesn't work on Oracle. So I wrote a new query:
select * from test
where egno in (
select egno
from test
group by egno
having count (*) >1
)
and dsver in (
select dsver
from test
group by dsver
having count (*) = 1
)
I think it will use more resources on the server. I need some suggestions on this query.
Use the COUNT() analytic function:
SELECT *
FROM (
SELECT t.*,
COUNT(*) OVER ( PARTITION BY egno ) AS num_egno,
COUNT(*) OVER ( PARTITION BY dsver ) AS num_dsver
FROM test t
)
WHERE num_egno > 1
AND num_dsver = 1;
When i try run the below query i'm getting this error
ORA-00917: missing comma
00917. 00000 - "missing comma"
*Cause:
*Action: Error at Line: 26 Column: 22
Query:
select * from
(select 'india',po.team,pa.NAME,pa.userid,me.amount as RN
from
port po,
switch pa,
merchant me
where po.SEQNO=pa.SEQNO
and (pa.SEQNO=me.SEQNO and RN is not null))
PIVOT (
count(*) for RN in(RN<1,RN-20.01)
)
I want display the percentage columns as <=1, between 10 to 20 and >20 by using the pivot logic
Use a case statement in your initial sub-query:
select *
from (
select *
from (
select 'india',
po.team,
pa.NAME,
pa.userid,
CASE
WHEN me.amount <= 1 THEN '<1'
WHEN me.amount BETWEEN 10 AND 20 THEN '10-20'
WHEN me.amount > 20 THEN '>20'
END as RN
from port po
INNER JOIN switch pa
ON ( po.SEQNO=pa.SEQNO )
INNER JOIN merchant me
ON ( pa.SEQNO=me.SEQNO )
)
where RN is not null
) PIVOT (
count(*) for RN in( '<1', '10-20', '>20' )
);
I'm trying to run the following query on Hive:
SELECT COUNT(*)
FROM mydata
WHERE store NOT IN (SELECT store_out
FROM ( SELECT a.store as store_out, COUNT(*) AS CNT
FROM mydata a
GROUP BY store) TB1
WHERE CNT > AVG(CNT) + STDDEV(CNT) AND CNT < AVG(CNT) - STDDEV(CNT))
But I'm getting the following errors:
Error while compiling statement: FAILED: SemanticException [Error 10249]: Line 3:6 Unsupported SubQuery Expression 'store': Correlating expression cannot contain unqualified column references.
How can I write this query in another way?
Thanks!
I don't have you exact data so it is hard to verify this but I would do something like
SELECT COUNT(*)
FROM (
SELECT a.*
, flg
FROM mydata a
LEFT OUTER JOIN (
SELECT store_out, flg
FROM (
SELECT store_out
, cnt
, 1 AS flg
, AVG(cnt) OVER () AS avg_cnt
, STDDEV_SAMP(cnt) OVER () AS std_cnt
FROM (
SELECT store AS store_out
, COUNT(*) AS cnt
FROM mydata
GROUP BY store ) x
) y
WHERE cnt > avt_cnt + std_cnt AND cnt < avg_cnt - std_cnt ) z
ON a.store = z.store_out ) final
WHERE flg IS NULL
Basically, left join the subquery and create a dummy column. That column won't exists in the main table so where all the flg values are NULL, these are the stores you want. Hope this helps.
two records should exist in a table in oracle, one is from the request and the other is from the response.
i would like to select all those records from today but the problem is the other pair might be existing from the previous two days or more.
how do i ensure that the records that will be returned are only 1 and will not exist in the previous days
select A.referenceNum, A.datetime, A.Type from table A where A.datetime >= sysdate - 1
To visualize, i would like to select only reference num 789ef.
ReferenceNum DateTime Type
123ab (datetoday) Request
123ab (datetoday) Response
456cd (datetoday) Request
456cd (datetoday-2) Response
789ef (datetoday) Request
You can do it in a single table scan (compared to using NOT EXISTS which will use two table scans) using analytic functions:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( ReferenceNum, DateTime, Type ) AS
SELECT '123ab', SYSDATE, 'Request' FROM DUAL UNION ALL
SELECT '123ab', SYSDATE, 'Response' FROM DUAL UNION ALL
SELECT '456cd', SYSDATE, 'Request' FROM DUAL UNION ALL
SELECT '456cd', SYSDATE - 2, 'Response' FROM DUAL UNION ALL
SELECT '789ef', SYSDATE, 'Request' FROM DUAL;
Query 1:
SELECT ReferenceNum
FROM (
SELECT ReferenceNum,
COUNT( CASE WHEN TRUNC( DateTime ) = TRUNC( SYSDATE ) THEN 1 END )
OVER ( PARTITION BY ReferenceNum ) AS num_today,
COUNT( CASE WHEN TRUNC( DateTime ) <> TRUNC( SYSDATE ) THEN 1 END )
OVER ( PARTITION BY ReferenceNum ) AS num_other_day
FROM table_name t
)
WHERE num_today = 1
AND num_other_day = 0
Results:
| REFERENCENUM |
|--------------|
| 789ef |
Use NOT EXISTS operator
SELECT A.referenceNum, A.datetime, A.Type
from table A
where A.datetime >= sysdate - 1
AND NOT EXISTS (
SELECT null FROM table B
WHERE A.referenceNum = B.referenceNum
AND b.datetime < a.datetime
)
SELECT referenceNum
FROM A A1
WHERE TRUNC(A1.datetime) > TRUNC(sysdate)-1
AND A1.Type = 'Request'
AND NOT EXISTS
(SELECT 1
FROM A
WHERE TRUNC(A.datetime) >= TRUNC(sysdate)-1
AND A.Type = 'Response'
AND A.referenceNum = A1.referenceNum
)