FreakDJ
12/03/2021, 3:02 AMhtml
<tr>
<th>Collection Name</th>
<th>Current Floor Price</th>
<th>Previous Floor Price</th>
<th>Volume</th>
</tr>
So, I need to group by collection name and then order by timestamp and get the two most recent prices to put into the table (current and previous).
What would the SQL for something like this look like? Hopefully I worded this properly enough to portray what I am trying to accomplish!FreakDJ
12/03/2021, 3:04 AMjavascript
const test = async () => {
let { data, error } = await supabase
.from("SolanaFloorTracker")
.select("*")
.order("CollectionName", { ascending: true })
.order("created_at", { ascending: false });
if (error) {
console.error(error);
}
console.log(data);
};
however, it returns data in a rather inefficient way that I would need to use a lot JavaScript and processing time to sort through the arrays and get what I need out of it which wouldn't be good in the long run when lots more data comes in.tourdownunder
12/03/2021, 5:57 AMtourdownunder
12/03/2021, 6:18 AMsql
create view SolanaFloorTrackers AS
select psft.id, psft."timestamp", psft.name, psft.price, psft.volume
FROM (
SELECT *, rank() over (partition by (sft.name) order by sft."timestamp" desc) as rank
from SolanaFloorTracker sft
) as psft
where psft.rank in (1,2)
edit: need to alias the inner query.tourdownunder
12/03/2021, 6:18 AMtourdownunder
12/03/2021, 8:24 AMlag
instead of rank
here as one of the other postgresql Window Function available to you.
Though after a quick look I couldn't get my head around how it would work. I'll try another way using rank.
sql
create view SolanaFloorTrackers AS
-- this is a CTE
with psft as (
SELECT *, rank() over (partition by (sft.name) order by sft."timestamp" desc),
select
c.id , c."timestamp", c.name, c.price, c.volume, p.price, p.volume
from psft c. -- c for current
left outer join psft p ON c.name = p.name. -- p for previous
where c.rank = 1 and p.rank =2;
FreakDJ
12/03/2021, 11:41 AMtourdownunder
12/03/2021, 8:03 PMFreakDJ
12/04/2021, 3:16 AMFreakDJ
12/04/2021, 3:16 AMFreakDJ
12/04/2021, 3:16 AMtourdownunder
12/04/2021, 3:44 AMcomedies
example in the [postgres docs](https://www.postgresql.org/docs/13/sql-createview.html) is a simplified example
Its just a way to pre bake a query I guess to decrease complexity.tourdownunder
12/04/2021, 3:46 AM