How can I set a value after Select query in PostgreSQL? - postgresql

I use simple select query:
SELECT
transactiontype
FROM
posfeed
LIMIT 100;
I result I have some rows with empty values.
I need set some default value in SELECT query result for some filed.
Something like:
if (transactiontype = '') {
//SET SOME VALUE HERE
} else {
//LEAVE
}

and can I somehow use it in 'where' clause
To use a column alias in the where clause you must wrap the select into a derived table:
select *
from (
SELECT CASE transactiontype
WHEN '' THEN 'some_default_value'
ELSE transactiontype
END AS transactiontype
FROM the_table
) t
where transactiontype = '...';
But I don't see the reason to do that, if you want to find those where the some_default_value is returned just run:
SELECT CASE transactiontype
WHEN '' THEN 'some_default_value'
ELSE transactiontype
END AS transaction_type
FROM the_table
where transactiontype = '';
But that then doesn't really makes sense, because the above is equivalent to:
SELECT 'some_default_value'
FROM the_table
where transactiontype = '';
So I guess there is something you did not include in the question or your comments.

Related

Postgres function passing array of strings

I have the Postgres function below to return some info from my DB. I need the p_ic parameter to be able to take an array of
strings.
CREATE OR REPLACE FUNCTION eddie.getinv(
IN p_ic character varying[],
IN p_id character varying)
RETURNS TABLE(cnt bigint, actualid text, actualcompany text, part text, daysinstock double precision, condition text,
ic text, price numeric, stock text, quantity bigint, location text, comments text) AS
$
BEGIN
RETURN QUERY
WITH cte AS (
SELECT
CASE WHEN partnerslist IS NULL OR partnerslist = '' THEN
'XX99'
ELSE
partnerslist
END AS a
FROM support.members WHERE id = p_id
), ctegroup AS
(
SELECT
u.id AS actualid,
(SELECT m.company || ' (' || m.id ||')' FROM support.members m WHERE m.id = u.id) AS actualcompany,
u.itemname AS part,
DATE_PART('day', CURRENT_TIMESTAMP - u.datein::timestamp) AS daysinstock,
TRIM(u.grade)::character varying AS condition,
u.vstockno::text AS stock,
u.holl::text AS ic,
CASE WHEN u.rprice > 0 THEN
u.rprice
ELSE
NULL
END AS price,
u.quantity,
u.location,
u.comments::text
FROM public.net u
WHERE u.holl in (p_ic)
AND visibledate <= now()
AND u.id = ANY(REGEXP_SPLIT_TO_ARRAY(p_id ||','|| (SELECT a FROM cte), ','))
ORDER BY u.itemname, u.id
)
SELECT
COUNT(ctegroup.ic) OVER(PARTITION BY ctegroup.ic ORDER BY ctegroup.ic) AS cnt,
actualid,
MAX(actualcompany) AS actualcompany,
MAX(part) AS part,
MAX(daysinstock) AS daysinstock,
STRING_AGG(condition,',') AS condition,
MAX(ic) AS ic,
MAX(price) AS price,
STRING_AGG(stock,',') AS stock,
SUM(quantity) AS qty,
STRING_AGG(location,',') AS location,
STRING_AGG(comments,';') AS comments
FROM ctegroup
GROUP BY part, actualid, ic
ORDER BY actualid;
END; $
LANGUAGE 'plpgsql';
I am calling it from the pgAdminIII Query window like this:
SELECT * FROM eddie.getinv(array['536-01036','536-01033L','536-01037'], 'N40')
But it is returning this error:
ERROR: operator does not exist: text = character varying[]`
LINE 28: WHERE u.holl in (p_ic)`
How do I fix this, or am I calling it incorrectly? I will be calling it from a PHP API function similar to this:
$id = 'N40';
$ic = array('536-01036','536-01033L','536-01037');
$sql = "SELECT * FROM eddie.getinv(array['". implode("','",$ic)."'], '".$id."');";
try
{
$results = pg_query($sql);
if(pg_num_rows($results) == 0) {
$rows = [];
}
else
{
$data = pg_fetch_all($results);
foreach($data as $item)
{
$rows[$item["ic"]][] = $item;
}
}
pg_free_result($results);
}
catch (Exception $e)
{
$err = array("message"=>$e->getMessage(), "code"=> $e->getCode(), "error"=>$e->__toString().",\n".print_r($_REQUEST, true));
echo json_encode($err);
}
echo json_encode($rows);
It looks like your array is being passed to the function just fine. The problem is in your query.
IN () clauses expect a comma-separated list of values. When you put an array in there, it's interpreted as a one-element list, where the value is the whole array. In other words, u.holl in (p_ic) will check if u.holl is equal to p_ic, and the comparison fails due to the type mismatch.
If you want to test the value against the contents of the array, use u.holl = ANY(p_ic).

Checking if string is in table

Hi I have table called mytable and 1 column first_name , how can I check if 'John' is in this table and return the result as true/false.
You can utilize a EXISTS-query:
select exists (select * from mytable where mytable.first_name = 'John')
The above query will return a boolean which will be true if the sub-query inside the braces returns any rows at all; the boolean will false if the sub-query return no rows.
SELECT count(*) FROM mytable WHERE first_name LIKE 'John';
returns number of occurrences. If there is no John in table, returns zero.
SELECT CASE WHEN COUNT(*) > 0 THEN 'true' ELSE 'false' END
FROM mytable
WHERE first_name LIKE '%John%'

SELECT CASE: more than one row returned by a subquery used as an expression

I got very good help for my question PostgreSQL: return message after count = 0
This query works fine:
SELECT CASE WHEN COUNT(*) = 0 THEN 'NO RESULT'
ELSE CAST(COUNT(*) as TEXT)
END as myfield from mytable
But today I need if some rows are in table, I need to show select result.
I tried:
SELECT CASE WHEN COUNT(*) = 0 THEN 'NO RESULT'
ELSE (select name from mytable)
END as myfield from mytable
And I got error:
ERROR: more than one row returned by a subquery used as an expression
I found that IN function but don't have any idea to improve it.
I think you can do something like this:
select name from mytable
UNION ALL
SELECT 'NO RESULT'
WHERE NOT EXISTS(SELECT NULL FROM mytable)

Return a value if select returned null

I need to return a value if select returned null. however I found a solution here by putting a query in a sub-query
SELECT COALESCE((SELECT id FROM tbl WHERE id = 9823474), 4) AS id FROM RDB$DATABASE;
The query above would return Null because the value 9823474 does not exist in the table but I want to return a value in that case (for ex 4) so I found the only solution to use select inside sub query and then COALESCE would work, If I did not do that COALESCE will also return Null.
Is it the only solution ?
No, that is not an only way for example
Select first 1 id from (
Select id FROM tbl WHERE id = 9823474
Union All
Select 4 from rdb$database)
Or you can use anonymous procedure http://firebirdsql.su/doku.php?id=execute_block
EXECUTE BLOCK RETURNS ( id integer )
AS
BEGIN
IF ( EXISTS (SELECT * FROM tbl WHERE id = 9823474) )
THEN id = 9823474;
ELSE id = 4;
SUSPEND;
END
... there always are many methods there

Select records but if null, show a message

I know this easy, just can't get the syntax right. If no results, show a message instead:
select COALESCE(select SomeField from SomeTable where [#SomeTableVariable].SomeID = #SomeID, 'none to delete')
if that query is only going to return one row then:
select COALESCE(
(select SomeField from SomeTable where [#SomeTableVariable].SomeID = #SomeID),
'none to delete'
)
should do it.