Im trying to get a percentage calculation im having an error.
ERROR: syntax error at or near "100"
LINE 1: SELECT dispute_lost , ROUND(COUNT() 100/SUM(COUNT (*)) OVER(...
^
SQL state: 42601
Character: 37
Query:
SELECT dispute_lost , ROUND(COUNT() 100/SUM(COUNT (*)) OVER(),2) AS Percentage_lost
FROM yellevate_invoices
GROUP BY dispute_lost;
You need to COUNT something, you cannot COUNT nothing. COUNT() is not accepted. The syntax for COUNT agergate is :
COUNT( [ { DISTINCT | ALL } ] <expression>)
<expression ::= { * | <scalar_expression> }
Related
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
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)
I'm getting a syntax error when trying to use the GROUPING SETS function in my Postgres DB.
I've looked at the documentation here and I believe the syntax is correct, but I'm still getting an error.
I believe the syntax should be GROUP BY GROUPING SETS ((COLUMN), (COLUMN), ()) to get the grand total of my SUM() from the the SELECT part of my code. So I put in GROUP BY GROUPING SETS ((amount.date), ( places.place), ());
What am I doing wrong here?
SELECT
EXTRACT(MONTH FROM amount.date),
places.place,
CASE
WHEN places.place = 'A' THEN SUM(amount.amount)
WHEN places.place = 'B' THEN SUM(amount.amount)
WHEN places.place = 'C' THEN SUM(amount.amount)
ELSE SUM((amount.amount) * 2.5)
END AS "Total"
FROM amount
LEFT JOIN places ON places.id = amount.id
WHERE EXTRACT(YEAR FROM amount.date) = 2017
GROUP BY GROUPING SETS ((amount.date), (places.place), ());
I want to use something like REGEXP_SUBSTR in DB2 (version 10.5).
There is an example of what I tried:
SELECT REGEXP_SUBSTR('hello to you', '.o')
FROM sysibm.sysdummy1
I got this error : [Error Code: -420, SQL State: 22018]
09:23:12 [SELECT - 0 row(s), 0.000 secs] [Error Code: -420, SQL State: 22018] DB2 SQL Error: SQLCODE=-420, SQLSTATE=22018, SQLERRMC=INTEGER, DRIVER=3.57.82
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec [0 successful, 0 warnings, 1 errors]
There is no equivalent function to REGEXP_SUBSTR in DB2.
However you can achieve similar results with the XMLQUERY function
SELECT
XMLCAST(
XMLQUERY('fn:replace($src,"^hello | you$","")'
PASSING 'hello to you' AS "src")
AS VARCHAR(255))
FROM SYSIBM.SYSDUMMY1;
Difficulty Here, fn:replace removes matched patterns because DB2's implementation doesn't support () and $1 sub-grouping patterns
With DB2 V11.1 there is now REGEXP_SUBSTR(). It works simply as this:
Example 1
SELECT REGEXP_SUBSTR('hello to you', '.o',1,1)
FROM sysibm.sysdummy1Copy
Return the string which matches any character preceding a 'o'.
The result is 'lo'.
Example 2
SELECT REGEXP_SUBSTR('hello to you', '.o',1,2)
FROM sysibm.sysdummy1Copy
Return the second string occurrence which matches any character preceding a 'o'.
The result is 'to'.
Example 3
SELECT REGEXP_SUBSTR('hello to you', '.o',1,3)
FROM sysibm.sysdummy1
Return the third string occurrence which matches any character preceding a 'o'.
The result is 'yo'.
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.