Hi yogeshc,
I have create a sample procedure as per you provided details. Please check the query.
SQL
-- EXEC GetLastMonthsRecords 'yogesh.0650@gmail.com'
CREATE PROCEDURE GetLastMonthsRecords
@userId varchar(50)
AS
BEGIN
DECLARE @responces TABLE (ReqDate DATETIME,ticket_id INT,User_ids varchar(50))
INSERT INTO @responces VALUES('2017-07-15' , 7, 'yogesh.0650@gmail.com' )
INSERT INTO @responces VALUES('2017-07-15' , 7, 'yogesh.0650@gmail.com' )
INSERT INTO @responces VALUES('2017-07-15' , 8 , 'yogesh.0650@gmail.com' )
INSERT INTO @responces VALUES('2017-07-15' , 8 , 'yogesh.0650@gmail.com' )
INSERT INTO @responces VALUES('2017-06-23' , 4 , 'yogesh.0650@gmail.com' )
INSERT INTO @responces VALUES('2017-02-23' , 3 , 'yogesh.0650@gmail.com' )
INSERT INTO @responces VALUES('2017-02-23' , 5 , 'yogesh.0650@gmail.com' )
DECLARE @add_view_log TABLE(Ticket_id INT,user_ids varchar(30),view_date DATETIME)
INSERT INTO @add_view_log VALUES(6,'yogesh.0650@gmail.com','2017-07-17')
INSERT INTO @add_view_log VALUES(4,'yogesh.0650@gmail.com','2017-07-17')
INSERT INTO @add_view_log VALUES(4,'yogesh.0650@gmail.com','2017-07-17')
INSERT INTO @add_view_log VALUES(6,'yogesh.0650@gmail.com','2017-02-17')
INSERT INTO @add_view_log VALUES(6,'yogesh.0650@gmail.com','2017-01-17')
SELECT res.User_ids ,
(SELECT COUNT(*) FROM @responces responces WHERE (responces.ReqDate BETWEEN DATEADD(Month,-3,GETDATE()) AND GETDATE()) AND user_ids = @userId) Response_3Month,
(SELECT COUNT(*) FROM @responces responces WHERE (responces.ReqDate BETWEEN DATEADD(Month,-6,GETDATE()) AND GETDATE()) AND user_ids = @userId) Response_6Month,
(SELECT COUNT(*) FROM @add_view_log addview WHERE (addview.view_date BETWEEN DATEADD(Month,-3,GETDATE()) AND GETDATE()) AND user_ids = @userId) viewlog_3month,
(SELECT COUNT(*) FROM @add_view_log addview WHERE (addview.view_date BETWEEN DATEADD(Month,-6,GETDATE()) AND GETDATE()) AND user_ids = @userId) viewlog_6month
FROM @responces res
GROUP BY res.user_ids
END
GO
Output
User_ids |
Response_3Month |
Response_6Month |
viewlog_3month |
viewlog_6month |
yogesh.0650@gmail.com |
5 |
7 |
3 |
4 |