How to get entries of a date greater then x date in Apache Phoenix - apache-phoenix

I have a column activation_date in Phoenix of type Date, which contains entries like "2016-06-18 07:00:00.000", how do I do select on same for a date greater then x date:
select activation_date from xyz where activation_date > '2017-03-17 07:00:00.000' limit 10;
Above query gives error like:
Error: ERROR 203 (22005): Type mismatch. DATE and VARCHAR for
USER_DATA.ACTIVATION_DATE > '2017-03-17 07:00:00.000'
(state=22005,code=203)
org.apache.phoenix.schema.TypeMismatchException: ERROR 203 (22005):
Type mismatch. DATE and VARCHAR for ACTIVATION_DATE > '2017-03-17
07:00:00.000'

I fixed the issue using the TO_DATE function:
select activation_date from xyz where activation_date > TO_DATE('2017-03-17 00:00:00.000') limit 10;
TO_DATE

Related

How can I filter for CURRENT_DATE / SYSDATE - 2 years?

I have a table t with a column "date" which has the type "DATE":
date
2018-10-01
2019-02-03
2020-01-01
2021-01-01
I want to get only entries where CURRENT_DATE / SYSDATE minus 2 years is true. So the result should be (CURRENT_DATE / SYSDATE = "2021-05-01":
date
2019-02-03
2020-01-01
2021-01-01
My code:
SELECT *
FROM t
WHERE YEAR(t.date) >= ADD_YEARS(TRUNC(CURRENT_DATE), -2)
But that gives me the error
Feature not supported: Incomparable Types: DECIMAL(4,0) and DATE!
Using SYSDATE with
SELECT *
FROM t
WHERE YEAR(t.date) >= ADD_YEARS(TRUNC(SYSDATE), -2)
gives the error
Feature not supported: Incomparable Types: DECIMAL(4,0) and DATE!
I tried https://stackoverflow.com/a/28888880/4435175 with
SELECT *
FROM t
WHERE YEAR(t.date) >= add_months( trunc(sysdate), -12*2 )
but that gave me the same error
Feature not supported: Incomparable Types: DECIMAL(4,0) and DATE!
sysdate already returns you date. No need to trunc it.
Looks like something is wrong with data type of column t.date.
What describe t; shows you?
UPD.
I see 2 options here. First one is more preferable for me, as it doesn't apply function to every t.date value.
SELECT *
FROM t
WHERE t.date >= add_years (sysdate, -2)
--WHERE years_between (sysdate, t.date) >= 2

How to multiple decimal numbers in column within a group by?

I have sql table that looks like this:
date id value type
2020-01-01 1 1.03 a
2020-01-01 1 1.02 a
2020-01-02 2 1.06 a
2020-01-02 2 1.2 a
2020-01-03 3 1.09 b
I need to build a query that groups by date,id, and type by multiplying the value column whereever type = 'a'.
what new table should look like:
date id value type
2020-01-01 1 1.0506 a
2020-01-02 2 1.272 a
2020-01-03 3 1.09 b
currently I am building this query,
select
date, id, value, type
from my_table
where date between 'some date' and 'some date'
and trying to fit in EXP(SUM(LOG(value)
but, how do I do the multiplication only where type = 'a' in a group by?
edit:
there are more than 2 values in the type column
I am using redshift. Not postgresql.
select date
, id
-- use the 'case' syntax to check if it is type 'a'
, case when type = 'a' then EXP(SUM(LOG(value::float))) -- your multiply logic
else max(value) -- use min or max to pick only one value
end as value
from my_table
where date between 'some date' and 'some date'
group
by date, id, type

BigQuery - DATE_TRUNC error

trying to get the monthly aggregated data from Legacy table. Meaning date columns are strings:
amount date_create
100 2018-01-05
200 2018-02-03
300 2018-01-22
However, the command
Select DATE_TRUNC(DATE date_create, MONTH) as month,
sum(amount) as amount_m
from table
group by 1
Returns the following error:
Error: Syntax error: Expected ")" but got identifier "date_create"
Why does this query not run and what can be done to avoid the issue?
Thanks
It looks like you meant to cast date_create instead of using the DATE keyword (which is how you construct a literal value) there. Try this instead:
Select DATE_TRUNC(DATE(date_create), MONTH) as month,
sum(amount) as amount_m
from table
GROUP BY 1
I figured it out:
date_trunc(cast(date_create as date), MONTH) as Month
Another option for BigQuery Standard SQL - using PARSE_DATE function
#standardSQL
WITH `project.dataset.table` AS (
SELECT 100 amount, '2018-01-05' date_create UNION ALL
SELECT 200, '2018-02-03' UNION ALL
SELECT 300, '2018-01-22'
)
SELECT
DATE_TRUNC(PARSE_DATE('%Y-%m-%d', date_create), MONTH) AS month,
SUM(amount) AS amount_m
FROM `project.dataset.table`
GROUP BY 1
with result as
Row month amount_m
1 2018-01-01 400
2 2018-02-01 200
In practice - I prefer PARSE_DATE over CAST as former kind of documents expectation about data format
Try to add double quote to date_creat :
Select DATE_TRUNC('date_create', MONTH) as month,
sum(amount) as amount_m
from table
group by 1

Getting different results when converting timestamp to text

I'm tying to convert timestamp field in format (YYYY-MM-DD HH:MI:SS.MS) to it's text representation. But for some reason getting different results:
If I'm trying to convert timestamp from table:
create table test_dt (dt timestamp);
insert into test_dt values ('2016-04-14 17:10:33.007');
insert into test_dt values ('2016-04-14 17:10:33');
Timestamps are getting truncated up to the seconds:
select dt::text from test_dt;
dt
---------------------
2016-04-14 17:10:33
2016-04-14 17:10:33
(2 rows)
But if Im using direct select statement, everything works:
select '2016-04-14 17:10:33.007'::timestamp::text;
varchar
-------------------------
2016-04-14 17:10:33.007
(1 row)
The question is not how to convert it to the text from table and include precision, but rather:
what am I doing wrong?
why those 2 approaches returns different result?
what's the rational behind this behaviour?
UPDATE
as #muistooshort suggested the following command gives the correct result:
select c::text from (select '2016-04-14 17:10:33.007'::timestamp union select '2016-04-14 17:10:33'::timestamp ) as t(c);
c
-------------------------
2016-04-14 17:10:33
2016-04-14 17:10:33.007
(2 rows)
and yes test_dt does have .007 :
select * from test_dt;
dt
-------------------------
2016-04-14 17:10:33
2016-04-14 17:10:33.007
(2 rows)
Also to_char gives milliseconds from the table:
select to_char(dt, 'YYYY-MM-DD HH:MI:SS.MS') from test_dt;
to_char
-------------------------
2016-04-14 05:10:33.000
2016-04-14 05:10:33.007
(2 rows)

PSQL: Aggregate function (sum) not working

I have this query (artist_money is of MONEY type, e.g. $30,456.11.):
SELECT SUM(
CASE
WHEN end_date - date '2015-12-3' <= 28 AND end_date - date '2015-12-3' > 0 THEN artist_money
END,
CASE
WHEN date '2015-12-3' - start_date > 28 THEN artist_money
END
) AS "gonorar"
FROM peacecard
WHERE artist_id = 12345 AND contract IS NOT NULL
When I try to get the result, here's the error:
ERROR: function sum(money, money) does not exist
LINE 1: SELECT sum(
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
What's going on? According to Documentation, SUM should take the parameters if it's MONEY type.
Thanks a lot!
The problem is the comma, not the money type. Perhaps you intend:
SELECT SUM(CASE WHEN end_date - date '2015-12-3' <= 28 AND end_date - date '2015-12-03' > 0
THEN artist_money
END),
SUM(CASE WHEN date '2015-12-3' - start_date > 28
THEN artist_money
END
) AS "gonorar"
FROM peacecard
WHERE artist_id = 12345 AND contract IS NOT NULL;
Or, if you want one column, then it makes much more sense to only use one case:
SELECT SUM(CASE WHEN end_date - date '2015-12-3' <= 28 AND end_date - date '2015-12-03' > 0
THEN artist_money
WHEN date '2015-12-3' - start_date > 28
THEN artist_money
END
) AS "gonorar"
FROM peacecard
WHERE artist_id = 12345 AND contract IS NOT NULL;
You are trying to pass two parameters to an aggregate 1 parameter function.