PostgreSQL Getting error in order to change explicit type cast - postgresql

SELECT COUNT(*)
FROM WASADMIN.DAILYTXNSREPORT
WHERE UBACCOUNTID = '01ED10EOD0100'
AND UBTXNAMT = '109.63'
AND UBTYPE = 'I'
AND UBVALUEDTTM LIKE '11/7/2015 12:00:00 AM%'
AND UBTXNAMTCR = '109.63'
AND UBTXNAMTDR = '0.0'
AND UBTXNCODE = 'IAP'
AND UBTXNNARRATION = 'Fixed Narrative:Interest Application'
AND UBTXNSRCBRANCH = '70000001.0'
AND UBTXNBASEEQ = '109.63'
AND UBCHANNELID = 'UXP'
An error occurred when executing the SQL command:
SELECT COUNT(*) FROM WASADMIN.UBTB_DAILYTXNSREPORT WHERE UBACCOUNTID='01ED10EOD0100' AND UBTXNAMT='109.63' AND UBTYPE='I' AND UBVALUEDTTM LIKE '11/7/2...
ERROR: operator does not exist: timestamp without time zone ~~ unknown
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 139
Expected answer: to get the count

LIKE is for comparing string values, not for timestamps.
I am not sure what the LIKE condition is supposed to achieve, but it seem you want to find rows where `` is equal to midnight of 2015-11-07. The following would do that:
AND ubvaluedttm = timestamp '2015-11-07 00:00:00'
A range condition is probably closer to what the LIKE condition would do:
AND ubvaluedttm >= timestamp '2015-11-07 00:00:00'
AND ubvaluedttm <= timestamp '2015-11-07 00:00:00.999999'

Related

Tableau Prep: How can I refer to a parameter in the Custom SQL query?

Using Tableau Prep Builder version 2021.4.4 I connected to Postgresql database (version 12) and created a Custom Query as the only input in the Flow.
Then I created a parameter (my_date).
In the custom query I have:
select my_field
from my_table
where date = -- How can I refer to my_date?
I have already tried the following but all failed:
where date = ${my_date}-- syntax error at or near "$"
where date = $my_date -- syntax error at or near "$"
where date = :my_date -- syntax error at or near ":"
where date = (my_date) -- "my_date" does not exist
where date = my_date -- "my_date" does not exist
where date = $1 -- Index 0 out of bounds for length 0
Use < > , for example:
where date = <my_date>
OR
where date = '<my_date>'

case when in where clause postresql

select service_code,service_cat_code,mobile_no,upper(applicant_name_eng) as name,to_char(license_date,'dd/mm/yyyy')as license_from,to_char(license_valid_upto,'dd/mm/yyyy')as license_to,Upper(license_no),district_code,taluk_code,CONCAT(address_building,', ', address_cityvillage,', ',address_locality,', ',address_landmark,', ',address_street) as address
from mst_license
WHERE cast(license_valid_upto as date) = case
WHEN license_valid_upto < now()
THEN
case
when license_valid_upto = '2021-06-30'
then 1 else 0
END
ELSE
case when license_valid_upto > now()
then 1 else 0
End
END
and Upper(license_no)='1SP146924BJP'
I want license valid should be either greater than now or if license valid less than now it must be with the date ''30/06/2021' but when i use above query i get error
ERROR: operator does not exist: date = integer
LINE 3: WHERE cast(license_valid_upto as date) = case
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 418
Help me out guys
The main issue you have is that your case statement returns an integer (1 or 0) but you are trying to compare that to a date, which you cannot do as Postgres is a strict data typing. Even if it did work it would always be false (well except for 1969-12-31/1970-01-01). Moreover the case structure is not needed. The best/correct to compare dates is just use date values. Since you did not indicate the data type for column license_valid_upto so based on how it is used I'll assume it is timestamp with timezone as that is what NOW() returns. Your query becomes:
select service_code
, service_cat_code
, mobile_no
, upper(applicant_name_eng) as name
, to_char(license_date,'dd/mm/yyyy')as license_from
, to_char(license_valid_upto,'dd/mm/yyyy')as license_to
, upper(license_no) as license_no
, district_code
, taluk_code
, concat(address_building,', '
,address_cityvillage,', '
,address_locality,', '
,address_landmark,', '
,address_street) as address
from mst_license
where and upper(license_no)='1SP146924BJP'
and ( cast(license_valid_upto as date) > cast( now() as date)
or (cast (icense_valid_upto as date) < cast( now() as date)
and cast (icense_valid_upto as date) = date '2021-06-30'
)
);
Also, learn for format your queries for readability and so you do not need to scroll right. You, and others looking at your queries later will appreciate it later.

ERROR: operator does not exist : time without time zone >= bytea

I get error like this
ERROR: operator does not exist : time without time zone >= bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
when i try the following sql in java hibernate
select sto.name AS store,
sum(odi.subtotal_price) AS sales, sum(odi.qty) AS qty_sold,
((sum(odi.subtotal_price))/(sum(odi.qty))) AS average,
min(CAST(ord.date_out AS date)) AS start_date,
max(CAST(ord.date_out AS date)) AS end_date,
concat(min(CAST(ord.date_out AS time)), ' - ', max(CAST(ord.date_out AS time))) as time,
sum(odi.cost_of_good_sold*odi.qty) AS COGS, ((sum(odi.cost_of_good_sold*odi.qty))/(sum(odi.qty))) AS average_cogs,
date_trunc('day', ord.date_out) AS trx_day
FROM trx_order_detail_item odi
LEFT JOIN trx_order AS ord on ord.id = odi.order_id
LEFT JOIN mst_store AS sto on sto.id = ord.store_id
WHERE sto.id = :store and ord.date_out between :date1 and :date2
and CAST(ord.date_out AS TIME) BETWEEN :hour1 AND :hour2 and ord.order_status_id IN :orderstatus and ord.void_status = :voidStatus
GROUP BY sto.name, date_trunc('day', ord.date_out)
ORDER BY date_trunc('day', ord.date_out)
the error exist in CAST(ord.date_out AS TIME) BETWEEN :hour1 AND :hour2
before i added that it runs perfectly
any suggestion for this?

Jasper-reports: Error: operator does not exist date = boolean

[EDIT: This problem was a result of a bug within version 3.7.6]
The following postgresql query is returning an error:
operator does not exist date = boolean.
I can't figure out why. Here is the postgresql code that is giving me the error:
select
c.source,
s.name,
s.grouping,
s.kli,
s.term_desc,
(s.population - s.online) as non_hb_pop,
s.online as hb_pop,
s.population as full_pop,
s.rep_date
from
dwh.rpt_cu_private_kli_summary s, dwh.rpt_sgmt_clients c
where
s.partner_id::integer = $P{rpt_cu}
and s.rep_date = $P{rpt_date_beg}
and s.userid=c.userid
group by
c.source, s.term_desc, s.name, s.grouping,
s.population, s.online, s.kli, s.rep_date
order by
s.grouping,
full_pop desc,
s.term_desc;
What does the above error message mean?
What is the value for $P{rpt_date_beg} ? That's where things go wrong. Check the real query, might be in the errorlog, and do some debugging. Maybe some quotes ' are missing around the date value.

column "statement_date" is of type date but expression is of type integer

Consider the following query:
INSERT INTO statement_line_items
SELECT count(*)::integer as clicks, sum(amount_cents)::integer as amount_cents, imps.user_id, imps.created_at::date as statement_date
FROM impression_events imps
INNER JOIN transactions t ON t.event_id = imps.id
AND t.event_type = 'ImpressionEvent'
AND amount_cents >= 0
WHERE imps.created_at >= (now() - interval '8 days')::date
AND imps.created_at < (now() - interval '7 day')::date
AND imps.clicked = true
GROUP BY imps.user_id, imps.created_at::date;
This is returning:
ERROR: column "statement_date" is of type date but expression is of type integer
LINE 2: ...icks, sum(amount_cents)::integer as amount_cents, imps.user_...
^
HINT: You will need to rewrite or cast the expression.
********** Error **********
ERROR: column "statement_date" is of type date but expression is of type integer
SQL state: 42804
Hint: You will need to rewrite or cast the expression.
Character: 117
My table structure for statement_line_items is:
"id"; "integer"
"user_id"; "integer"
"statement_date"; "date"
"description"; "character varying(255)"
"clicks"; "integer"
"amount_cents"; "integer"
"created_at"; "timestamp without time zone"
"updated_at"; "timestamp without time zone"
You are putting the imps.userid in your statement_date column. That has to fail.
count(*)::integer as clicks goes into id
sum(amount_cents)::integer as amount_cents goes into userid
imps.user_id goes into statement_date
To specify the order you are inserting you can do this:
INSERT INTO statement_line_items (col1, col2, ...)
values (select data_for_col1, data_for_col2, ...)
If you have this kind of problem check if your date is in the apostrophe.