0% found this document useful (0 votes)
16 views

Bda Ans For Ia2 (Partial

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

Bda Ans For Ia2 (Partial

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

7. Explain the MongoDB aggregation pipeline with examples.

An aggregation pipeline consists of one or more stages that process documents:

 Each stage performs an operation on the input documents. For example,


a stage can filter documents, group documents, and calculate values.

 The documents that are output from a stage are passed to the next stage.

 An aggregation pipeline can return results for groups of documents. For


example, return the total, average, maximum, and minimum values.

Calculate Total Order Quantity

The following aggregation pipeline example contains two stages and returns the
total order quantity of medium size pizzas grouped by pizza name:
The $match stage:

 Filters the pizza order documents to pizzas with a size of medium .

 Passes the remaining documents to the $group stage.

The $group stage:

 Groups the remaining documents by pizza name .

 Uses $sum to calculate the total order quantity for each pizza name . The total
is stored in the totalQuantity field returned by the aggregation pipeline.

Expected Output

4. Consider the Table Structure:


 Orders table
o OrderID
o CustomerID
o OrderAmount
1. List all products with total sales quantity greater than a specified threshold
2. Compute the total sales amount for each month
3. Calculate the average order value for customers who have placed more than 5 orders.

OrderID CustomerID OrderAmount Quantity Month

1 11 1000 1 June
2 12 1500 3 July
3 12 2000 3 June

1. List all products with total sales quantity greater than a specified threshold.

Select * from Orders where OrderAmount>=1250;


2 12 1500 3 July
3 12 2000 3 June

2. Compute the total sales amount for each month.

Select sum(OrderAmount) from Orders GROUP BY Month;


sum(OrderAmount)
3000

3. Calculate the average order value for customers who have placed more
than 5 orders.

Select AVG(OrderAmount) from Order GROUP BY CustomerID


HAVING sum(Quantity)>5;
Create a Hive table named employees with the following columns:
employee_id (int)
 first_name (string)
 last_name (string)
 email (string)
 salary (double)
Perform following operations: s
1. Remove all data from the employees table but keep the table
structure
2. Add a new column department (string) to the employees table. Rename
the salary column to monthly_salary.
3. Add a new column department (string) to the employees table. Rename
the salary
column to monthly_salary.
4. Display all the tables available in the current Hive database.
5. Display the structure of the employees table

Here are the Hive queries to perform the requested operations:


employee_id first_name last_name email salary
1 F1 L1 [email protected] 25000
2 F2 L2 [email protected] 34000
m
3 F3 L3 [email protected] 50000
m

1. Drop the table named employees if it exists:

DROP TABLE IF EXISTS employees;

2. Remove all data from the employees table (note: this assumes the table already
exists):

TRUNCATE TABLE employees;


SELECT * FROM employees;
0 ROWS

3. Add a new column department and rename the salary column:

ALTER TABLE employees


ADD COLUMN department string;

ALTER TABLE employees


RENAME COLUMN salary TO monthly_salary;

4. Display all tables in the current Hive database:

SHOW TABLES;
employees

5. Display the structure of the employees table:

DESCRIBE employees;

name type comment


employee_id Int NULL
first_name string NULL
last_name string NULL
email string NULL
monthly_salary double NULL
department string NULL

You might also like