Hello again experts,
I have the following tables:
Create Table Employees (
EmployeeID int IDENTITY(1,3) NOT NULL,
employeeName nvarchar(150) NULL,
empTitle nvarchar(100) NULL,
email nvarchar(100) NULL)
CREATE Table DateDetails (
dateID int IDENTITY (1,1) NOT NULL,
employeeID int NULL,
dateCreated datetime NULL)
Then some sample data:
INSERT INTO Employees (employeeName, empTitle, email)
VALUES
('Jane Doe','Senior Assistant Attorney','jdoe@hotmail.com'),
('Karen Wise','Internal Auditor','kwise@yahoo.com'),
('Angel Marist','Director, Library','Amarist@gmail.com'),
('Kevin Wiseman','Library Director',' kwiseman@yahoo.com'),
('Major Bates','Director','mbates@bing.com'),
('May Baker','PRINCIPAL AUDITOR','mbaker@outlook.com'),
('Jane Doe','Senior Assistant Attorney','jdoe@hotmail.com'),
('Karen Wise','Internal Auditor','kwise@yahoo.com'),
('Angel Marist','Director, Library','Amarist@gmail.com'),
('Kevin Wiseman','Library Director',' kwiseman@yahoo.com'),
('Major Bates','Director','mbates@bing.com'),
('May Baker','PRINCIPAL AUDITOR','mbaker@outlook.com'),
('Jane Doe','Senior Assistant Attorney','jdoe@hotmail.com'),
('Karen Wise','Internal Auditor','kwise@yahoo.com'),
('Angel Marist','Director, Library','Amarist@gmail.com'),
('Kevin Wiseman','Library Director',' kwiseman@yahoo.com'),
('Major Bates','Director','mbates@bing.com'),
('May Baker','PRINCIPAL AUDITOR','mbaker@outlook.com'),
('Jane Doe','Senior Assistant Attorney','jdoe@hotmail.com'),
('Karen Wise','Internal Auditor','kwise@yahoo.com'),
('Angel Marist','Director, Library','Amarist@gmail.com'),
('Kevin Wiseman','Library Director',' kwiseman@yahoo.com'),
('Major Bates','Director','mbates@bing.com'),
('May Baker','PRINCIPAL AUDITOR','mbaker@outlook.com');
INSERT INTO DateDetails (employeeID, dateCreated)
VALUES
(1,'2018-04-19 10:12:29.133'),
(4,NULL),
(7,'2018-04-24 17:22:39.143'),
(10,NULL),
(13,'2018-04-24 11:20:23.103'),
(16,'2018-04-24 12:22:29.133'),
(19,'2019-04-19 10:12:29.133'),
(22,'2019-04-24 12:22:29.130'),
(25,NULL),
(28,'2019-04-16 10:09:29.100'),
(31,'2019-04-24 11:20:23.103'),
(34,NULL),
(37,NULL),
(40,'2022-04-24 12:22:29.130'),
(43,'2022-04-24 17:22:39.143'),
(46,'2022-04-16 10:09:29.100'),
(49,'2022-04-24 11:20:23.103'),
(52,'2022-04-24 12:22:29.133'),
(55,'2023-04-19 10:12:29.133'),
(58,'2023-04-24 12:22:29.130'),
(61,'2023-04-24 17:22:39.143'),
(64,'2023-04-16 10:09:29.100'),
(67,'2023-04-24 11:20:23.103'),
(70,'2023-04-24 12:22:29.133');
Finally, the following stored procedure with pivot table
CREATE PROCEDURE [dbo].[uspGetPivotedData]
AS
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(FORMAT(d.dateCreated, 'yyyy'))
FROM DateDetails d
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT employeeName,empTitle,email, ' + @cols + ' from
(
select employeeName
, empTitle
,email
, YEAR(dateCreated) AS YearCreated
from SourceDetails inner join Employees on SourceDetails.employeeID = Employees.EmployeeID
inner join DateDetails on SourceDetails.employeeID = DateDetails.employeeID
) x
pivot
(
max(YearCreated)
for YearCreated in (' + @cols + ')
) p '
EXECUTE sp_executesql @query
When you run this store procedure agains the sample data, it works.
However, I am having invoking it with say gridview or something else.
Any ideas how to invoke the stored procedure?
The issue is how to get the @cols to display the years along with the rest of the fieldnames.
Any help is greatly appreciated.
Also if it possible, can I replace this:
max(YearCreated) in such that if Max(YearCreated) has value, replace it with 'Submitted' and if NULL, replace the NULL with 'No data'
Any assistance is greatly appreciated.
Many thanks in advance.