I have a sql-query like this:
select
field_name,
field_value
from
my_table
where field_name not in (array_construct('A', 'B'))
The array_construct is something I get out of another column.
I want to use it as a filter with an in-condition.
Do you have any idea on how to do this?
I get an error message like: Can not convert parameter 'ARRAY_CONSTRUCT('A', 'B')' of type [ARRAY] into expected type [VARCHAR(120)]
Using ARRAY_CONTAINS function:
select
field_name,
field_value
from
my_table
where NOT ARRAY_CONTAINS(filed_name::VARIANT, array_construct('A', 'B'));
A more compact form using []
where not array_contains( [col1], [col2,col3] )
Related
I have a table that has JSON data stored and I'm using json_exists functions in the query. Below is my sample data from the column for one of the rows.
{"fields":["query.metrics.metric1.field1",
"query.metrics.metric1.field2",
"query.metrics.metric1.field3",
"query.metrics.metric2.field1",
"query.metrics.metric2.field2"]}
I want all those rows which have a particular field. So, I'm trying below.
SELECT COUNT(*)
FROM my_table
WHERE JSON_EXISTS(fields, '$.fields[*]."query.metrics.metric1.field1"');
It does not give me any results back. Not sure what I'm missing here. Please help.
Thanks
You can use # operator which refers to an occurrence of the array fields such as
SELECT *
FROM my_table
WHERE JSON_EXISTS(fields, '$.fields?(#=="query.metrics.metric1.field1")')
Demo
Edit : The above case works for 12R2+, considering that it doesn't work for your version(12R1), try to use JSON_TABLE() such as
SELECT fields
FROM my_table,
JSON_TABLE(fields, '$.fields[*]' COLUMNS ( js VARCHAR2(90) PATH '$' ))
WHERE js = 'query.metrics.metric1.field1'
Demo
I have no idea how to "pattern match" on the array element, but just parsing the whole thing and filtering does the job.
with t(x, json) as (
select 1, q'|{"fields":["a", "b"]}|' from dual union all
select 2, q'|{"fields":["query.metrics.metric1.field1","query.metrics.metric1.field2","query.metrics.metric1.field3","query.metrics.metric2.field1","query.metrics.metric2.field2"]}|' from dual
)
select t.*
from t
where exists (
select null
from json_table(
t.json,
'$.fields[*]'
columns (
array_element varchar2(100) path '$'
)
)
where array_element = 'query.metrics.metric1.field1'
);
In your code, you are accessing the field "query.metrics.metric1.field1" of an object in the fields array, and there is no such object (the elements are strings)...
I have data in this format:
[{"id":"b","type":"user"},{"id":"c","type":"system"}]
Would like to generate JSON message with only "id" selected, for example:
[{"id":"b"},{"id":"c"}]
So far I was only able to split them and remove "type", then concatenate with []
select json_array_elements_text(column1)::jsonb #- '{type}'
from (
select '[{"id":"b","type":"user"},{"id":"c","type":"system"}]'::json as column1
) t
Are there a better way to do it (I'm sure there is), please help thanks.
Edit:
There might be other properties in addition to "id" and "type" added in future, code will need to reference to "id" only.
[{"id":"b","type":"user"},{"id":"c","type":"system"}, {"id":"d","type":"system", "flag":"Y"}]
I have to advice next flow:
select array_agg(row_to_json(t.*)) from (
select id
from jsonb_to_recordset('[{"id":"b","type":"user"},{"id":"c","type":"system"}]'::jsonb) as x(id varchar, type varchar)
) t;
You can play SQL here
I found an answer that doesn't referencing other properties in JSON objects.
select json_agg(json_build_object('id', id)) as id
from (
select json_array_elements('[{"id":"b","type":"user"},{"id":"c","type":"system"}, {"id":"d","type":"system", "flag":"Y"}]'::json)::json->'id' as id
) t
Is there a way to do a partial string match on a string array column in postgres?
I'm trying the following syntax, but it is not working as I'd expect it to:
SELECT * FROM example_table WHERE '%partial string' ILIKE ANY(array_column)
Is there a correct way of doing this?
drop table if exists temp_a;
create temp table temp_a as
(
select array['alpha','beta'] as strings
union all
select array['gamma','theta']
);
select *
from (select unnest(strings) as string from temp_a) as sq
where string ilike '%eta'
You need to search each subscript separately -- here's an example that can be expounded on to include more columns
SELECT distinct array_column FROM
(SELECT array_column,
generate_subscripts(array_column, 1) AS s
FROM example_table) AS foo
WHERE array_column[s] like '%partial string';
Alternative hack:
select * from example_table where array_column::text like '%partial%'
if necessary you could hack "partial" to include the opening/closing brackets and quotes to be just a bit more precise.
Just implemented this using array_to_string:
SELECT * FROM example_table WHERE array_to_string(array_column) ILIKE '%partial string'
I have database which contains a field named groupid and group name.
sample data
groupid groupname
123 abc
234 bcr
1237 cde
I like to compare groupid with another inputted data, its size is greater than size of group id.
I tried a query that not return correct answer
SELECT *
FROM mydata
WHERE groupid LIKE '12309098';
My expected answer is abc
What are the changes to made for correct answer
thanks in advance
Since you want the value in the row to be the prefix of your input and not the other way around, you can just turn LIKE around the other way;
SELECT *
FROM mydata
WHERE '12309098' LIKE CONCAT(groupid, '%');
An SQLfiddle to test with;
EDIT: Since you asked about SQLite, there you need to use || for concatenation;
SELECT *
FROM mydata
WHERE '12309098' LIKE `groupid` || '%';
Another SQLfiddle.
You could do like below:
SELECT *
FROM my data
WHERE '12309098' LIKE CONCAT('%', groupid, '%');
You could use something like this. But it is highly subjective to your SQL Server. For Oracle following syntax should be fine
SELECT *
FROM mydata
WHERE groupid LIKE SUBSTR('12309098',1, LENGTH(groupid));
If you are sure that input data will start with 123, then:
SELECT * FROM mydata WHERE groupid LIKE '123%';
If you are sure that input data will contain 123, then:
SELECT * FROM mydata WHERE groupid LIKE '%123%';
I'm converting a SQL Server stored procedure to HiveQL.
How can I convert something like:
SELECT * FROM table1 WHERE id NOT IN (7,6,5,4,2,12)
NOT IN is now supported in Hive. See https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF.
Try this:
SELECT * FROM table1 WHERE NOT array_contains(array(7,6,5,4,2,12), id)
According to the documentation it says you can use not in:
The negated forms can be written as follows:
from DomesticCat cat where cat.name not between 'A' and 'B'
from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
Are you getting an error when you try your query in the question?
Please try based on the references as well.
Reference 1.
NOT IN Clause in HQL.