Is there a way to pretty print SELECT query result in ClickHouse? - clickhouse

Alternative in ClickHouse Select query to pretty print like in MySQL vertically.
For example -
SELECT * from table \g;
I found this resource but its just text
https://clickhouse-docs.readthedocs.io/en/latest/formats/vertical.html
With --multiline as a parameter for clickhouse-client I am getting following error . Though i can use \G format as per one of the answer without this param :
:) select * from hits_v1 limit 2; \G
:-] ;
Syntax error (Multi-statements are not allowed): failed at position 30 (end of query):
select * from hits_v1 limit 2; \G ;

ClickHouse supports this feature:
You can specify \G instead of or after the semicolon. This indicates Vertical format.
select * from numbers(2)\G
/* result
Row 1:
──────
number: 0
Row 2:
──────
number: 1
*/
select * from numbers(2);\G
/* result
Row 1:
──────
number: 0
Row 2:
──────
number: 1
*/

Well, it was quick. I have to use FORMAT Parameter :
select * from hits_v1 limit 100 FORMAT Vertical;
More to study here :
https://clickhouse.tech/docs/en/interfaces/formats/

Related

H2 recursive call not recognizing column

I have the below code and the database says that it can't see column n.
I have same problem when I use real database tables. But with real table I'm not even aliasing the column.
WITH RECURSIVE counter AS (
SELECT 1 as n
UNION ALL
SELECT n + 1 FROM counter WHERE n < 10
)
SELECT * from counter;
Following error is given:
Error: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "N" not found; SQL statement:
WITH RECURSIVE counter AS (
SELECT 1 as n
UNION ALL
SELECT n + 1 FROM counter WHERE n < 10
)
SELECT * from counter [42122-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:451)
CTEs are experimental in H2 and have different issues. Actually you don't need them here. In H2 you can use
SELECT * FROM SYSTEM_RANGE(1, 10);
In both PostgreSQL and H2 you can use the PosgtreSQL-specific
SELECT * FROM GENERATE_SERIES(1, 10);
If you need to change the column name to N add a derived column list T(N) after the function.
SELECT * FROM GENERATE_SERIES(1, 10) T(N);
But if you still want to use a CTE, add column name(s) to the WITH clause and optionally remove the alias from the constant, if will not be required in that case:
WITH RECURSIVE counter(n) AS (
SELECT 1
UNION ALL
SELECT n + 1 FROM counter WHERE n < 10
)
SELECT * from counter;
I faced same error. My unit test was failling (using h2 2.1.210).
My conclusion to avoid error like "org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "XXXXX" not found" was:
add the list of your column names after CTE name as "AS" keyword
don't name your CTE "CTE"
:-)

ORA-01422: exact fetch returns more than requested number of rows

Stuck with this error...
DECLARE
AVGRUNTIME number(10,0);
PERFCATEGORYRANGELOCOUNT number(10,0);
PERFCATEGORYRANGEHICOUNT number(10,0);
DW_LOW number(10,0);
DW_HI number(10,0);
CURSOR LC_ABC IS
select distinct(ap.dwprocessid)
from auditprocess ap, dwprocess d
where ap.dwprocessid = d.dwprocessid
and ap.insertts > sysdate - 61
and dwprocessmonitorind = 'Y';
BEGIN
FOR REC IN LC_ABC
LOOP
select ((ap.LASTUPDATETS - INSERTTS)*24*60*60) as AVGRUNTIME,
(.1 * ((ap.LASTUPDATETS - INSERTTS)))as PERFCATEGORYRANGELOCOUNT ,
(1.9 * ((ap.LASTUPDATETS - INSERTTS)))as PERFCATEGORYRANGEHICOUNT
INTO AVGRUNTIME, PERFCATEGORYRANGELOCOUNT, PERFCATEGORYRANGEHICOUNT
from auditprocess ap
where ap.dwprocessid = rec.dwprocessid
and insertts > sysdate - 61
group by (ap.LASTUPDATETS - INSERTTS);
[Error][1] at line 1
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 27
You are grouping by ap.LASTUPDATETS - INSERTTS and if you have 2 different values an error will rise since you cannot insert 2 rows into a variable.
Maybe you should not group by those columns and use an aggregation function on the select clause like:
select avg((ap.LASTUPDATETS - INSERTTS)*24*60*60) as AVGRUNTIME,
min(.1 * ((ap.LASTUPDATETS - INSERTTS)))as PERFCATEGORYRANGELOCOUNT ,
max(1.9 * ((ap.LASTUPDATETS - INSERTTS)))as PERFCATEGORYRANGEHICOUNT
INTO AVGRUNTIME, PERFCATEGORYRANGELOCOUNT, PERFCATEGORYRANGEHICOUNT
from auditprocess ap
where ap.dwprocessid = rec.dwprocessid
and insertts > sysdate - 61;
It's exactly what the error says. Your SELECT...INTO returns more than one row.
CURSOR LC_ABC contains a DISTINCT - this implies you already know that dwProcessId may occur more than once in table auditProcess.
Then you select again from the table using the exact same logic - this will return all matching rows.
You then aggregate using the value of lastUpdateTS - insertTs. You will therefore return one row per dwProcessId per value of (lastUpdateTS - insertTs)

Expected CHAR got NUMBER

DB: Oracle 11g
Query:
SELECT CASE
WHEN rs.OPTION = '3'
THEN
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_GRP cg
ON cg.GROUP_ID = ex.GRP_ID
)
ELSE
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_POL cg
ON cg.GROUP_ID = ex.GRP_ID
)
END AS PROPTS
FROM PR_OPTS
I am getting error 'expected CHAR got NUMBER', here EXTS,GROUP_ID & GRP_ID are numeric. Then how there is a chance of expecting CHAR?
Generally when Oracle compares different datatypes such as a NUMBER with a CHARACTER, implicit conversion kicks in and all is well (provided the data can be converted.) For example, if you have a function that expects a CHARACTER value but you pass it a NUMBER, all is well - Oracle simply converts the NUMBER to character.
E.g. a function like this:
create or replace function get_something(p_id VARCHAR2) return number ...
works if you call it with this:
get_dno(10);
or this:
get_dno('10');
and in SQL:
select * from some_table where numeric_column = '10' -- no problem.
A popular place where you see this kind of error is with the return values in CASE statements. For instance, you'll get that error if you have something like this:
SQL> SELECT CASE WHEN 1 = 1 THEN '1' ELSE 2 END
2 FROM dual
3 ;
SELECT CASE WHEN 1 = 1 THEN '1' ELSE 2 END
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected CHAR got NUMBER
(The datatype from the first WHEN clause is what it expects in the other WHEN/ELSE clauses that follow.)
But in your case the WHEN and THEN both return counts - the datatypes are consistent. So, I think you have a red-herring in there.
As Alex mentioned above, OPTION is a keyword and if you try and create a table with that as a column name, Oracle disagrees:
SQL> create table dummy
2 (option varchar2(10)
3 );
(option varchar2(10)
*
ERROR at line 2:
ORA-00904: : invalid identifier
This works:
SQL> create table dummy
2 (option_col varchar2(10)
3 );
Table created.
or you could do it with quotes:
SQL> create table dummy
2 ("option" varchar2(10));
Table created.
But now you're in a world of hurt - you need quotes from now on:
SQL> select option from dummy;
select option from dummy
*
ERROR at line 1:
ORA-00936: missing expression
SQL> select d.option from dummy d;
select d.option from dummy d
*
ERROR at line 1:
ORA-01747: invalid user.table.column, table.column, or column specification
With quotes:
SQL> select d."option" from dummy d;
no rows selected
So, if your query is really giving you "expected CHAR, got NUMBER", it looks to me like something is off.
Essentially, it means some of the fields you are using aren't compatible with each other. It's basically a "type mismatch". Just check to see if any types of CHAR are being used with types of NUMBER. Then you can either switch the type of one, or simply use a conversion as part of the query.
The issue is OPTION = '3', the quotation marks indicate that you're looking for a string containing the solitary character 3.
Try this instead:
SELECT CASE
WHEN rs.OPTION = 3
THEN
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_GRP cg
ON cg.GROUP_ID = ex.GRP_ID)
ELSE
(SELECT COUNT(DISTINCT ex.EXTS) AS TMPCOL0
FROM CRSIDM.SUB_OPTS ex
INNER JOIN CRSIDM.SUB_OPTS_POL cg
ON cg.GROUP_ID = ex.GRP_ID)
END AS PROPTS
FROM PR_OPTS

Special Sorting in Teradata

I have the following query that is not sorting the table the way I want it:
SELECT * FROM tbl
ORDER BY
BAN,
BEN,
bill_seq_no DESC,
CASE
WHEN Ebene='BAN - Open Debts' THEN 1
WHEN Ebene='BEN - Open Debts' THEN 2
END,
Rufnummer
;
It should sort the table first by BAN, then by BEN. Now in the third level row with Ebene='BEN - Open Debts' has bill_seq_no = NULL. This is why it sorts this row in the bottom.
I want it at the top.
How can I do that?
Got it! It's
SELECT * FROM adam_tmp.AAM711119__result
ORDER BY
BAN,
BEN,
CASE
WHEN Ebene LIKE '%BEN - Open Debts%' THEN 1
ELSE 2
END,
bill_seq_no DESC,
Rufnummer
;

Using substitution variables in table name

My issue is easy, I want to find out if what I am trying to do can be done or not.
I have tables like this : detailcro1, detailcro2, detailcro3 ... I want to use a substitution var for some automatic process.
I wrote
DEFINE TT = 'detailcro'
select * from &TT||'2';
and as a result I have ORA-00933:.
Can I create such a query ?
Thank you
You need to usu a dot notation at the end of the variable
SQL> select * from &TT.l;
Enter value for tt: dua
old 1: select * from &TT.l
new 1: select * from dual
D
-
X
so
SQL> DEFINE TT = 'detailcro';
SQL> select * from &TT.2;
old 1: select * from &TT.2
new 1: select * from detailcro2
no rows selected

Resources