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

day7_assignments_solutions

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

day7_assignments_solutions

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1- write a sql to find top 3 products in each category by highest rolling 3 months

total sales for Jan 2020.

with xxx as (select category,product_id,datepart(year,order_date) as


yo,datepart(month,order_date) as mo, sum(sales) as sales
from orders
group by category,product_id,datepart(year,order_date),datepart(month,order_date))
,yyyy as (
select *,sum(sales) over(partition by category,product_id order by yo,mo rows
between 2 preceding and current row ) as roll3_sales
from xxx)
select * from (
select *,rank() over(partition by category order by roll3_sales desc) as rn from
yyyy
where yo=2020 and mo=1) A
where rn<=3

2- write a query to find products for which month over month sales has never
declined.

with xxx as (select product_id,datepart(year,order_date) as


yo,datepart(month,order_date) as mo, sum(sales) as sales
from orders
group by product_id,datepart(year,order_date),datepart(month,order_date))
,yyyy as (
select *,lag(sales,1,0) over(partition by product_id order by yo,mo) as prev_sales
from xxx)
select distinct product_id from yyyy where product_id not in
(select product_id from yyyy where sales<prev_sales group by product_id)

3- write a query to find month wise sales for each category for months where sales
is more than the combined sales of previous 2 months for that category.

with xxx as (select category,datepart(year,order_date) as


yo,datepart(month,order_date) as mo, sum(sales) as sales
from orders
group by category,datepart(year,order_date),datepart(month,order_date))
,yyyy as (
select *,sum(sales) over(partition by category order by yo,mo rows between 2
preceding and 1 preceding ) as prev2_sales
from xxx)
select * from yyyy where sales>prev2_sales

4- write a user defined functions which takes 2 input parameters of DATE data
type.
The function should return no of business days between the 2 dates.
note -> if any of the 2 input dates are falling on saturday or sunday then function
should use immediate Monday
date for calculation

example if we pass dates as 2024-11-30 and 2024-12-05..then it should calculate


business days
between 2024-12-02 and 2024-12-05

Solution :
CREATE FUNCTION dbo.GetBusinessDays (
@start_date DATE,
@end_date DATE
)
RETURNS INT
AS
BEGIN
-- Adjust the start date to the next Monday if it falls on Saturday or Sunday
SET @start_date = CASE
WHEN DATENAME(WEEKDAY, @start_date) = 'Saturday' THEN DATEADD(DAY, 2,
@start_date)
WHEN DATENAME(WEEKDAY, @start_date) = 'Sunday' THEN DATEADD(DAY, 1,
@start_date)
ELSE @start_date
END;

-- Adjust the end date to the next Monday if it falls on Saturday or Sunday
SET @end_date = CASE
WHEN DATENAME(WEEKDAY, @end_date) = 'Saturday' THEN DATEADD(DAY, 2,
@end_date)
WHEN DATENAME(WEEKDAY, @end_date) = 'Sunday' THEN DATEADD(DAY, 1,
@end_date)
ELSE @end_date
END;

RETURN
(select DATEDIFF(DAY,@start_date,@end_date) - 2 *
DATEDIFF(week,@start_date,@end_date))
END;

5- This is in continuation to the stored procedure for namastesql LMS.

we created a stored procedure sp_manage_students to manage students.

Now create a courses table manually which will have course_id , course_name and
insert some records.
eg
100, SQL
200, Python
300, Tableau

on namaste sql you have option to buy combos as well which gives you access to
multiple courses. create a table called combos which will have 2 columns combo_id ,
course_id and insert some data
eg
combo_id , course_id
1,100
1,200
2,100
2,200
2,300

Next create a stored procedure to manage enrollments : sp_manage_enrollments


for this first create a table student_courses which will have 2 columns student_id,
course_id

student_id will refer to the students table created in the lecture.


sp_manage_enrollments should take 3 arguments : @student_id, @course_combo_id ,
@iscombo_flag

based on these arguments the procedure should enroll the students by making entry
in student_courses table.

Solution :

alter table students add primary key (student_id)

CREATE TABLE courses (


course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);

INSERT INTO courses (course_id, course_name) VALUES


(100, 'SQL'),
(200, 'Python'),
(300, 'Tableau');

CREATE TABLE combos (


combo_id INT ,
course_id INT,
PRIMARY KEY (combo_id, course_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

INSERT INTO combos (combo_id, course_id) VALUES


(1, 100),
(1, 200),
(2, 100),
(2, 200),
(2, 300);

CREATE TABLE student_courses (


student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

CREATE OR ALTER PROCEDURE sp_manage_enrollments (


@student_id INT,
@course_combo_id INT,
@iscombo_flag INT
)
AS
BEGIN
IF @iscombo_flag = 1
BEGIN
-- Enroll student in all courses of the combo
INSERT INTO student_courses (student_id, course_id)
SELECT @student_id, course_id
FROM combos
WHERE combo_id = @course_combo_id;

PRINT 'Student successfully enrolled in combo courses.';


END
ELSE
BEGIN
-- Enroll student in a single course
INSERT INTO student_courses (student_id, course_id)
VALUES (@student_id, @course_combo_id);

PRINT 'Student successfully enrolled in a single course.';


END
END;

EXEC sp_manage_enrollments @student_id = 1, @course_combo_id = 1, @iscombo_flag =


1;

You might also like