How to get the second highest number from an array? - swift

I have two arrays:
var arrPrice = [500, 400, 300, 300, 200, 100, 100]
var arrId = [1,2,3,4,5,6,7]
I need the output for id 1 price should be 400, for id 2 price should be 300, for id 3 price should be 200, for id 4 price should be 200, for id 5 price should be 200, for id 6 price should be 100 and last id and price should be same like for id 7 price should be 100. How do I get the values like these.

Try a Sort
Please add some code what you've tried and I can edit this post with further help. Although this seems like this is a CS100 assignment early in the semester. :P

Related

Postgres, Supabase and Math

I am new to Supabase and I know it uses Postgres. I have database containing 2 tables (for now):
meals
days of diet
meals contain:
name, photo, calories, protein, carbs etc.
days of diet contain:
array with meals id, total calories, total proteins, total carbs etc.
Can I set Postgres to do the math for me? Something like:
day of diet calories: sum all calories of array with meals
Also, since I can't have foreign key array it would have to be something like:
day of diet calories: for each (get meal with id) calories sum it up
Thank you for answering - I know doing it first method is possible but I don't think it's the best way. Hope you have a great day.
I can use my "backend" to add meal to day of diet, take "total calories" and add new meal calories to it. It is working great BUT what if I edit meal? I change calories? Then day of diet is wrong. I could code my backend to "refresh" data of every day of diet containing edited meal but it seems like a slow approach.
I almost finished this with Postgres Functions and Triggers :P Going well, will post an answer once I finish.
BEGIN
UPDATE ingredient
SET iname = NEW.iname,
calories = NEW.calories_per_100 * ingredient.grams / 100,
protein = NEW.protein_per_100 * ingredient.grams / 100,
carbs = NEW.carbs_per_100 * ingredient.grams / 100,
typ = NEW.typ
WHERE ingredient = NEW.id;
RETURN NEW;
END

update several columns of a table at once

In postgresql, I want to update several rows of a table according to their id. This doesn't work:
UPDATE table SET othertable_id = 5 WHERE id = 2, 45, 22, 75
What is the correct syntax for this?
Use an IN operator:
update the_table
set othertable_id = 5
where id in (2,45,22,75);

Selecting/Inner Joining multiple values from one table into a single row

I've been searching google all afternoon I'm not even sure how I would word the question I have so that google or bing might understand it. So I have 2 tables(the tables and the database setup were not my doing) one is the details of a product
name, value, weight, weight unit of measure id, length, width, height, dimensions unit of measure id, description
Then another table with the units of measure and their ID values
eg.
id, unit name
1, cm
2, inches
3, lbs
4, kg
5, feet
I need to select from the products table, but replace the unit of measure ID's with the actual unit of measure text from the other table.
the data looks like
sm widgets, $10, 20, 3, 10, 10, 10, 1, small widgets
I want the results to come out like
sm widget, $10, 20, lbs, 10, 10, 10, cm, small widgets
lg widget, $15, 50, kg, 10, 10, 10, inches, large widgets
thanks for any insight you guys can give me
I think you just need to join the tables and return the description:
select
p.name,
p.value,
p.weight,
c.[unitname],
p.length,
p.width,
p.height,
c2.[unitname] as DimensionUnitName,
p.[description]
from products p
inner join unitcodetable c
on c.id = p.[weight unit of measure id]
inner join unitcodetable c2
on c2.id = p.[dimensions unit of measure id]

postgresql postgis : defining a new subzone index consistent with the old one

Here is my problem: I had a polygon layer with an index which looks like this:
id, population
100, 26
200, 12
300, 45
...
I edited the polygon layer and divided some of the polygons into smaller polygons (approximately 3-7 subpolygons). I already took care of having my data splitted between subzones (according to population density). So now I have this:
id, population
100, 22
100, 1
100, 3
200, 6
200, 6
I would like to create a new index that reflects the old one. For instance:
oldId, newId, population
100, 100, 22
100, 101, 1
100, 102, 3
200, 200, 6
200, 201, 6
Things I tried:
Defining a sequence:
DROP SEQUENCE IF EXISTS increment_id;
CREATE TEMP SEQUENCE increment_id INCREMENT BY 1 MINVALUE 0;
SELECT
id,
id+nextval('increment_id') AS new_id
FROM polygon_mapping WHERE id = 100;
THis works well for a single id to rename (the WHERE clause), but I don't know how to restart the sequence for every id.
I made some thinking around using the 'lag' function to compare current id with previous id. But I don't manage make it work.
Any suggestions?
Thank you
ps: I went through
Reset auto increment counter in postgres
where they reset the SEQUENCE but I don't manage to make it work in a SELECT clause.
Maybe using generate_series()?
SELECT id, generate_series(id,id + count(*)) AS newid
FROM polygon_mapping GROUP BY id;
If you want to select additional attributes, use a subquery and group the attributes using array_agg, than select the values from the array in the primary query:
SELECT id,
generate - 1 + id AS newid,
population_array[generate]
FROM (
SELECT id,
generate_series(1,count(*)) AS generate,
array_agg(population) AS population_array
FROM polygon_mapping GROUP BY id
) AS foo ORDER BY newid,id;

SUM with specific condition in crystal report

Basicalyy i want to sum data by certain condition.These are example of my data :
Group 1 - Value:
10,
20,
30,
Group 2-Value:
10,
20,
30,
Group 3-Value:
10,
20,
30,
I want to sum group 1 and group 3 only.How can do it,like in sql we can write select sum(group) from table1 where group <>'Group 2'.How can we do that using formula.Or there's other way to do it.