replace elements in a 2d array - postgresql

Given a 2d array
select (ARRAY[[1,2,3], [4,0,0], [7,8,9]]);
{{1,2,3},{4,0,0},{7,8,9}}
Is there a way to replace the slice at [2:2][2:] (the {{0,0}}) with values 5 and 6? array_replace replaces a specific value so I'm not sure how to approach this.

I believe it's more readable to code a function in plpgsql. However, a pure SQL solution also exists:
select (
select array_agg(inner_array order by outer_index)
from (
select outer_index,
array_agg(
case
when outer_index = 2 and inner_index = 2 then 5
when outer_index = 2 and inner_index = 3 then 6
else item
end
order by inner_index
) inner_array
from (
select item,
1 + (n - 1) % array_length(a, 1) inner_index,
1 + (n - 1) / array_length(a, 2) outer_index
from
unnest(a) with ordinality x (item, n)
) _
group by outer_index
)_
)
from (
select (ARRAY[[1,2,3], [4,0,0], [7,8,9]]) a
)_;

Related

How to add a label based on dense rank value [duplicate]

This question already exists:
Cannot when add ORDER BY in a CTE
Closed 9 months ago.
Is there any way I can reference the inner dense rank results and give them appropriate labels like I am trying to do? It seems like in T-SQL I just can NOT do an "Order by" in an inner query, it is producing an error like:
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
But then how do I attach appropriate labels to the dense rank results in the code below?
WITH basequery AS (
SELECT c.baseentityid,
c.team,
c.providerid,
-- rank() OVER (PARTITION BY c.baseentityid ORDER BY c.version DESC) AS rn,
t.vaccinedate,
t.vaccine,
t.vaccinesource,
t.num
FROM vr.child_registration c
CROSS APPLY ( VALUES (c.bcgsource,c.bcgdate,'bcg',1), (c.opv0source,c.opv0date,'opv0',2), (c.penta1source,c.penta1date,'penta1',3), (c.pcv1source,c.pcv1date,'pcv1',4), (c.rota1source,c.rota1date,'rota1',5), (c.opv1source,c.opv1date,'opv1',6), (c.penta2source,c.penta2date,'penta2',7), (c.pcv2source,c.pcv2date,'pcv2',8), (c.rota2source,c.rota2date,'rota2',9), (c.opv2source,c.opv2date,'opv2',10), (c.penta3source,c.penta3date,'penta3',11), (c.ipvsource,c.ipvdate,'ipv',12), (c.pcv3source,c.pcv3date,'pcv3',13), (c.opv3source,c.opv3date,'opv3',14), (c.measles1source,c.measles1date,'measles1',15), (c.tcvsource,c.tcvdate,'tcv',16), (c.ipv2source,c.ipv2date,'ipv2',17), (c.measles2source,c.measles2date,'measles2',18)) t(vaccinesource, vaccinedate, vaccine, num)
WHERE t.vaccinedate IS NOT NULL AND t.vaccinedate <> ''
)
SELECT aa.baseentityid,
aa.team,
aa.vaccinedate,
aa.vaccine,
aa.vaccinesource,
aa.num,
aa.providerid,
aa.visitrank,
CASE
WHEN aa.visitrank = 0 THEN 'external vaccination'
WHEN aa.visitrank = 1 THEN 'Enrollment'
ELSE 'Visitation'
END AS visittype,
CASE
WHEN aa.providerid LIKE '%vacc%' THEN 'Vacc'
ELSE 'Non-Vacc'
END AS providertype
FROM ( SELECT a.baseentityid,
a.team,
a.vaccinedate,
a.vaccine,
a.vaccinesource,
a.num,
a.providerid,
CASE
WHEN a.vaccinesource = 'vaccinatoradministered' THEN dense_rank() OVER (PARTITION BY a.baseentityid, (
CASE
WHEN a.vaccinesource = 'vaccinatoradministered' THEN 1
ELSE 0
END) ORDER BY (CONVERT(VARCHAR(10),a.vaccinedate,111)) )
ELSE 0
END AS visitrank
FROM basequery a
WHERE a.rn = 1
GROUP BY a.baseentityid, a.team, a.vaccinedate, a.vaccine, a.vaccinesource, a.num, a.providerid
ORDER BY a.baseentityid, a.num) aa;

Multiple AND statements in a FileMaker calculation

I want to calculate if a value falls outside of 10% of the last two values added to a database. This calculation is not giving me correct feedback when I have 'Weight" values close to 10, or from 100-110. Otherwise it works fine.
Case (
Get(RecordNumber) ≤ 2 ; "Continue Collecting Data" ;
(((GetNthRecord ( Weight ; Get(RecordNumber)-2))*.9) ≤ Weight) and
(((GetNthRecord ( Weight ; Get(RecordNumber)-2))*1.1) ≥ Weight) and
(((GetNthRecord ( Weight ; Get(RecordNumber)-1))*.9) ≤ Weight) and
(((GetNthRecord ( Weight ; Get(RecordNumber)-1))*1.1) ≥ Weight);
"Stable";
"Unstable")
I’m going to start with the assumption that your table includes fields for both the primary key and the creation timestamp. If not, I would highly recommend adding both to this table and every other table you create.
Assuming these fields are in place, you need to create another occurrence of the table on which this layout is based, then relate the primary key to itself via a Cartesian (×) join. Sort the relationship by creation timestamp descending. Your calculation is then:
Case (
(((GetNthRecord ( Weight ; 1 ) *.9 ) ≤ Weight) and
(((GetNthRecord ( Weight ; 1 ) *1.1 ) ≥ Weight) and
(((GetNthRecord ( Weight ; 2 ) *.9 ) ≤ Weight) and
(((GetNthRecord ( Weight ; 2 ) *1.1 ) ≥ Weight);
"Stable";
"Unstable")
Another thing I noticed is that your code is kind of complex. The Let function can make things easier to read, and your four criteria can be cut down to whether either difference is out of range. So, a simpler version becomes:
Let ( [
#weight1 = GetNthRecord ( all::weight ; 1 ) ;
#weight2 = GetNthRecord ( all::weight ; 2 )
] ; //end define Let
Case (
Abs ( #weight1 - weight ) > .1 ; "Unstable" ;
Abs ( #weight2 - weight ) > .1 ; "Unstable" ;
"Stable"
) //end Case
) //end Let
Does that help?
Assuming you are using FileMaker v12 or later, this looks like a good place to use the ExecuteSQL function (not the script step) to retrieve the last two values. You could do something like this:
Let (
sqlQuery = "
SELECT t.weight
FROM MyTable t
WHERE t.id <> ?
ORDER BY t.id DESC
FETCH FIRST 2 ROWS ONLY
" ;
ExecuteSQL ( sqlQuery ; "" ; "" ; MyTable::id )
)
This query assumes you have a unique 'id' field (i.e. a primary key) that's defined as an 'auto-enter serial' value. The WHERE clause makes sure that the current record (presumably the one the user is entering) is not included in the query. The ORDER BY DESC clause forces the last two records to the top where we can fetch the 'weight' values easily into a value list.
Assuming you use a 'Set Variable' script step to put the query results into $lastValues, you can then test for whether they are in range like so:
Let ( [
lastValue1 = GetValue ( $lastValues ; 1 ) ;
lastValue2 = GetValue ( $lastValues ; 2 ) ;
Value1_InRange = lastValue1 - Abs ( lastValue1 - weight ) >= ( 0.9 * lastValue1 ) ;
Value2_InRange = lastValue2 - Abs ( lastValue2 - weight ) >= ( 0.9 * lastValue2 )
] ;
Value1_InRange and Value2_InRange // returns 1 if both values within range, 0 if not
)
If I were doing this, I would put the above range-checking code into a custom function so it's generic and can be easily reused:
IsWithinRange ( valueToTest ; lastValue ; range ) =
lastValue - Abs ( lastValue - valueToTest ) >= ( ( 1 - range ) * lastValue )
Then the above range-checking code can be reduced to:
IsWithinRange ( MyTable::weight ; GetValue ( $lastValues ; 1 ) ; 0.1 ) &
IsWithinRange ( MyTable::weight ; GetValue ( $lastValues ; 2 ) ; 0.1 )
And one last note.. if you use the ExecuteSQL function in a calculated field, be sure to make it 'unstored' so that it only executes when needed. However, I would recommend you avoid that altogether and simply call it from a script step like 'Set Variable'. That way you can control exactly when it executes.
Hope that helps!

Generate series for various mixes of numbers and letteres

I am using this syntax:
generate_series(1, COALESCE((string_to_array(table.id_number, '-')) [2] :: INT, 1)) AS n (numbers)
To generate IDs in elements that have got ID like 32.22.1-4 to get 4 rows with IDs 32.22.1, 32.22.2, 32.22.3 and 32.22.4. How to change it to accept also letters?
So for 32.22.a-c there would be:
32.22.a, 32.22.b, 32.22.c
And for 32.22.d1-d4 there would be
32.22.d1, 32.22.d2, 32.22.d3, 32.22.d4
EDIT:
The whole code looks like:
INSERT INTO ...
(
SELECT
...
FROM table
CROSS JOIN LATERAL
generate_series(1, COALESCE((string_to_array(table.id_number, '-')) [2] :: INT, 1)) AS n (numbers)
WHERE table.id_number LIKE ...
);
WITH t(id_number) AS ( VALUES
('32.33.a1-a5'::TEXT),
('32.34.a-c'::TEXT),
('32.35.b-e'::TEXT)
), stats AS (
SELECT
chars,
chars[1] pattern, -- pattern use
CASE
WHEN (ascii(chars[3]) - ascii(chars[2])) = 0
THEN FALSE
ELSE TRUE
END char_pattern, -- check if series of chars
CASE
WHEN (ascii(chars[3]) - ascii(chars [2])) = 0
THEN right(chars[3],1)::INTEGER
ELSE (ascii(chars[3]) + 1 - ascii(chars[2]))::INTEGER
END i -- number of series
FROM t,
regexp_matches(t.id_number, '(.*\.)(\w*)-(\w*)$') chars
)
SELECT
CASE WHEN char_pattern
THEN pattern || chr(ascii(chars[2]) - 1 + s)
ELSE pattern || left(chars[2],1) || s::TEXT
END output
FROM stats,generate_series(1,stats.i) s;
Result:
output
---------
32.33.a1
32.33.a2
32.33.a3
32.33.a4
32.33.a5
32.34.a
32.34.b
32.34.c
32.35.b
32.35.c
32.35.d
32.35.e
(12 rows)

Trouble with sums and a having clause

I have a query that populates a report of "written off" hours, the total of which is the sum RegHrs, OvtHrs, and SpecialOvtHrs, but only those values with a positive value (each of these fields may have positive and negative values - positive values are "written off").
The query I am using (which doesn't work) is:
select
LD.Employee,
max(EM.LastName + ', ' + Em.FirstName) as EMName,
LD.WBS1, LD.WBS2,
sum(LD.RegHrs + LD.OvtHrs + LD.SpecialOvtHrs) as [Hours],
CL.Name as ClientName, pctf.CustProgram,
max(PR.Name) as ProjName,
LD.PKey, ISNULL(BillingDirectives.Comment, 'None')
from AnvilProd..LD
left join AnvilProd..PR on LD.WBS1 = PR.WBS1 and PR.WBS2 = ' ' and PR.WBS3 = ' '
left join AnvilProd..EM on LD.Employee = EM.Employee
left join AnvilProd..CL on PR.ClientID = CL.ClientID
left join AnvilProd..ProjectCustomTabFields pctf on PR.WBS1 = pctf.WBS1 and pctf.WBS2 = ' ' and pctf.WBS3 = ' '
left join InterfaceDev..BillingDirectives on BillingDirectives.PKey = LD.PKey
where LD.BillStatus = 'X'
and LD.WrittenOffPeriod = #custPeriod
and LD.WBS1 not in (select distinct WBS1 from AnvilProd..BT where FeeBasis = 'L')
and LD.WBS1 not in (select distinct WBS1 from InterfaceDev..CircledHoursReportEliminatedJobs where ActiveStatus = 'Active')
group by pctf.CustProgram, CL.Name, LD.WBS1, LD.WBS2, LD.Employee, BillingDirectives.Comment, LD.PKey
-- having ((sum(LD.RegHrs) > 0) or (sum(LD.OvtHrs) > 0) or (sum(LD.SpecialOvtHrs) > 0))
order by pctf.CustProgram, CL.Name, LD.WBS1, WBS2, EMName
I need to find written off hours for each Employee, WBS1, WBS2 combination.
I have tried a dozen different things with that having clause and cannot get it to give me an accurate result.
Use a case inside the sum():
select blah, blah,
sum(
case when LD.RegHrs > 0 then LD.RegHrs else 0 end +
case when LD.OvtHrs > 0 then LD.OvtHrs else 0 end +
case when LD.SpecialOvtHrs > 0 then LD.SpecialOvtHrs else 0 end
) as [Hours], blah, blah
from blah join blah ...
group by blah, blah
-- no having clause
As a mathematical curiosity, you could also code the sum this way:
sum((LD.RegHrs + abs(LD.RegHrs) +
(LD.OvtHrs + abs(LD.OvtHrs) +
(LD.SpecialOvtHrs + abs(LD.SpecialOvtHrs)) / 2
which although is a bit less readable, it uses less code and may impress your colleagues more :)

Is it possible to refactor this statement to remove the subquery?

I'm trying to take data from one column, MyTable.SSN, and copy it to another in the same table, MyTable.SSNWithDashes, just formatted differently. If MyTable.SSN doesn't have exactly 9 digits, I don't care to process it at all.
I've tried this:
IF( SELECT LEN( [SSN] ) FROM [MyTable] ) = 9
UPDATE [MyTable] SET [SSNWithDashes] = LEFT( [SSN], 3 ) + '-' + SUBSTRING( [SSN], 4, 2 ) + '-' + RIGHT( [SSN], 4 )
ELSE
UPDATE [MyTable] SET [SSNWithDashes] = NULL
While this works, it throws an error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
While I do understand what the warning is saying, I'm not really sure how to go about this differently.
How can I refactor this to remove that warning (and perhaps read a little cleaner)?
UPDATE dbo.[MyTable]
SET [SSNWithDashes] = CASE
WHEN LEN(SSN) = 9 THEN
LEFT([SSN],3) + '-' + SUBSTRING([SSN],4,2) + '-' + RIGHT([SSN],4)
ELSE NULL
END;
Assuming your SSNWithDashes is already NULL then
UPDATE dbo.[MyTable]
SET [SSNWithDashes] = LEFT([SSN],3) + '-' + SUBSTRING([SSN],4,2) + '-' + RIGHT([SSN],4)
WHERE LEN(SSN) = 9
Every other rows remain NULL