PSQL Apply mapping to an array - postgresql

I have an array of an enum type order_options that gets passed through to a function and it is pretty simple, it looks like this:
'{master_date, e_name}'
I have 6 possible values and the enum type looks like this:
create type order_options as enum ('master_date','e_name','r_data','n_count','creation_date','last_updated_on');
In the function however, I want to apply a mapping to these values to change the value of sort_params which is the array passed through.
For each enum, I have an 'alternative name' that I want to use in the order by clause of a subsequent select statement. For example:
'master_date' = o.master_date
'e_name' = d.e_name
and so on.
I've looked in to doing a replace whereby I loop through each element of the array and attempt a replace with each mapping but it gets pretty messy and complex.
So for example, this works for one individual mapping:
select array_replace('{master_date}'::text[],'master_date','o.master_date');
I'd like my sort_params to look like this after mapping:
'{o.master_date, d.e_name}'
Is there any easier way to do this?

For now, this is what I have:
select array_replace(sort_params_u,'master_date','o.master_date') into sort_params_u;
select array_replace(sort_params_u,'e_name','d.e_name') into sort_params_u;
etc

Related

How to extract the value from a json object in Azure Data Factory

I have my ADF pipeline, Where my final output from set variable activity is something like this {name:test, value:1234},
The input coming to this variable is
{
"variableName": "test",
"value": "test:1234"
}
The expression provided in Set variable Item column is #item().ColumnName. And the ColumnName in my JSon file is something like this "ColumnName":"test:1234"
How can I change it so that I get only 1234. I am only interested in the value coming here.
It looks like you need to split the value by colon which you can do using Azure Data Factory (ADF) expressions and functions: the split function, which splits a string into an array and the last function to get the last item from the array. This works quite neatly in this case:
#last(split(variables('varWorking'), ':'))
Sample results:
Change the variable name to suit your case. You can also use string methods like lastIndexOf to locate the colon, and grab the rest of the string from there. A sample expression would be something like this:
#substring(variables('varWorking'),add(indexof(variables('varWorking'), ':'),1),4)
It's a bit more complicated but may work for you, depending on the requirement.
It seems like you are using it inside of an iterator since you got item but however, I tried with a simple json lookup value
#last(split(activity('Lookup').output.value[0].ColumnName,':'))

Update - Where Clause Using Text Not Responding As Expected

I have a table that contains a column named 'time_source', with five potential values:
'model', 'country_city', 'region', 'update', and 'storage'
I've been attempting to make an update statement that will not update column date_destination if the time_source is 'model' like this:
update t_vessel_list_ballast
set date_destination = date_depart + voyage_time
where time_source not like 'model';
But it isn't working as i'd expect. instead, it's overwriting the date_destination even when 'model' is in the time_source. I tried something like this:
update t_vessel_list_ballast
set date_destination = date_depart + voyage_time
where time_source like 'country_city'
or time_source like 'region';
but i'm still getting the same results.
There is no other place in the model where this could be happening.
Why am i not getting the result i'm expecting? How do i solve this to get what I want?
I think with like, if you want to match rows (or not match rows like you do here), you'll also need %s on the pattern to show where to match any other characters. For example like '%match' will match any string ending with match, and like 'match%' will match strings starting with match. You can also combine them to match a string containing match with like '%match%'.

(kdb+/q) append to dictionary

I am trying to programmatically construct arguments to functional select call having the form:
?[ `t; () ; groupBy; ()]
The problematic part is groupBy, which should be a dictionary.
Suppose the objective is to arrive at the parse tree in the form:
parse "select by sym,month:`date$dt.month from t"
I start constructing the by part with:
groupBy: enlist[`sym]!enlist(`sym)
Then I try to append the month part of the group by statement (note that periodicity is parameterised):
per: `month / monthly periodicity
groupBy,: {enlist[x]!enlist[ $[x = `day;
`dt;
$[x=`month;
((parse "select by month:`date$dt.month from x")#3)#`month
;` sv (`dt,x)]
]]
}[per]
However, that throws type error. What goes wrong?
I think it doesn't like the compound assignment
groupBy,:{...}
Try
groupBy:groupBy,{...}
The difference is that in the first case it's trying to directly alter the data in memory without creating a copy, whereas in the second case it is creating a copy of the data in memory and then re-assigning it to that variable. Perhaps the compound assignment only works when the types are uniform

How do I get the contents of all of the nodes in a root node with SQL and XQuery?

I have the following table structure:
CREATE TABLE SpecialTable
(
Key UNIQUEIDENTIFIER,
XMLField VARCHAR(MAX)
)
In the first tuple:
Key = "28384841-17283848-7836-18292939"
XMLField =
"<RootNode>
<ForeignKey>92383829-27374848-1298-19283789</ForeignKey>
<ForeignKey>47585853-27374848-4759-19283939</ForeignKey>
<ForeignKey>37383829-27374848-3747-19283930</ForeignKey>
</RootNode>"
In another tuple, I see:
Key = "89984841-17283848-7836-18292939"
XMLField =
"<RootNode>
<ForeignKey>92383829-27374848-1298-19283789</ForeignKey>
<ForeignKey>37383829-27374848-3747-19283930</ForeignKey>
</RootNode>"
In a further tuple, I see:
Key = "11114841-17283848-7836-18292939"
XMLField =
"<RootNode>
<ForeignKey>37383829-27374848-3747-19283930</ForeignKey>
</RootNode>"
What I need to do is to get the following dataset out:
Key ForeignKey
28384841-17283848-7836-18292939 92383829-27374848-1298-19283789
28384841-17283848-7836-18292939 47585853-27374848-4759-19283939
28384841-17283848-7836-18292939 37383829-27374848-3747-19283930
89984841-17283848-7836-18292939 92383829-27374848-1298-19283789
89984841-17283848-7836-18292939 37383829-27374848-3747-19283930
11114841-17283848-7836-18292939 37383829-27374848-3747-19283930
I must say that this is a simplified data set and that the data was more complex than this and I have got to a point where I cannot get any further.
I have tried this:
SELECT sp.Key,
x.XmlCol.Query('.')
FROM SpecialTable AS sp
CROSS APPLY sp.XMLField.nodes('/RootNode') x(XmlCol)
However, it seems just to show the Key and the whole of the XML of the XMLField.
Also, I tried this:
SELECT sp.Key,
x.XmlCol.Query('ForeignKey[text]')
FROM SpecialTable AS sp
CROSS APPLY sp.XMLField.nodes('/RootNode') x(XmlCol)
And I get only the first value in the first ForeignKey node and not the others.
What am I doing wrong?
Kindest regards,
QuietLeni
First of all - if your data looks like XML, quacks like XML, smells like XML - then it IS XML and you should use the XML datatype to store it!
Also: be aware that Key is a very generic term, and also a T-SQL reserved keyword, so it makes for a really bad column name - use something more meaningful that doesn't clash with a keyword!
Once you've done that, you should be able to use this code to get your desired results:
SELECT
[Key],
ForeignKey = xc.value('.', 'varchar(50)')
FROM
dbo.SpecialTable
CROSS APPLY
XMLField.nodes('/RootNode/ForeignKey') AS XT(XC)
This will only work if you column XMLField is of XML datatype !! (which it really should be anyway)

PostgreSQL CASE: position within text array

I am currently successfully using a CASE expression to update an empty column based on attributes from other columns. For example
UPDATE table SET cat = CASE
WHEN term = '{"Boulder"}' then 'Boulder'
However, I need to do the same but on an text array and particularly when an element is in a specific position within that array.
For example if the data looks like
{"Boulder, Tree, Bush"}
WHEN position('Tree' in term) > 0 then 'Boulder'
But I receive an error message
function pg_catalog.position(character varying[], unknown) does not exist
I have used position in a function before so I am not sure why PostgreSQL does not like it in this situation.
Is there a way to using a CASE expression whilst determining the position of a text element within an array.
Apparently your term column is defined as an array, e.g. varchar[]. The position function only works with scalar values, not with arrays.
If you want to test if an element is contained in an array you need to use a different operator: #>
update foobar
set cat = 'Boulder'
where term #> '{"Boulder"}'
The expression '{"Boulder"}' creates an array with a single element. It's equivalent to array['Boulder'] (which I find more readable). So the above where condition updates all rows where the array term contains all elements of the array on the right hand side of the operator. In this case it's only a single element you are testing for.
More details about the array functions and operators can be found in the manual: http://www.postgresql.org/docs/current/static/functions-array.html
Edit after the requirements have changed
To find and update only those where boulder is in the first, second or third place, you can use this:
update foobar
set cat = 'Boulder'
where 'Boulder' in (term[1], term[2], term[3]);