day7_assignments_solutions
day7_assignments_solutions
2- write a query to find products for which month over month sales has never
declined.
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.
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
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;
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
based on these arguments the procedure should enroll the students by making entry
in student_courses table.
Solution :