Skip to content

Instantly share code, notes, and snippets.

View tecmaverick's full-sized avatar

AbrahamJP tecmaverick

View GitHub Profile
@tecmaverick
tecmaverick / Redshift Interval DateAdd.sql
Created March 15, 2022 06:37
Redshift Interval DateAdd
-- Solution to use 'dateadd' function to add years, instead of 'interval'
WITH recursive numbers(n) as
(
SELECT 0 as n
UNION all
SELECT n + 1
FROM numbers n
WHERE n.n <= datediff(year,'2000-01-01'::date,trunc(current_date))
),
date_list as (
@tecmaverick
tecmaverick / Decoding Redshift SQL Error.txt
Last active March 15, 2022 15:40
Decoding Redshift SQL Error
Redshift outputs the following error, when SQL syntax error is encountered.
ERROR: syntax error at or near "," Position: 937
In error message, position 937 denotes the character where the error is identified. To locate this,
remove line feed from the SQL statement.
Removing line feeds via Visual Studio Code.
Paste the SQL statement, and open Find and Replace
MAC: Option + Command + F
Windows: Ctrl + H
@tecmaverick
tecmaverick / Redshift DateTime Operations.sql
Last active March 16, 2022 03:54
Redshift Date Time Operations
-- ************************************************************
-- Substract 10 days from 25 Jan 2018
SELECT '2018-01-25'::date - interval '10 day' val
-- ************************************************************
-- Substract date via interval
SELECT '2018-01-15'::date - interval '1 day' interval_val
SELECT '2018-01-15'::date - interval '1 week' interval_val
SELECT '2018-01-15'::date - interval '1 month' interval_val
SELECT '2018-01-15'::date - interval '1 year' interval_val
@tecmaverick
tecmaverick / Redshift Datatype Conversion.sql
Last active March 6, 2022 11:48
Redshift Datatype Conversion
SELECT integer '1' id;
SELECT timestamp '2010-10-31 01:00:00' test_datetime;
-- Concat integer to text. Outputs 12
SELECT 1::text || 2::text concat_demo
@tecmaverick
tecmaverick / Redshift Integer Sequence Generator.sql
Last active March 6, 2022 12:01
Redshift Integer Sequence Generator
-- Different ways to generate sequential Integer values in Redshift
-- Option 1: (The following statement is executed from leader node,
-- hence any join operation from tables stored in compute node may cause query to fail.)
SELECT * FROM generate_series(0,100);
-- Generates sequence of integer by incrementing two
SELECT * FROM generate_series(0,100,2);
-- Generates positive sequence of integers upto 11,110
@tecmaverick
tecmaverick / aws-redshift-copy-s3-role.md
Created January 31, 2022 05:24 — forked from nokernel/aws-redshift-copy-s3-role.md
allow Amazon Redshift to load data from S3 bucket in another AWS account and possibly in different AWS Region

Summary

This quick document highlight how allow Amazon Redshift to load data from S3 bucket in another AWS account and possibly in different AWS Region

Setup

AWS Account A
region us-west-2 -> S3 bucket my.bucket
@tecmaverick
tecmaverick / Cloud9 Custom AMI Prep
Created June 25, 2020 09:57
Cloud9 Custom AMI Prep
sudo yum update -y
#Python 2.7 installed
python --version
sudo yum -y install python36
sudo yum install python3 -y
#Install Node
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
. ~/.bashrc
@tecmaverick
tecmaverick / Install PHP7 AWS Cloud9
Last active June 25, 2020 09:56
Installing PHP7 AWS Cloud9
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
$ sudo yum install yum-utils -y
$ sudo yum update
$ sudo yum remove php*
$ sudo yum remove httpd*
$ sudo yum clean all
$ sudo yum upgrade -y
$ sudo yum install httpd24
$ sudo yum install php72 php72-mysqlnd php72-imap php72-pecl-memcache php72-pecl-apcu php72-gd
@tecmaverick
tecmaverick / Lambda_CLI_Commands.txt
Last active May 15, 2020 03:37
Lambda CLI Commands
#**********************************************************
#Upload binary file via AWS CLI v2
data=$(base64 < replace_with_filename.png)
aws lambda invoke \
--function-name replace_with_function_name \
--payload "{\"test\" : \"$data\"}" \
--cli-binary-format raw-in-base64-out \
output.txt
#---------------------------------------------------------
@tecmaverick
tecmaverick / LambdaListFunction.js
Last active May 15, 2020 13:59
Lambda Function to list all lambda functions. Using marker to page the results
const AWS = require('aws-sdk')
const lam = new AWS.Lambda()
async function getLambdaFuntions(marker)
{
let promise = new Promise(function(resolve, reject) {
var records = [];
var result = {"records":records,"marker":marker}
var params = {MaxItems: 100, FunctionVersion: 'ALL'}