0% found this document useful (0 votes)
9 views9 pages

Upload 4

Uploaded by

manas.k1288
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Upload 4

Uploaded by

manas.k1288
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Aim:

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

● Definition: The "Slice" operation selects a single dimension from a cube,


resulting in a sub-cube. It reduces the number of dimensions by fixing a specific
value for one of the dimensions, allowing a focus on a single aspect of the data.
● Example: Consider a data cube with dimensions for time, geography, and
product. A slice could select data for a specific time period, e.g., "Sales in 2023,"
reducing the cube to just geography and product dimensions for that time.

2. Dice

● Definition: The "Dice" operation selects a sub-cube by choosing specific values


from multiple dimensions. This narrows down the analysis by restricting the data
along two or more dimensions.
● Example: Continuing with the sales cube, a dice operation could extract data for
"Sales of Product X in Region Y during Q1 2023," selecting specific values from
the time, geography, and product dimensions.

3. Rollup

● Definition: The "Rollup" operation aggregates data along a hierarchy within a


dimension, moving from detailed data to more summarized levels. It reduces the
level of detail by climbing up the hierarchy.
● Example: In a sales cube, rolling up could involve aggregating data from "daily
sales" to "monthly sales" by summarizing individual transactions over days to
produce monthly totals.

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."Year", t."Month", t."Week", sum(f."UnitPrice" * f."Quantity") AS Total_Sales


FROM public."Time" t, public."Fact" f
where t."Date" = f."Date"
GROUP BY ROLLUP(t."Year", t."Month", t."Week")

SELECT t."Year", t."Month", t."Week", sum(f."UnitPrice" * f."Quantity") AS Total_Sales


FROM public."Time" t, public."Fact" f
where t."Date" = f."Date"
GROUP BY CUBE(t."Year", t."Month", t."Week")
Drill down:

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
);

You might also like