In this demo, we will:
redshift-demo-bucket-468024
sale_id,customer_id,product_id,sale_date,quantity
6,3,1,2023-01-20,2
7,1,2,2023-01-21,1
8,2,3,2023-01-22,3
redshift-demo-cluster
awsuser
Awsuser123
cluster-subnet-group-1
cluster-subnet-group-1
demodb
5439
demodb
awsuser
Awsuser123
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(100),
signup_date DATE
);
INSERT INTO users (user_id, username, email, signup_date)
VALUES
(1, 'john_doe', 'john@example.com', '2023-01-15'),
(2, 'jane_smith', 'jane@example.com', '2023-02-20'),
(3, 'bob_johnson', 'bob@example.com', '2023-03-10'),
(4, 'alice_brown', 'alice@example.com', '2023-04-05'),
(5, 'charlie_davis', 'charlie@example.com', '2023-05-12');
SELECT * FROM users;
SELECT DATE_TRUNC('month', signup_date) AS month, COUNT(*) AS user_count
FROM users
GROUP BY DATE_TRUNC('month', signup_date)
ORDER BY month;
1. The DATE_TRUNC('month', signup_date) function is used to group dates by month.
2. The COUNT(*) function is used to count the number of users for each month.
SELECT
TO_CHAR(DATE_TRUNC('month', signup_date), 'Month') AS month,
COUNT(*) AS user_count
FROM users
GROUP BY DATE_TRUNC('month', signup_date)
ORDER BY DATE_TRUNC('month', signup_date);
Copy S3 Data Directly to Redshift Table
CREATE TABLE sales (
sale_id INTEGER PRIMARY KEY,
customer_id INTEGER,
product_id INTEGER,
sale_date DATE,
quantity INTEGER
);
COPY sales FROM 's3://redshift-demo-bucket-468024/sales_data.csv'
IAM_ROLE 'arn:aws:iam::651623850282:role/service-role/AmazonRedshift-CommandsAccessRole-20241020T104609'
region 'us-east-1'
CSV
IGNOREHEADER 1;
select * from sales;
delete