Code Cheatsheet

**Still being organized**

def main(n: int):
    return n+1

if __name__ == '__main__':
    print(f'value plus one= {main(6)}')

value plus one= 7
/************************************************************
*Say you want to do analysis based on two tables in a PostgreSQL database, 
*one containing census tracts while the other has block 
*attributes. Now you're tasked with retreiving the block in 
*each tract with the highest population density. One way 
*would be to use a partitioned table.
************************************************************/


SELECT t.id, t.tract, b.block, b.population_density
FROM censusdata.tracts t
/*The left join retains all the elements in your tract 
table while joining it up with any matching results 
from the sub query below*/
LEFT JOIN (
SELECT *
FROM (
    /*This sub query partitions/groups the census blocks
    on the 'tract' field and orders these smaller groups 
    in a decending fashion so that the top row (#1) has
    the most densely populated block*/
    SELECT *, 
        row_number() over (
            partition by tract
            ORDER BY population_density desc
        ) as sorted_block_popdensity
    FROM censusdata.blocks
    ) subquery
--Grab the first element of the ordered group
WHERE sorted_block_popdensity = 1
--Now tell it what field(s) to join the two tables on
) b on b.tract = t.tract
pgfunc