How to use count and row_number in java spring boot - spring

I want to fetch the data in JPA Spring Boot from database using cast and count function, but my data is not returning any ID. So, I creating new field that returning auto number using row_number() in SQL.
I'm getting a problem in JPA Spring Boot to fetch the data using row_number,
here is my SQL Syntax
select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction
from vx_transaction."transaction" t
group by cast(t.create_datetime as date)
the result in databases is as expected
Result in DBEAVER
but, I'm getting error in JPA Spring
ERROR: column "t.id" must appear in the GROUP BY clause or be used in
an aggregate function
Then, I'm trying to fix the problem with adding the id in group by. Here's the code
select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction
from vx_transaction."transaction" t
group by cast(t.create_datetime as date), id
There is not error, but..... the result not as expected
Not as expected result
Can anyone help me to solve this problem??

Related

Spring Auth Server JDBC implementation - error with Oracle DB BLOB [duplicate]

I have this query I am trying to run but I keep running into this error. I am trying to do a Where clause that compares the data (BLOB column) to :var2 which is a blob object.
Here is my code.
SELECT max(id)
INTO :var1
FROM table_name
where data = :var2;
Any suggestions to why I would be getting this ORA-00932 error?
I am comparing a blob to a blob column, shouldn't that be fine?
Thanks
They aren't simple types and you need to use a function to compare them.
SELECT max(id)
INTO :var1
FROM table_name
where dbms_lob.compare(data,:var2) = 0;

query to change the date format in the column

I have a report to create but there's a little problem I can't solve because the column(date) I generate has a different value. I use it in a subquery. My question is can I used a format so that I can manage to edit the value of the column? Please see the table below for reference,
My column(date) contains
date_columns
2019-06-20T11:09:15.674+00:00
2019-06-20T11:09:15.674+00:00
2019-06-20T11:09:15.674+00:00
2019-06-20T11:09:15.673+00:00
Now, my problem is it returned me ORA-01427: single-row subquery returns more than one row becaue of that 2019-06-20T11:09:15.673+00:00. Can I do a format to make it looked like 2019-06-20T11:09:15?
I tried the query below but nothing changed. It returned me a same error.
select distinct to_date(substr(dar.last_update_date,1,15),'YYYY-MM-DD HH:MI:SS')
select distinct to_date(dar.last_update_date,1,15,'YYYY-MM-DD HH:MI:SS')
Thanks!
2019-06-20T11:09:15.673+00:00 appears to be a string of a datetime in the official XML representation. We can turn it into an actual timestamp using to_timestamp_tz() and then cast the timestamp to a date:
select cast(
to_timestamp_tz('2019-06-20T11:09:15.673+00:00','YYYY-MM-DD"T"HH24:MI:SS:FFTZH:TZM')
as date)
from dual;
However, I'm not sure how this will resolve the ORA-01427: single-row subquery returns more than one row error. This exception occurs when we use a subquery like this …
where empno = ( select empno
from emp
where deptno = 30
and sal > 2300 )
… and the subquery returns more than one row because the WHERE clause is too lax. The solution is to fix the subquery's WHERE clause so it returns only one row (or use distinct in the subquery's projection if that's not possible).

How to validate nulls - oracle etl testing?

I am new to oracle and I would like to know how do we check the nulls in non null columns in oracle tables as part of the ETL testing process. (The two tables could be T1 and T2). Please let me know a sample query.
I have already tried
select count(*) from T2 where T2.column is Null;
Thanks, Santosh
'=' operator can not be used while comparing to null. Replace '=' to 'is' as per below query.
select count(*) from T2 where T2.column is Null;
Assume there is column name: id in the table record, and id should not contain any null value. Query to check this is:
select id from record where id is null;

Spark SQL throwing error "java.lang.UnsupportedOperationException: Unknown field type: void"

I am getting below error in Spark(1.6) SQL while creating a table with column value default as NULL. Ex: create table test as select column_a, NULL as column_b from test_temp;
The same thing works in Hive and creates the column with data type "void".
I am using empty string instead of NULL to avoid the exception and new column getting string data type.
Is there any better way to insert null values in hive table using spark sql ?
2017-12-26 07:27:59 ERROR StandardImsLogger$:177 - org.apache.hadoop.hive.ql.metadata.HiveException: java.lang.UnsupportedOperationException: Unknown field type: void
at org.apache.hadoop.hive.ql.metadata.Hive.createTable(Hive.java:789)
at org.apache.hadoop.hive.ql.metadata.Hive.createTable(Hive.java:746)
at org.apache.spark.sql.hive.client.ClientWrapper$$anonfun$createTable$1.apply$mcV$sp(ClientWrapper.scala:428)
at org.apache.spark.sql.hive.client.ClientWrapper$$anonfun$createTable$1.apply(ClientWrapper.scala:426)
at org.apache.spark.sql.hive.client.ClientWrapper$$anonfun$createTable$1.apply(ClientWrapper.scala:426)
at org.apache.spark.sql.hive.client.ClientWrapper$$anonfun$withHiveState$1.apply(ClientWrapper.scala:293)
at org.apache.spark.sql.hive.client.ClientWrapper.liftedTree1$1(ClientWrapper.scala:239)
at org.apache.spark.sql.hive.client.ClientWrapper.retryLocked(ClientWrapper.scala:238)
at org.apache.spark.sql.hive.client.ClientWrapper.withHiveState(ClientWrapper.scala:281)
at org.apache.spark.sql.hive.client.ClientWrapper.createTable(ClientWrapper.scala:426)
at org.apache.spark.sql.hive.execution.CreateTableAsSelect.metastoreRelation$lzycompute$1(CreateTableAsSelect.scala:72)
at org.apache.spark.sql.hive.execution.CreateTableAsSelect.metastoreRelation$1(CreateTableAsSelect.scala:47)
at org.apache.spark.sql.hive.execution.CreateTableAsSelect.run(CreateTableAsSelect.scala:89)
at org.apache.spark.sql.execution.ExecutedCommand.sideEffectResult$lzycompute(commands.scala:58)
at org.apache.spark.sql.execution.ExecutedCommand.sideEffectResult(commands.scala:56)
at org.apache.spark.sql.execution.ExecutedCommand.doExecute(commands.scala:70)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$5.apply(SparkPlan.scala:132)
at org.apache.spark.sql.execution.SparkPlan$$anonfun$execute$5.apply(SparkPlan.scala:130)
at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:150)
at org.apache.spark.sql.execution.SparkPlan.execute(SparkPlan.scala:130)
at org.apache.spark.sql.execution.QueryExecution.toRdd$lzycompute(QueryExecution.scala:56)
at org.apache.spark.sql.execution.QueryExecution.toRdd(QueryExecution.scala:56)
at org.apache.spark.sql.DataFrame.withCallback(DataFrame.scala:153)
at org.apache.spark.sql.DataFrame.<init>(DataFrame.scala:145)
at org.apache.spark.sql.DataFrame.<init>(DataFrame.scala:130)
at org.apache.spark.sql.DataFrame$.apply(DataFrame.scala:52)
at org.apache.spark.sql.SQLContext.sql(SQLContext.scala:829)
I couldn't find much information regarding the datatype void but it looks like it is somewhat equivalent to the Any datatype we have in Scala.
The table at the end of this page explains that a void can be cast to any other data type.
Here are some JIRA issues that are kinda similar to the problem you are facing
HIVE-2901
HIVE-747
So, as mentioned in the comment, instead of NULL you can cast it to any of the implicit data types.
select cast(NULL as string) as column_b
I started to get a similar issue. I build the code down to an example
WITH DATA
AS (
SELECT 1 ISSUE_ID,
DATE(NULL) DueDate,
MAKE_DATE(2000,01,01) DDate
UNION ALL
SELECT 1 ISSUE_ID,
MAKE_DATE(2000,01,01),
MAKE_DATE(2000,01,02)
)
SELECT ISNOTNULL(lag(IT.DueDate, 1) OVER (PARTITION by IT.ISSUE_ID ORDER BY IT.DDate ))
AND ISNULL(IT.DueDate)
FROM DATA IT

How to min function without group by in Hive

Consider the following hive query.
SELECT
id,
name,
min(from_unixtime(unix_timestamp(), 'yyyy_MM_dd_HH_mm_ss')) as SYSDATE
FROM tablename
The reason why I used min function is that I wanted the same SYSDATE in all of my records. If I don't add min here, multiple SYSDATE may appear.
I got an error running the query:
An exception was caught.
Error while compiling statement: FAILED: SemanticException [Error 10025]: Line 3:4 Expression not in GROUP BY key 'name'
So I added GROUP BY in my query and it worked.
SELECT
id,
name,
min(from_unixtime(unix_timestamp(), 'yyyy_MM_dd_HH_mm_ss')) as SYSDATE
FROM tablename
GROUP BY id, name
But what if I have twenty or more columns? Isn't it inconvenient to add them all to GROUP BY? And why should I add GROUP BY here? I just want a consistent SYSDATE all across the records. Is there any other way to make it work?
if you do not have any concern about performance, try to use window function to calculate min:
SELECT
id ,
name ,
min(from_unixtime(unix_timestamp(), 'yyyy_MM_dd_HH_mm_ss')) over(partition by 1) as SYSDATE
FROM tablename

Resources