postgresql order by field? - postgresql

I want to get the result ordered by the same order of the list, but it keeps giving errors.
select "Usuario"."nombre", "Usuario"."codSis"
from "public"."Usuario"
where "codSis" in (6, 8)
order by field(codSis ,6,8)
this code gives mi this error
ERROR: column "codsis" does not exist
LINE 4: order by field(codSis ,6,8)
^
HINT: Perhaps you meant to reference the column "Usuario.codSis".
SQL state: 42703
Character: 112
but when I put this
select "Usuario"."nombre", "Usuario"."codSis"
from "public"."Usuario"
where "codSis" in (6, 8)
order by field(Usuario.codSis ,6,8)
it gives me this error
ERROR: column "Usuario.codSis" does not exist
LINE 4: order by field("Usuario.codSis" ,6,8)
^
SQL state: 42703
Character: 112
then I tried this
select "Usuario"."nombre", "Usuario"."codSis"
from "public"."Usuario"
where "codSis" in (6, 8)
order by field("Usuario"."codSis" ,6,8)
and it gave me this error
ERROR: function field(integer, integer, integer) does not exist
LINE 4: order by field("Usuario"."codSis" ,6,8)
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 106

You query should be next:
select
"Usuario"."nombre",
"Usuario"."codSis"
from "public"."Usuario"
where "codSis" in (6, 8)
order by "Usuario"."codSis" asc;

Related

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.

Unable To Assign Query Result To A Variable In 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

Ambiguous column error using orange3 - POSTGRES

When executing the query inside Orange 3, I got an ambiguous column error
QUERY
SELECT CAST(REPLACE(memoria_em_uso, 'G','') AS FLOAT),
CAST(REPLACE(memoria_total, 'G','') as FLOAT),
CAST(REPLACE(memoria_livre, 'M','') as FLOAT)
FROM tbl_memoria_hosts
OUTPUT
Error encountered in widget SQL Table:
Traceback (most recent call last):
File "/home/luis/anaconda3/lib/python3.7/site-packages/Orange/data/sql/backend/postgres.py", line 81, in execute_sql_query
cur.execute(query, params)
psycopg2.errors.AmbiguousColumn: column reference "replace" is ambiguous
LINHA 1: SELECT (("replace")::double precision) AS "replace", (("repl...
Note:
All columns in the table are VARCHAR, where the information is written as follows 7.7G

syntax error at or near "'select to_char(application_date::timestamp, '"

EXECUTE 'select to_char(application_date::timestamp, 'Mon-YY') as appl_month from my_schema.my_table;';
The above PostgreSQL EXECUTE statement is giving the below error:
ERROR: syntax error at or near "'select
to_char(application_date::timestamp, '" LINE 1: EXECUTE 'select
to_char(application_date::timestamp, 'Mon-YY...
^
********** Error **********
ERROR: syntax error at or near "'select
to_char(application_date::timestamp, '" SQL state: 42601 Character: 9
Any suggestions will be helpful.
Changed to below statement
EXECUTE 'select to_char(application_date::timestamp, ' || quote_literal(Mon-YY) || ') from standard.npo_weekly_export;';
But giving new error:
ERROR: syntax error at or near "'select to_char(application_date::timestamp, '"
LINE 1: EXECUTE 'select to_char(application_date::timestamp, ' || qu...
^
********** Error **********
ERROR: syntax error at or near "'select to_char(application_date::timestamp, '"
SQL state: 42601
Character: 9
Expected Output: - Counts by month in Mon-YY format
Application month Application # Final Approval #
Jan-17 1,000 800
Feb-17 1,010 808
Mar-17 1,020 816
Apr-17 1,030 824
If I do the below query:
select to_char(application_date, 'Mon-YY') as appl_month,
count(distinct application_id) as appl_count,
sum(final_approval_ind) as fa_count,
from my_schema.my_table
group by appl_month
order by appl_month;
Generated output: (Note: Sorted by text, not by date)
"Apr-17";94374;19953
"Apr-18";87446;20903
"Aug-17";102043;21536
"Aug-18";91107;20386
"Dec-17";63263;13755
"Dec-18";21358;74
"Feb-17";89447;18084
"Feb-18";75426;16144
"Jan-17";86103;16394
"Jan-18";79403;17766
"Jul-17";90380;18929
"Jul-18";85439;20186
"Jun-17";95596;20403
"Jun-18";85764;18707
"Mar-17";112929;23323
"Mar-18";91179;21841
"May-17";101907;22349
"May-18";90885;21550
"Nov-17";78284;16791
"Nov-18";80472;7656
"Oct-17";87955;18524
"Oct-18";82821;17056
"Sep-17";80740;17788
"Sep-18";75785;18009
Problem: to_char() returns text and it sorts by text and not by date. So the output is jumbled rather than sorted by Mon-YY.
Do the aggregation in a derived table (aka "sub-query") that preserves the data type, then do the sorting in the outer query:
select to_char(ap_month, 'Mon-YY') as appl_month
appl_count,
fa_count
from (
select date_trunc('month', application_date) as ap_month,
count(distinct application_id) as appl_count,
sum(final_approval_ind) as fa_count,
from my_schema.my_table
group by ap_month
) t
order by ap_month;
date_trunc('month', application_date) will normalize the application_date to the start of the month, but will retain the date data type, so that the sorting in the outer query works correctly.
I have no idea what the dynamic SQL in your question is supposed to do, but if you need to use that query for whatever reasons as dynamic SQL, you need to escape the single quotes by doubling them.
execute '
select to_char(ap_month, ''Mon-YY'') as appl_month
appl_count,
fa_count
from (
select date_trunc(''month'', application_date) as ap_month,
count(distinct application_id) as appl_count,
sum(final_approval_ind) as fa_count,
from my_schema.my_table
group by ap_month
) t
order by ap_month;
'; -- end of dynamic SQL
But using Postgres' dollar quoting would be easier:
execute $dyn$
select to_char(ap_month, 'Mon-YY') as appl_month
appl_count,
fa_count
from (
select date_trunc('month', application_date) as ap_month,
count(distinct application_id) as appl_count,
sum(final_approval_ind) as fa_count,
from my_schema.my_table
group by ap_month
) t
order by ap_month;
$dyn$; -- end of dynamic SQL
Note that you can nest dollar quoted strings, so if that query is used inside a function, just use a different delimiter than you use for the function body (see the example in the manual)

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.