Postgresql RIGHT function giving an error - postgresql

SELECT DISTINCT
purchase_order_master.po1_num AS "PO #",
RIGHT(purchase_order_history.poh1_gl, 2) AS "Dept",
purchase_order_history.poh1_gl,
purchase_order_master.po1_podate AS "PO Date",
purchase_order_history.poh1_gl AS "GL Account #",
purchase_order_master.po1_dept,
purchase_order_master.po1_vname AS "Vendor Name",
purchase_order_master.po1_vnum AS "Vendor Number"
FROM
purchase_order_master
INNER JOIN
purchase_order_line_items
ON
purchase_order_master.po1_num = purchase_order_line_items.po2_num AND
purchase_order_master.po1_arid = purchase_order_line_items.po2_arid
INNER JOIN
purchase_order_history
ON
purchase_order_line_items.po2_num = purchase_order_history.poh1_ponum AND
purchase_order_line_items.po2_arid = purchase_order_history.poh1_arid
ORDER BY
"PO Date" DESC;
Gives error:
ERROR: function right(numeric, integer) does not exist
LINE 3: RIGHT(purchase_order_history.poh1_gl, 2) AS Dept,
^
HINT: No function matches the given name and argument types. You might need to add explicit type >casts.
Do I need to change the column values before I try?

Related

CASE expression in WHERE clause Postgresql

I have been struggling to figure out how to put a CASE expression in the WHERE clause using PostgreSQL. I need to convert the string to a date (as seen in line 3). This works fine. When I try to pull CURRENT_DATE in the WHERE clause I run into errors. Is this the best way to do this? Any suggestions would be very much welcomed.
SELECT
CASE WHEN
multi_app_documentation.nsma1_code = 'DATE' THEN TO_DATE(multi_app_documentation.nsma1_ans, 'MMDDYYYY') END AS "Procedure Date",
' ' AS "Case Confirmation Number",
ip_visit_1.ipv1_firstname AS "Patient First",
ip_visit_1.ipv1_lastname AS "Patient Last",
visit.visit_sex AS "Patient Gender",
TO_CHAR(visit.visit_date_of_birth, 'MM/DD/YYYY') AS "DOB",
visit.visit_id AS "Account Number",
visit.visit_mr_num AS "MRN",
' ' AS "Module",
' ' AS "Signed off DT",
CASE WHEN
multi_app_documentation.nsma1_code = 'CRNA' THEN multi_app_documentation.nsma1_ans END AS "Primary CRNA",
' ' AS "Secondary CRNA",
' ' AS "Primary Anesthesiologist",
' ' AS "Secondary Anesthesiologist",
' ' AS "Canceled Yes/No"
FROM
multi_app_documentation
INNER JOIN ip_visit_1 ON multi_app_documentation.nsma1_patnum = ip_visit_1.ipv1_num
INNER JOIN visit ON ip_visit_1.ipv1_num = visit.visit_id
WHERE
CASE
WHEN ( multi_app_documentation.nsma1_code = 'DATE' AND TO_DATE( multi_app_documentation.nsma1_ans, 'MMDDYYYY' ) = CURRENT_DATE END )
AND multi_app_documentation.nsma1_ans IS NOT NULL
ORDER BY
ip_visit_1.ipv1_lastname ASC
Your parentheses are unbalanced. Also, you don't even need a CASE expression in the WHERE clause. Use this version:
WHERE
multi_app_documentation.nsma1_code = 'DATE' AND
TO_DATE(multi_app_documentation.nsma1_ans, 'MMDDYYYY') = CURRENT_DATE
In addition, it is undesirable to store text dates in the nsma1_ans column. It would be better to use a bona-fide date column. If you must store the dates as text, then at least use the format YYYY-MM-DD, which then could simply be cast to date using nsma1_ans::date.

I try to join tables but I got error to it

My Code:
DB::table('books')->join('brands', 'books.user_id', '=', 'brands.user_id')->get();
Got this Error:
"SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: bigint = character varying\nLINE 1: ...* from \"users\" inner join \"books\" on \"users\".\"id\" = \"books\"....\n ^\nHINT: No operator matches the given name and argument types. You might need to add explicit type casts

ERROR: UNION types integer and character varying cannot be matched [SQL State=42804]

I am doing union of two views but getting above error. Please help me to resolve this issue.
some values are hardcoded there and need to extract the same in final view.
I am using below query in redshift-
query:
DROP VIEW IF EXISTS jgbl.vw_jgvcc_crm_case_activity CASCADE;
CREATE OR REPLACE VIEW jgbl.vw_jgvcc_crm_case_activity
AS
SELECT case_number as "CASE Number",
parent_case_number as "Parent Case Number",
date_opened as "Date Opened",
number_of_questions as "Number of Questions",
case_record_type as "CASE Record Type",
NULL as "Sub Type",
category as "Category",
NULL as "Sub Category",
country as Country,
customer_type as "Customer Type",
primary_account_subtype as "Primary Account Subtype",
source as Source,
call_center_location as "Call Center Location",
region as Region,
customer_region as "Customer Region",
NULL as "AE",
NULL as "PQC",
'ASPAC' as datasource
FROM JG_ASPAC.vw_jgvcc_aspac_crm_activity
UNION ALL
SELECT case_num as "CASE Number",
NULL as "Parent Case Number",
open_dt as "Date Opened",
cast(num_of_ques as integer) as "Number of Questions",
rec_type_nm as "CASE Record Type",
rec_sub_type as "Sub Type",
cat_desc as "Category",
sctgy_desc as "Sub Category",
custm_latam_ctry_nm as Country,
acct_type as "Customer Type",
NULL as "Primary Account Subtype",
src_in as Source,
case when alph_fl='Y' then 'Alphanumeric' else 'LATAM Center' end as "Call Center Location",
'LATAM' as Region,
NULL as "Customer Region",
Case when rec_type_nm='AE/PQC' and rec_sub_type in ('ADVERSE EVENT','AE + PQC') then 'Y' else 'N' End as "AE",
Case when rec_type_nm='AE/PQC' and rec_sub_type in ('AE + PQC', 'PRODUCT QUALITY COMPLAINT') then 'Y' else 'N' End as "PQC",
'LATAM' as datasource
FROM JG_LTM.vw_jgvcc_latam_crm_activity as v2
--LEFT JOIN jgbl.dim_iso_reg_cntry as t1 on t1.region = v2.ctry_iso2_cd
with no schema binding;
error:
An error occurred when executing the SQL command:
select * from jgbl.vw_jgvcc_crm_case_activity limit 10
ERROR: Invalid digit, Value '.', Pos 1, Type: Integer
Detail:
error: Invalid digit, Value '.', Pos 1, Type: Integer
code: 1207
context: 1.0
query: 87576
location: :0
process: query1_562_87576 [pid=0]
----------------------------------------------- [SQL State=XX000]
1 statement failed.
Execution time: 4.81s

postgresql/pgAdmin - accepting start_date & end_date arguments as input on query run

This is a postgresql db I'm working with using pgAdmin.
Forgive me if this is somewhat common knowledge, I'm new to postgresql in particular... and I didn't find any direct answers through prior searching.
I'm wondering if there's a simple way to implement start_time/end_time arguments as inputs when the query runs using pgadmin and any of it's built in features.
The data type I'm working with here is "timestamp with timezone".
Looking for some direction on the best way to implement this.
I considered declaring start_time and end_time as variables, then using WHERE to filter based on those, but without 3rd party/application level solutions, is there a way to prompt for input when the query runs inside of pgadmin?
I appreciate any suggestions- here's my attempt at getting something working, but it errors out: query has no destination for result data.
do $$
DECLARE
start_date timestamp := '2020-10-1';
end_date timestamp := '2020-10-5';
begin
select distinct on (account.id, menu.name, kitchen_item.name)
account.id as "Account ID",
account.firstname as "Seller First Name",
account.lastname as "Seller Last Name",
account.email as "Seller Email",
account.phone as "Seller Phone",
address.address as "Seller Address (Street)",
address.address_2 as "Seller Address 2",
account.zip_code as "Seller Zip",
address.neighborhood as "Seller Neighborhood",
menu.name as "Name of active menu",
kitchen_item.name as "Dishes",
kitchen_item.price as "Price",
kitchen_item.daily_max_orders as "Quantity",
menu.pickup_start_time as "Start time",
menu.pickup_end_time as "End time",
menu.repeat_mon as "Monday",
menu.repeat_tues as "Tuesday",
menu.repeat_wed as "Wednesday",
menu.repeat_thurs as "Thursday",
menu.repeat_fri as "Friday",
menu.repeat_sat as "Saturday",
menu.repeat_sun as "Sunday",
order_item.created as "Date of last sale"
from account
left join store on account.id = store.account_id
left join menu on store.id = menu.store_id
left join menu_item on menu.id = menu_item.menu_id
left join kitchen_item on (menu_item.kitchen_item_id = kitchen_item.id and store.id = kitchen_item.store_id)
left join orders on (orders.store_id = store.id)
left join order_item on (order_item.order_id = orders.id)
join store_address on store.id = store_address.store_id
join address on store_address.address_id = address.id
where orders.placed BETWEEN start_date AND end_date
order by account.id asc, menu.name, kitchen_item.name asc, order_item.created desc;
end $$;
DO creates an anonymous function that returns no data.
You can use WITH:
WITH input (start_date, end_date) AS
(SELECT '2020-10-01'::timestamp AS start_date,
'2020-10-05'::timestamp AS end_date)
SELECT ...
FROM...
JOIN input
WHERE orders.placed BETWEEN input.start_date AND input.end_date

how to replace comma in List of string in Jasper report

I have create one report with list of the string. The string got value parameter TO.REFOS_STATUS_CODE in ('10','ZZ','11','12','13'). i want to replace "," with ".". i have try the solution with my expression but it does'nt work.
The parameter expression is $P{refos_status}.equalsIgnoreCase("0") ?" ": " TO.REFOS_STATUS_CODE IN ( " +$P{refos_status}.replace( ',', '.' )+ ")".
Anyone know how to do that?
This is my query:
SELECT
TO.REFOS_STATUS_DESC_RPT ,
SUM(US.ENFUS_TOTAL_OFF_ACT) TOTAL
FROM TENF_RPT_UNSETTLE_SUMMACT US
INNER JOIN TREF_BRANCH B ON B.REFBR_BRANCH_ID = US.ENFUS_BRANCH
INNER JOIN TREF_STATE ST ON B.REFBR_STATE_CODE = ST.REFST_STATE_CODE
INNER JOIN TREF_OFFENCE_STATUS TO ON US.ENFUS_OFF_ACT =TO.REFOS_STATUS_CODE
WHERE
$P!{refos_status_1}
GROUP BY
TO.REFOS_STATUS_CODE,
TO.REFOS_STATUS_DESC_RPT
ORDER BY
TO.REFOS_STATUS_CODE,
TO.REFOS_STATUS_DESC_RPT