SQL
SQL
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Sample Output :-
(Please keep same column names as below)
*/
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
(Tag: Creation of procs, Index, Triggers, Single line, and Multiline comments,
insert, update, delete)
*/
--deleted the duplicate records using cte table and inserted into backupsales table
using trigger
/*
TRIGGER
create or alter trigger Trig_DDL_Q2
on sales
AFTER Delete
AS
insert into backupsales select SalesOrderID,User_id,OrderDate,OrderQty,ProductID
from deleted
*/
/*
BACKUP SALES TABLE
create table backupsales(
SalesOrderID int,
User_id int,
OrderDate date,
OrderQty int,
ProductID int)
*/
with cte as(
select *,ROW_NUMBER() over(partition by SalesOrderID,ProductID order by ShipDate
desc) rn from Sales
)
delete from cte where rn > 1
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Create a new table VISITS_MASK which is an exact copy of VISITS table. Copy all
data from VISITS table into VISITS_MASK table.
VISITS_MASK table should be created in such a way that first three characters of
each subjects in the Subjects column should be masked.
(Tag: Joins, Group By, Window Functions, CTE, Rank, String Operations, Data
Masking)
OUTPUT SAMPLE
GO
--mask table
select Name,LOCATION,FLOOR,CONCAT(REPLICATE('*', 3), SUBSTRING(subjects, 4,
LEN(SUBJECTS) - 3)) as Subjects into VISITS_MASK from VISITS
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Sample OUTPUT :-
*/
GO
EXEC(@Query);
END;
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
sample input:
EmployeeID Reporting To Email ID
HRM146 SuperAdmin tkala@celebaltech.com
HRM1997 NULL
pratik.singh@celebaltech.com
HRM1682 NULL ats@celebaltech.com
HRM42 Anirudh Kala HRM146
abhishek.parti@celebaltech.com
HRM31 Anirudh Kala HRM146
tushar.mittal@celebaltech.com
HRM27 Anirudh Kala HRM146
rohan.saini@celebaltech.com
HRM92 Tushar Mittal HRM31
muskan.khatnani@celebaltech.com
HRM87 Tushar Mittal HRM31
gaurav.modi@celebaltech.com
HRM86 Tushar Mittal HRM31
abhishek.jarwal@celebaltech.com
HRM393 Gaurav Modi HRM87
kanishk.shrotriya@celebaltech.com
HRM341 Gaurav Modi HRM87 jennet.john@celebaltech.com
HRM1764 Jennet John HRM341
aditi.bohra@celebaltech.com
HRM1746 Jennet John HRM341
chayan.sharma@celebaltech.com
HRM3253 Jennet John HRM341
kartik.yadav@celebaltech.com
UNION ALL
SELECT
e.EmployeeID,
e.[Reporting To],
e.[Email ID],
rh.Level + 1
FROM
(select *,substring([Reporting To],charindex('HRM',[Reporting
To]),len([Reporting To])) [RM_HRM]from employee_master) e
JOIN
RecursiveHierarchy rh ON e.RM_HRM = rh.EmployeeID
),cte2 as(
SELECT
r.EmployeeID,
r.[Reporting To],
r.[Email ID],
r.Level
FROM
RecursiveHierarchy r
UNION ALL
SELECT
EmployeeID,
[Reporting To],
[Email ID],
2 AS Levels
FROM
(select *,substring([Reporting To],charindex('HRM',[Reporting
To]),len([Reporting To])) [RM_HRM]from employee_master) e
WHERE
EmployeeID NOT IN (SELECT DISTINCT EmployeeID FROM RecursiveHierarchy)
)
select * into #temp from cte2 ORDER BY Level;
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Source input:
eid ename esal edept
E201 David 40000 10
E202 Preeta 35000 20
E203 Kevin 63000 10
E204 George 58000 30
E205 Danny 71000 20
Above records should be updated in the target table in SCD type 2 method after
running the stored procedure.
Output Sample:
create table emp_target (eid nvarchar(40), ename nvarchar(40), esal int, edept int,
Active nvarchar(40))
-- create procedures
update source
set ename = target.ename, esal = target.esal, edept = target.edept
from emp_data as source
inner join emp_target as target
on source.eid = target.eid
where target.Active = 'Y';
end;
exec sp_scd_type2
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Sample Output :-
*/
GO
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Display a column called Most_purchased_product which will show the most purchased
Product overall.
Also display a column called Total_Purchase_Count to show how many times the
most_purchased_product is purchased by each Users.
If any of the values of Total_amount, Total_Points_Earned or Total_Purchase_Count
is Null then replace it with 0.
(Tag: Views,Joins)
Output Sample
USER_ID Total_amount Total_Points_Earned Color
Total_Purchase_Count Most_purchased_product
17044 2294.99 458.998 Black 0
870
1229 4454.10 0.00 Blue 4
870
1419 1187.78 593.89 Yellow 4
870
*/
GO
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Create a view to generate a matrix to show the total inwards calculated per month
of the products having product code 1,4,6.
Also, display the grand total of the inwards per month and the grand total for
each product.
*/
GO
) v
group by rollup(prod_code)
select * from vw_mat_cost
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Display one column called NEW_NAME which will show first 50% part of the name in
lower case and
remaining part of the name in upper case.
Display one more column called EMP_JOB showing first name of employee with his/her
job (job in bracket).
E.g., David (Shopkeeper).
Display another column called EMP_POSITION. An employee who has atleast 2
reportees reporting to him,
mark his postion as SUPER SENIOR whereas mark others as SENIOR
Sample Output :-
(Please keep same column names as below)
e.deptid,
LOWER(CASE WHEN CHARINDEX(' ', e.name) > 0 THEN LEFT(e.name, CHARINDEX(' ', e.name)
- 1) ELSE e.name END) + ' ' + UPPER(CASE
WHEN CHARINDEX(' ', e.name) > 0 THEN SUBSTRING(e.name, CHARINDEX(' ', e.name) + 1,
LEN(e.name) - CHARINDEX(' ', e.name))
ELSE ''
END) AS New_Name,
CASE
WHEN CHARINDEX(' ', e.name) > 0 THEN LEFT(e.name, CHARINDEX(' ', e.name))
ELSE e.name
END + ' (' + d.deptname + ')' AS Emp_job,
CASE
WHEN (
SELECT COUNT(*)
FROM Employee
WHERE manager = e.empid
) >= 2 THEN
'Super Senior'
ELSE
'Senior'
END AS Emp_Position
FROM
Employee e
JOIN Department d ON e.deptid = d.deptid
group by e.empid,
e.name,
e.salary,
e.hiredate,
e.deptid,
d.deptname
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Sample Output :-
(Please keep same column names as below)
*/
GO
create or alter view Vw_Worker_Event
as
with cte1 as (
select eventtime,status,row_number() over (order by eventtime) as rn
from eventsdata
)
, cte2 as (
select c1.eventtime as start_time, min(c2.eventtime) as end_time,
datediff(minute, c1.eventtime, min(c2.eventtime)) as minutes
from cte1 c1
join cte1 c2 on c1.rn < c2.rn and c2.status = 'ended'
where c1.status = 'started'
group by c1.eventtime
)
select start_time, end_time, minutes
from cte2;
***********************************************************************************
*************************************************************
USE [Parikshak]
GO
SET QUOTED_IDENTIFIER ON
GO
Output::::
Sample Target records in Target tables:
Emp_trg table:
eid ename esal edept Active
E202 Preeta 35000 20 N
E202 Preeta 52000 20 Y
E206 Rebecca 48000 10 Y
Customer_trg table:
Cust_ID cust_name loc membership Active
30014 Celina Mumbai Yes N
30014 Celina Bangalore Yes Y
Please keep object names and column names exactly same as mentioned in the
question.
INSERT INTO emp_data (eid, ename, esal, edept) VALUES ('E206', 'Rebecca', 48000,
10)
INSERT INTO emp_data (eid, ename, esal, edept) VALUES ('E207', 'Mark', 62000, 10)
EXEC sp_prob2_scd_2;
***********************************************************************************
*************************************************************