I'm importing some data from a JSON file, but I can't use the IN operator in the query, as it generates an error. But it works when I use only 1 condition.
Below is the code:
with dados as (
select SPLIT_PART (sar_conteudo::text, 'header":', 2) as conteudo
from tb_gcc_situacao_arquivo
where sar_arquivo = 5012
--and sar_dt_movimento = '2022-01-12'), --> That's how it works
and sar_dt_movimento in ('2021-12-30','2021-12-31','2022-01-03')), --> not like this
dados_2 as (
select left (conteudo, length(conteudo)-1) as conteudo_2 from dados),
dados_final as(
SELECT *
from json_to_recordset((select * from dados_2)::json)
as x
("textoLinhaOriginal" text, "descricaoMensagem" text))
select
substring("textoLinhaOriginal",171,18) as captacao, "descricaoMensagem"
from dados_final
where "textoLinhaOriginal" ilike 'RDB 1INCL%'
and "descricaoMensagem" <> 'EXECUCAO OK'
Searching the internet, I noticed that the json_to_recordset only takes one line. So when passing more than 1 parameter, it will return an error message. How can I resolve this?
ERROR: more than one row returned by a subquery used as an expression
Related
I am trying to convert jsonb-column into string, but after using jsonb_array_elements I can't find any function what could do anything useful with setof jsonb it returns.
SELECT string_agg( jsonb_array_elements(col), ':' )
FROM test_json_array
WHERE id = 1;
ends with:
ERROR: function string_agg(jsonb, unknown) does not exist
LINE 1: SELECT string_agg( jsonb_array_elements(col), ':' ) FROM tes...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Using ARRAY-constructor or array_agg give another error:
$ SELECT ARRAY[ jsonb_array_elements(col) ] FROM test_json_array WHERE id = 1;
ERROR: set-valued function called in context that cannot accept a set
$ SELECT array_agg( jsonb_array_elements(col)) FROM test_json_array WHERE id = 1;
ERROR: set-valued function called in context that cannot accept a set
All I found googling around ended jsonb-part in FROM-clause, like:
SELECT string_agg( j.x, ':' )
FROM test_json_array tbl, jsonb_array_elements_text(tbl.col) AS j(x)
WHERE tbl.id = 1;
I'd like to have such aggregation together in SELECT-clause, but I can't understand, why it is impossible? Or is it?
Using Postgres 9.6
I want to get json out of my PostgreSQL database running version:
PostgreSQL 9.3.1 on x86_64-unknown-linux-gnu, compiled by gcc (GCC)
4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit
The data I am trying to get is like:
{
"a" : "value",
"b" : {
"c" : "some_vaue here"
}
}
I am getting value from one table and some_value from some other table using join.
How can I achieve that?
Here is something similar, but I'm getting an error:
QUERY="
SELECT row_to_json(o) FROM (
SELECT oltl.id::text as ordinal,
oltl.nid as code,
oltl.description as description,
SELECT row_to_json(j)
FROM ( SELECT cilantag.tag as code ) AS j
from olt_languages oltl
inner join ci_language_tags cilantag
on oltl.ci_language_tag_id=cilantag.id
) AS o";
The error I am getting is
ERROR: syntax error at or near "SELECT"
LINE 1: ...oltl.nid as code,oltl.description as description, SELECT row...
^ `
enter code here
A subquery in a select list has to be enclosed in brackets, try:
SELECT row_to_json(o)
FROM (
SELECT
oltl.id::text AS ordinal,
oltl.nid AS code,
oltl.description AS description,
( -- added
SELECT row_to_json(j)
FROM (
SELECT cilantag.tag AS code
) AS j
) AS tags -- + alias
FROM olt_languages oltl
INNER JOIN ci_language_tags cilantag
ON oltl.ci_language_tag_id=cilantag.id
) AS o
I have a query that i want it to be executed in a condition, for instance the api key .
Human :
If Api key is in the Api database, do the following query, say 'You Are Not Allowed' .
What i tried :
Select IF ( api.key = 'myapikey' , TrueQuery , 'You are not allowed') from api
My problem is in the query, i'm getting a lot of errors, the query contains " SELECT ... FROM ... WHERE ... GROUP BY ... LEFT JOIN " .
What's the way to accomplish it ?
Following example returns null :
SELECT CASE WHEN (SELECT api.app FROM api WHERE api.app = 'Test' )
THEN (SELECT items.rom_id FROM items)
END
Try this, without using IF, but using EXISTS.
select *
from ( TrueQuery ) t
where exists(select 1 from api where api.key = 'myapikey')
I have got an error "ERROR: subquery must return only one column " when I am runing this query:
INSERT INTO details (id, object_id, detail)
(
SELECT
CASE
WHEN (SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id))
THEN
concat(SUBSTRING(main_base.id, '(\d+.\d+.)'), n.counted :: TEXT, 'A')
ELSE
concat( SUBSTRING (main_base.id, '(\d+.\d+.)'), n.counted :: TEXT)
END AS id,
main_base.object_id,
main_base.details
FROM main_base
CROSS JOIN LATERAL
generate_series(1, COALESCE ((string_to_array(main_base.id, '-')) [2] :: INT, 1)) AS n (counted)
WHERE main_base.id LIKE '%-%' AND NOT main_base.details ~ '^\.\d+|\(\.\d+\)'
);
I have not clue what is wrong. I've read some topic that people had the same problem but still dont know how to fix it.
I think the problem is that:
SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)
Can return more than one row, so causes problems in the WHEN statement. It can return more than one row, as the subquery will return 1 every time the condition is met.
If you want to trigger the case statement based on when there exists some records in this set, could you use:
(SELECT COUNT(*) FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)) > 1
I am trying to use the following query in SQL Server
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
IIf(a.expiryDate > Now(), 'TRUE', 'FALSE') AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT *
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;
but always get the error
Error in list of function arguments: '>' not recognized.
Unable to parse query text.
How do I resolve it?
Like Martin Smith said you need to use a case statement. Also it looks like you are only using a couple of fields in the derived table therefor I would suggest not using *. I put a example below.
SELECT [AL].[Subscriptions].Id,
[AL].[Subscriptions].name,
[AL].[Subscriptions].description,
[AL].[Subscriptions].price,
[AL].[Subscriptions].iconFileName,
case when a.expiryDate > GetDate() then 'TRUE' else 'FALSE' end AS isSubsByUser
FROM [AL].[Subscriptions]
LEFT JOIN (SELECT expiryDate, itemid
FROM [AL].[UserSubscriptions]
WHERE userId = 13259) AS a
ON Subscriptions.Id = a.itemid;