Unable To Assign Query Result To A Variable In PostgreSQL - postgresql

I have been struggling to assign the query result to a temp variable in PostgreSQL and I just can't get it to work. Can anyone guide please?
I tried with the both the below queries and I get the error shown below. I then tried with temp table. But it still fails during the assignment to the temp variable.
set var.ITEM_ID =(select t.item_id from api_item t inner join api_identifier i on i.item_id=t.item_id where i.value='99999');
set var.ITEM_ID = select t.item_id from api_item t inner join api_identifier i on i.item_id=t.item_id where i.value='99999';
The first statement throws the error :-
[2020-03-03 08:10:50] [42601] ERROR: syntax error at or near "("
[2020-03-03 08:10:50] Position: 23
Second one throws the below error:-
[2020-03-03 08:10:55] [42601] ERROR: syntax error at or near "select"
[2020-03-03 08:10:55] Position: 24

Related

In Postgres how do I apply UPPER/LOWER on a list of strings? I am able to apply UPPER/LOWER on a single String

However when I tried on a list of String it is throwing error.
select distinct(tx.sub_tran_type)
from tran.txn tx
where tx.sub_tran_type = UPPER('homede')
limit 100
This query runs fine . However if you look at
select distinct(tx.sub_tran_type)
from tran.txn tx
where tx.sub_tran_type in UPPER('homede','online','omni-online','amznow')
limit 100
It throws this error:
SQL Error [42601]: ERROR: syntax error at or near "UPPER"`
This is just a trick. You can try this with your case:
select distinct tx.sub_tran_type
from tran.txn tx
where tx.sub_tran_type in (SELECT upper(unnest(array['homede','online','omni-online','amznow'])))
limit 100

Database-migration ms sql to postgresql

I am getting a syntax error when converting between SQL Server to PostgreSQL. Any thoughts?
IF (var_port_with_bmrk_total_mv != 0 AND var_bmrk_info IS NOT NULL) THEN
BEGIN
insert into t$tmp_diff
select #asof_dt asof_dt,#choiceID choiceID ,p.input_array_type ,p.group_order, CONVERT(DECIMAL(32,10),p.port_value/#var_port_total_mv) port_value,convert(decimal(32,10), isnull(bmrk_value/#port_with_bmrk_total_mv,0)) bmrk_value
from t$tmp_port_sum p, t$tmp_bmrk_sum b
where p.input_array_type=b.input_array_type and p.group_order = b.group_order
END;
ELSE
Original before conversion
insert into #tmp_other_diff
select #asof_dt asof_dt,#choiceID choiceID , b.input_array_type,b.grouping,convert(decimal(32,10),0) port_value, (bmrk_value/#port_with_bmrk_total_mv) bmrk_value
from #tmp_bmrk_other_sum b
where b.key_value not in ( select p.key_value from #tmp_port_other_sum p)
Error message:
Error occurred during SQL query execution
Reason:
SQL Error [42601]: ERROR: syntax error at or near ","
Position: 9030
the relevant comma being:
CONVERT(DECIMAL(32,10),p.port_value
There is no convert() function in Postgres. Use the SQL standard cast or the Postgres extension ::data type. In this case:
...., cast(0 as decimal(30,10)) port_value, ....
OR
...., 0::decimal(30,10) port_value, ...
Note: No comma after the expression. In the original port_value is the column alias. You need to keep it that way.

Why does one of these very similar jsonpaths work, but not the other?

Bumped into this while debugging a query from the ORM and have no idea why it happens. Asking mostly out of curiosity. In PostgreSQL 12.5 the following occurs:
-- All good:
select ('$[*] ? (#.live_at <= 2020-11-18)')::jsonpath
jsonpath |
---------------------------------------|
$[*]?(#."live_at" <= (2020 - 11) - 18) |
-- But then:
select ('$[*] ? (#.live_at <= 2020-05-18)')::jsonpath
SQL Error [42601]: ERROR: syntax error, unexpected IDENT_P at end of jsonpath input
Position: 9
SQL Error [42601]: ERROR: syntax error, unexpected IDENT_P at end of jsonpath input
Position: 9
SQL Error [42601]: ERROR: syntax error, unexpected IDENT_P at end of jsonpath input
Position: 9
SQL Error [42601]: ERROR: syntax error, unexpected IDENT_P at end of jsonpath input
Position: 9
ERROR: syntax error, unexpected IDENT_P at end of jsonpath input
Position: 9
ERROR: syntax error, unexpected IDENT_P at end of jsonpath input
Position: 9
I've a suspicion that this is related to octal representation, but node seems to eat 2020-05-18 just fine. What gives?

Eloquent Date Query

I have a table (with soft delete) and it is a whats on table. There is a field which is just a date field with the start date.
What I am trying to do is use eloquent to get a list of what on today onwards, but my attempts throw an error:
$wo = App\WhatsOn::whereDate('startDate', '=>', date('Y-m-d') )->get();
The error I am getting is
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=> ? and whatson.deleted_at is null' at line 1 (SQL: select * from whatson where date(startDate) => 2019-05-05 and whatson.deleted_at is null)
Help greatly appreciated!
It should be:
$wo = App\WhatsOn::whereDate('startDate', '>=', date('Y-m-d') )->get();
Dumb mistake!

column does not exist when it does

using PGAdminqueries:
SELECT * FROM analyzed_users;
SELECT * FROM time_table;
runs successfully. But query:
SELECT * FROM analyzed_users, time_table WHERE analyzed_users.id = time_table.userId
returns error:
ERROR: column analyzed_users.id does not exist
LINE 2: SELECT * FROM analyzed_users, time_table WHERE analyzed_user...
********** Error **********
ERROR: column analyzed_users.id does not exist
SQL state: 42703
Character: 49
I'm struggling with it for a while and I have no idea why it doesn't want to work..
The problem is in the WHERE clause in the second query:
WHERE analyzed_users.id = time_table.userId
The error is saying that analyzed_users.id doesn't exist in that table.
Check for and use the actual name of the column in analyzed_users that you want to compare to time_table.userId in the second query. That should fix the problem.