Assignment 5
Assignment 5
Assignment 5
Create a YOY analysis for the count of customers enrolled with the company
--each month. The output should look like:
--B. Find out the top 3 best-selling products in each of the categories that are
--currently active on the Website
SELECT *
FROM (
SELECT P.ProductID,
C.CategoryID,
SUM(O.Quantity) sales,
ROW_NUMBER() OVER (PARTITION BY MAX(P.category_id) ORDER BY SUM(O.quantity)
DESC) as ranking
FROM Products P INNER JOIN OrderDetails O
ON O.ProductID = P.ProductID
join Category C on C.CategoryID=P.category_id
where active = 1
GROUP BY C.CategoryID , P.ProductID ) t
where ranking<=3
ORDER BY categoryid;
--C. Find the out the least selling products in each of the categories that are
--currently active on the website
SELECT *
FROM (
SELECT P.ProductID,
C.CategoryID,
MAX(P.Category_ID) category,
SUM(O.Quantity) sales,
ROW_NUMBER() OVER (PARTITION BY MAX(P.category_id) ORDER BY SUM(O.quantity))
as ranking
FROM Products P INNER JOIN OrderDetails O
ON O.ProductID = P.ProductID
join Category C on C.CategoryID=P.category_id
where active = 1
GROUP BY C.CategoryID , P.ProductID
) t
ORDER BY categoryid desc;
--D. We are trying to find paired products that are often purchased together by
--the same user, such as chips and soft drinks, milk and curd etc..
--Find the top paired products names.
--K. Find the 3-day rolling average for the total purchase amount by each
--customer
A GROUP BY normally reduces the number of rows returned by rolling them up and calculating
averages or sums for each row. PARTITION BY does not affect the number of rows returned, but it
changes how a window function's result is calculated
GROUP BY
The GROUP BY clause is used in SQL queries to define groups based on some given
criteria. These criteria are what we usually find as categories in reports. Examples
of criteria for grouping are:
PARTITION BY
Depending on what you need to do, you can use a PARTITION BY in our queries to
calculate aggregated values on the defined groups. The PARTITION BY is combined
with OVER() and windows functions to calculate aggregated values. This is very
similar to GROUP BY and aggregate functions, but with one important difference:
when you use a PARTITION BY, the row-level details are preserved and not
collapsed. That is, you still have the original row-level details as well as the
aggregated values at your disposal. All aggregate functions can be used as window
functions.
A view exists only for a single query. Each time you use the name of a view, its table is recreated
from existing data. A temporary table exists for the entire database session in which it was created.
In a relational database like SQL Server, views provide a way of giving users a way to work
with specific portions of a larger schema. Temporary tables are another means of providing
end users with a subset or collation of data from base tables, and at first glance it seems that
these are similar methods of achieving the same end. So it is important to understand what
the difference is between a view and a temp table.
A temp table is a base table that is not stored in the database. Instead it only exists while the
database session remains active, and it must be populated each session with data using
SQL INSERT commands. Similarly, a view is not stored with data but with a query that will
retrieve data. However, views exist only for a single query, and each time you generate a
view it is recreated from current data. In contrast, a temp table exists for the entire database
session, and once populated it retains those records until the session ends.
Views being a physical object on database (but does not store data physically) and can be used on
multiple queries, thus provide flexibility and centralized approach. CTE, on the other hand are
temporary and will be created when they are used; that's why they are called as inline view
CTE:
CTE stands for Common Table expressions can be thought of as a temporary result set that is defined within the
execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is like a derived
table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE
can be self-referencing and can be referenced multiple times in the same query. CTE improves readability and
ease in maintenance of complex queries and sub-queries.
View:
A view is a virtual table which doesn’t physically store any data, it consists of columns from one or more tables.
So, whenever we query a view then it retrieves data from the underlying base tables. It is a query stored as an
object. Views are used for security purpose in databases, views restrict the user from viewing certain column and
rows means by using view we can apply the restriction on accessing the rows and columns for specific user. Views
display only those data which are mentioned in the query, so it shows only data which is returned by the query
that is defined at the time of creation of the View.
Q5. What is the difference between Row Number, Rank and Dense Rank?
RANK Function
The RANK function is used to retrieve ranked rows based on the condition
of the ORDER BY clause. For example, if you want to find the name of the
car with third highest power, you can use RANK Function.
DENSE_RANK Function
The DENSE_RANK function is similar to RANK function however the
DENSE_RANK function does not skip any ranks if there is a tie between the
ranks of the preceding records.
ROW_NUMBER Function
Unlike the RANK and DENSE_RANK functions, the ROW_NUMBER function
simply returns the row number of the sorted records starting with 1. For
example, if RANK and DENSE_RANK functions of the first two records in
the ORDER BY column are equal, both of them are assigned 1 as their
RANK and DENSE_RANK. However, the ROW_NUMBER function will assign
values 1 and 2 to those rows without taking the fact that they are equally
into account