Upload 4
Upload 4
Implementation of all dimension table and fact table based on case study.
Perform OLAP operations: Slice, Dice, Rollup, Drilldown using PostgreSQL.
Theory:
Slice, Dice, Rollup, and Drilldown are fundamental operations in Online Analytical
Processing (OLAP) used for exploring and analyzing multidimensional data stored in
data warehouses. These operations help users examine data from various perspectives
to gain insights.
1. Slice
2. Dice
3. Rollup
4. Drilldown
● Definition: The "Drilldown" operation is the reverse of rollup. It moves from
summary data to more detailed levels, allowing users to explore finer details of
the data by going down the hierarchy.
● Example: Starting with "annual sales" data, a drilldown could break it down to
"quarterly sales," and further down to "daily sales" to examine granular trends or
outliers.
Code:
Insert values
1. Company:
INSERT INTO public."Company" ("Company_name", "Address", "Phone_no") VALUES
('PharmaCorp', '123 Health St, Wellness City', '555-0101'),
('MediCare Inc.', '456 Treatment Rd, Cure Town', '555-0102'),
('BioMed Ltd.', '789 Healing Ave, Remedyville', '555-0103'),
('HealthPlus', '101 Wellness Blvd, Healthburg', '555-0104'),
('CureTech', '202 Care Ln, Pharma City', '555-0105'),
('MediLabs', '303 Research Dr, Medicine City', '555-0106'),
('PharmaSolutions', '404 Drug St, Healthland', '555-0107'),
('WellnessWorks', '505 Therapy Blvd, Healthy Town', '555-0108'),
('BioHealth', '606 Remedy Rd, Cure City', '555-0109'),
('VitalPharm', '707 Cure St, Wellness City', '555-0110');
2. Consumer:
INSERT INTO public."Consumer" ("Consumer_ID", "Consumer_name", "Consumer_Address",
"Phone_no") VALUES
(1, 'John Doe', '101 Elm St, Springfield', '555-1001'),
(2, 'Jane Smith', '202 Oak St, Shelbyville', '555-1002'),
(3, 'Robert Johnson', '303 Pine St, Capital City', '555-1003'),
(4, 'Emily Davis', '404 Maple St, Rivertown', '555-1004'),
(5, 'Michael Brown', '505 Cedar St, Lakeview', '555-1005'),
(6, 'Mary Wilson', '606 Birch St, Hilltown', '555-1006'),
(7, 'David Moore', '707 Walnut St, Forestville', '555-1007'),
(8, 'Sarah Taylor', '808 Fir St, Creekside', '555-1008'),
(9, 'James Anderson', '909 Spruce St, Meadowbrook', '555-1009'),
(10, 'Laura Martinez', '1010 Redwood St, Greenfield', '555-1010');
3. Drug :
INSERT INTO public."Drug" ("Drug_ID", "Drug_name", "Category_name", "Description")
VALUES
(1, 'Aspirin', 'Pain Reliever', 'Used to reduce fever and relieve pain'),
(2, 'Ibuprofen', 'Anti-Inflammatory', 'Reduces inflammation and pain'),
(3, 'Amoxicillin', 'Antibiotic', 'Treats bacterial infections'),
(4, 'Metformin', 'Diabetes', 'Helps control blood sugar levels'),
(5, 'Lisinopril', 'Blood Pressure', 'Used to treat high blood pressure'),
(6, 'Simvastatin', 'Cholesterol', 'Lowers cholesterol levels'),
(7, 'Omeprazole', 'Antacid', 'Reduces stomach acid'),
(8, 'Cetirizine', 'Antihistamine', 'Relieves allergy symptoms'),
(9, 'Hydrochlorothiazide', 'Diuretic', 'Treats high blood pressure and fluid retention'),
(10, 'Lorazepam', 'Anxiolytic', 'Treats anxiety and sleep disorders');
4. Time:
INSERT INTO public."Time" ("Date", "Day", "Week", "Month", "Year") VALUES
('2024-08-01', 'Thursday', '31', 'August', 2024),
('2024-08-02', 'Friday', '31', 'August', 2024),
('2024-08-03', 'Saturday', '31', 'August', 2024),
('2024-08-04', 'Sunday', '31', 'August', 2024),
('2024-08-05', 'Monday', '31', 'August', 2024),
('2024-08-06', 'Tuesday', '31', 'August', 2024),
('2024-08-07', 'Wednesday', '31', 'August', 2024),
('2024-08-08', 'Thursday', '31', 'August', 2024),
('2024-08-09', 'Friday', '31', 'August', 2024),
('2024-08-10', 'Saturday', '31', 'August', 2024);
5. Fact:
INSERT INTO public."Fact" ("Drug_ID", "Company_ID", "Customer_ID", "UnitPrice", "Quantity",
"Date") VALUES
(1, 1, 1, 10, 2, '2024-08-01'),
(2, 2, 2, 15, 1, '2024-08-02'),
(3, 3, 3, 20, 3, '2024-08-03'),
(4, 4, 4, 25, 4, '2024-08-04'),
(5, 5, 5, 30, 2, '2024-08-05'),
(6, 6, 6, 35, 5, '2024-08-06'),
(7, 7, 7, 40, 6, '2024-08-07'),
(8, 8, 8, 45, 1, '2024-08-08'),
(9, 9, 9, 50, 3, '2024-08-09'),
(10, 10, 10, 55, 4, '2024-08-10');
Roll up:
SELECT
t."Date",
d."Drug_name",
SUM(f."UnitPrice" * f."Quantity") AS Total_Sales
FROM public."Fact" f
JOIN public."Drug " d ON f."Drug_ID" = d."Drug_ID"
JOIN public."Time" t ON f."Date" = t."Date"
GROUP BY t."Date", d."Drug_name";
Slice:
SELECT
d."Drug_name",
SUM(f."UnitPrice" * f."Quantity") AS Total_Sales
FROM public."Fact" f
JOIN public."Drug " d ON f."Drug_ID" = d."Drug_ID"
JOIN public."Company" c ON f."Company_ID" = c."Company_ID"
WHERE c."Company_name" = 'PharmaCorp'
GROUP BY d."Drug_name";
Dice:
SELECT
d."Drug_name",
t."Date",
SUM(f."UnitPrice" * f."Quantity") AS Total_Sales
FROM public."Fact" f
JOIN public."Drug" d ON f."Drug_ID" = d."Drug_ID"
JOIN public."Time" t ON f."Date" = t."Date"
WHERE t."Date" BETWEEN '2024-08-01' AND '2024-08-10'
AND d."Drug_name" IN ('Aspirin', 'Ibuprofen')
GROUP BY d."Drug_name", t."Date";
Pivot:
SELECT *
FROM crosstab(
'SELECT
d."Drug_name",
t."Month",
SUM(f."UnitPrice" * f."Quantity") AS Total_Sales
FROM public."Fact" f
JOIN public."Drug" d ON f."Drug_ID" = d."Drug_ID"
JOIN public."Time" t ON f."Date" = t."Date"
GROUP BY d."Drug_name", t."Month"
ORDER BY d."Drug_name", t."Month"',
'SELECT DISTINCT "Month" FROM public."Time" ORDER BY "Month"'
) AS ct (
"Drug_name" VARCHAR,
"August" BIGINT,
"September" BIGINT,
"October" BIGINT
-- Add more months as needed
);