Posts

Showing posts with the label SQL

SQL Analytic Functions

Image
Window functions play a very important part in analytic queries. I explore some of the window functions using Postgres 13.4. Window function defaults differ from Postgres to Redshift, although the results are the same. The OVER () default for Redshift is (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) . Postgres's default is (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) . When writing this post, Redshift hasn't implemented the RANGE . Therefore, I haven't mixed the two databases and only stuck to Postgres. This post is created using Jupyter Notebook. I've used Jupyter Magics. For example, the Jupyter SQL Magic result set was converted as Pandas data frame and plotted graph. Setup Jupyter Notebook and Postgres Windows functions RANGE and ROWS Functions Other important functions How to create a table with random data Generate Create pglia database using external SQLs Setup Jupyter Notebook and Postgres If you want to experiment with t...

PySpark Dataframe DSL basics

Image
In this blog post, I explore the PySpark DataFrame structured API and DSL operators. Typical tasks you can learn: Connection to remote PostgreSQL database Create DataFrame from above database using PostgreSQL Sample Database Create DataFrame using CSV (movieLens) files. In addition to that the equivalent SQL has been provided to compare with the DSL. Preperation Configure Database in the PySpark Aggreations DataFrame from a CSV file Spark SQL Preperation Setup the the environment mentioned in the blog post PySpark environment for the Postgres database 1 to execute the following PySpark queries on the Postgres Sample Database 2 . from pyspark.sql import SparkSession spark = SparkSession.builder.appName("Postgres Connection") \ .config("spark.jars", # add the PostgresSQL jdbc driver jar "/home/jovyan/work/extlibs/postgresql-9.4.1207.jar").getOrCreate() As shown in line# 4, I am using JDBC driver which is in my local macO...