Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

T-SQL insert data if it is same day ignore if it is same week

$
0
0

I have to insert data into these tables - f_up_msg & then f_up_hst and source table is - nhst. I need T-SQL.

I have to pick caseid's from nhst table and chose cas_notify + 7 . If I found same case id in f_up_hst then I have to pick from f_up_hst table and field is f_ts + 7. Basically, I have to add +7 days to these fields for a given case id and if it falls under +7 category days then I have to insert data into tables - f_up_msg & then f_up_hst.

One time data insertion should happen in these tables tables for a given employee and it could be multiple cases(same date) - f_up_msg & then f_up_hst in a given week. Left over caseid's will move to other week. If records inserted today and then till next 7 days nothing should be inserted for those case id's for that user.

We should consider only where ack=0. test2 & test3 for e1 falls in same week so we shouldn't insert that. As per rule only one notification to the employee and it could be different case(s) having same date for the user should receive in that week and will be ignored any other day in the same week.
So in the above scenario, test2 will go in next week, which is on 5/25/2014. Assume user not acknowledge

 test1 & test8 cases and also test2, test3 & test7 will go on 5/25/2014

f_up_msg & then f_up_hst are destination tables.

SSource table -

Below are scripts...

USE [test]
GO
/****** Object:  Table [dbo].[f_up_hst]    Script Date: 5/13/2014 12:20:56 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[f_up_hst](
	[f_id] [int] IDENTITY(1,1) NOT NULL,
	[empid] [nvarchar](50) NULL,
	[caseid] [int] NULL,
	[f_u_noteid] [int] NULL,
	[f_ts] [datetime] NULL
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[f_up_msg]    Script Date: 5/13/2014 12:20:56 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[f_up_msg](
	[f_u_noteid] [int] IDENTITY(1,1) NOT NULL,
	[empid] [nvarchar](50) NULL,
	[n_email] [nvarchar](50) NULL,
	[n_sub] [nvarchar](50) NULL,
	[n_body] [nvarchar](50) NULL,
	[n_msg_t] [datetime] NULL
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[nhst]    Script Date: 5/13/2014 12:20:56 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[nhst](
	[caseid] [int] NULL,
	[empid] [nvarchar](50) NULL,
	[ename] [nvarchar](50) NULL,
	[casename] [nvarchar](50) NULL,
	[cas_notify] [datetime] NULL,
	[ack] [int] NULL
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[nmsg]    Script Date: 5/13/2014 12:20:56 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[nmsg](
	[caseid] [int] NULL,
	[n_email] [nvarchar](50) NULL,
	[n_sub] [nvarchar](50) NULL,
	[n_body] [nvarchar](50) NULL,
	[n_msg] [datetime] NULL
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[f_up_hst] ON 

GO
INSERT [dbo].[f_up_hst] ([f_id], [empid], [caseid], [f_u_noteid], [f_ts]) VALUES (1, N'e1', 1, 1, CAST(0x0000A32F00000000 AS DateTime))
GO
INSERT [dbo].[f_up_hst] ([f_id], [empid], [caseid], [f_u_noteid], [f_ts]) VALUES (2, N'e1', 8, 1, CAST(0x0000A32F00000000 AS DateTime))
GO
INSERT [dbo].[f_up_hst] ([f_id], [empid], [caseid], [f_u_noteid], [f_ts]) VALUES (3, N'e2', 1, 2, CAST(0x0000A32F00000000 AS DateTime))
GO
INSERT [dbo].[f_up_hst] ([f_id], [empid], [caseid], [f_u_noteid], [f_ts]) VALUES (4, N'e6', 2, 3, CAST(0x0000A33000000000 AS DateTime))
GO
INSERT [dbo].[f_up_hst] ([f_id], [empid], [caseid], [f_u_noteid], [f_ts]) VALUES (5, N'e5', 5, 4, CAST(0x0000A33E00000000 AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[f_up_hst] OFF
GO
SET IDENTITY_INSERT [dbo].[f_up_msg] ON 

GO
INSERT [dbo].[f_up_msg] ([f_u_noteid], [empid], [n_email], [n_sub], [n_body], [n_msg_t]) VALUES (1, N'e1', N'test', N'test', N'test1 & test8', CAST(0x0000A32F00000000 AS DateTime))
GO
INSERT [dbo].[f_up_msg] ([f_u_noteid], [empid], [n_email], [n_sub], [n_body], [n_msg_t]) VALUES (2, N'e2', N'test', N'test', N'test1', CAST(0x0000A32F00000000 AS DateTime))
GO
INSERT [dbo].[f_up_msg] ([f_u_noteid], [empid], [n_email], [n_sub], [n_body], [n_msg_t]) VALUES (3, N'e6', N'test', N'test', N'test2', CAST(0x0000A33000000000 AS DateTime))
GO
INSERT [dbo].[f_up_msg] ([f_u_noteid], [empid], [n_email], [n_sub], [n_body], [n_msg_t]) VALUES (4, N'e5', N'test', N'test', N'test5', CAST(0x0000A33E00000000 AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[f_up_msg] OFF
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (1, N'e1', N'dinu', N'test1', CAST(0x0000A32800000000 AS DateTime), 0)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (2, N'e1', N'dinu', N'test2', CAST(0x0000A32900000000 AS DateTime), 0)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (3, N'e1', N'dinu', N'test3', CAST(0x0000A32A00000000 AS DateTime), 0)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (4, N'e4', N'esh', N'test4', CAST(0x0000A32900000000 AS DateTime), 1)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (5, N'e5', N'dinesh', N'test5', CAST(0x0000A33600000000 AS DateTime), 0)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (6, N'e6', N'kalva', N'test6', CAST(0x0000A33B00000000 AS DateTime), 1)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (1, N'e2', N'ka', N'test1', CAST(0x0000A32800000000 AS DateTime), 0)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (2, N'e6', N'kalu', N'test2', CAST(0x0000A32900000000 AS DateTime), 0)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (2, N'e7', N'k', N'test2', CAST(0x0000A32900000000 AS DateTime), 1)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (7, N'e1', N'dinu', N'test7', CAST(0x0000A32E00000000 AS DateTime), 0)
GO
INSERT [dbo].[nhst] ([caseid], [empid], [ename], [casename], [cas_notify], [ack]) VALUES (8, N'e1', N'dinu', N'test8', CAST(0x0000A32800000000 AS DateTime), 0)
GO
INSERT [dbo].[nmsg] ([caseid], [n_email], [n_sub], [n_body], [n_msg]) VALUES (1, N'dinu@test.com', N'email - test1', N'this is test1', CAST(0x0000A27300000000 AS DateTime))
GO
INSERT [dbo].[nmsg] ([caseid], [n_email], [n_sub], [n_body], [n_msg]) VALUES (2, N'dinu@test.com', N'email - test2', N'this is test2', CAST(0x0000A27400000000 AS DateTime))
GO
INSERT [dbo].[nmsg] ([caseid], [n_email], [n_sub], [n_body], [n_msg]) VALUES (3, N'dinu@test.com', N'email - test3', N'this is test3', CAST(0x0000A27500000000 AS DateTime))
GO
INSERT [dbo].[nmsg] ([caseid], [n_email], [n_sub], [n_body], [n_msg]) VALUES (4, N'esh@test.com', N'email -esh', N'this is esh', CAST(0x0000A27400000000 AS DateTime))
GO
INSERT [dbo].[nmsg] ([caseid], [n_email], [n_sub], [n_body], [n_msg]) VALUES (5, N'dinesh@test.com', N'email - dinesh', N'this is dinesh testing', CAST(0x0000A28100000000 AS DateTime))
GO
INSERT [dbo].[nmsg] ([caseid], [n_email], [n_sub], [n_body], [n_msg]) VALUES (6, N'kalva@test.com', N'email - kalva', N'this is kalva test', CAST(0x0000A28600000000 AS DateTime))
GO




Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>