| Hot to Get Cumulative Total.. Posted: 01 May 2013 03:10 AM PDT Hi Friends...I Having Table For The Following Structure..[code="sql"]Create Table EmpProcessDetail ( EmpId varchar(25), EmpName varchar(50),ProcessName varchar(30),GroupCode varchar(5),PlannedDays int,ActualDays int, DelayedDays int,TotalDelayDays int) insert into EmpProcessDetail values ('AKM_1','Saran','Purchase','A',15,11,4,NULL), ('AKM_1','Saran','Measurement','B',18,15,3,NULL), ('AKM_1','Saran','Cutting','C',12,15,-3,NULL), ('AKM_1','Saran','Stitching','D',20,27,-7,NULL), ('AKM_1','Saran','Ironing','E',14,19,-5,NULL), ('AKM_1','Saran','Packing','F',15,13,2,NULL), ('AKM_1','Saran','Checking','G',18,12,6,NULL), ('AKM_1','Saran','Delivery','H',22,33,-11,NULL), ('AKM_2','Arun','Purchase','A',12,13,-1,NULL), ('AKM_2','Arun','Measurement','B',15,19,-4,NULL), ('AKM_2','Arun','Cutting','C',22,29,-7,NULL), ('AKM_2','Arun','Stitching','D',41,43,-2,NULL), ('AKM_2','Arun','Ironing','E',27,26,-1,NULL), ('AKM_2','Arun','Packing','F',18,13,5,NULL), ('AKM_2','Arun','Checking','G',11,14,-3,NULL), ('AKM_2','Arun','Delivery','H',17,24,-7,NULL), ('AKM_3','Kumar','Purchase','A',41,43,-2,NULL), ('AKM_3','Kumar','Measurement','B',23,26,-3,NULL), ('AKM_3','Kumar','Cutting','C',22,29,-7,NULL), ('AKM_3','Kumar','Stitching','D',41,43,-2,NULL), ('AKM_3','Kumar','Ironing','E',12,13,-1,NULL), ('AKM_3','Kumar','Packing','F',14,19,-5,NULL), ('AKM_3','Kumar','Checking','G',12,14,-2,NULL), ('AKM_3','Kumar','Delivery','H',18,13,5,NULL) select * from EmpProcessDetail[/code]Below I have mentioned The my Required Table Data [code="sql"]EmpId EmpName ProcessName GroupCode PlannedDays ActualDays DelayedDays TotalDelayDays------------------------- -------------------------------------------------- ------------------------------ --------- ----------- ----------- ----------- --------------AKM_1 Saran Purchase A 15 11 4 4AKM_1 Saran Measurement B 18 15 3 7AKM_1 Saran Cutting C 12 15 -3 4AKM_1 Saran Stitching D 20 27 -7 -3AKM_1 Saran Ironing E 14 19 -5 -8AKM_1 Saran Packing F 15 13 2 -6AKM_1 Saran Checking G 18 12 6 0AKM_1 Saran Delivery H 22 33 -11 -11AKM_2 Arun Purchase A 12 13 -1 -1AKM_2 Arun Measurement B 15 19 -4 -5AKM_2 Arun Cutting C 22 29 -7 -12AKM_2 Arun Stitching D 41 43 -2 -14AKM_2 Arun Ironing E 27 26 -1 -15AKM_2 Arun Packing F 18 13 5 -10AKM_2 Arun Checking G 11 14 -3 -3AKM_2 Arun Delivery H 17 24 -7 -10AKM_3 Kumar Purchase A 41 43 -2 -2AKM_3 Kumar Measurement B 23 26 -3 -5AKM_3 Kumar Cutting C 22 29 -7 -12AKM_3 Kumar Stitching D 41 43 -2 -14AKM_3 Kumar Ironing E 12 13 -1 -15AKM_3 Kumar Packing F 14 19 -5 -20AKM_3 Kumar Checking G 12 14 -2 -22AKM_3 Kumar Delivery H 18 13 5 -17[/code][u]My Requirement :[/u]1) I want to be Fill the TotalDelayDays Column..2)The Cumulative Sum Based on the DelayedDays..2) The Cumulative Sum Should be Calculated on EmpID once it will finished for particular EmpID then Cumulative Sum should start from Initial for second EmpID ...Thanks & Regards,Saravanan.D |
| problem with join T-Sql Posted: 11 Feb 2013 06:40 AM PST i have customer table and it have 1 foreign key from other table in it then i have service table that customerID(foreign key) have relation with it.i want to show all custmer with last call,sum of payed service,last payed service,Liability of customer(acourding to getKeraye(bit) field),number of service of customer from service table.i write this query but it have wrong info from service and duplicate info.i give all table creation script with insert test data [code="sql"]/****** all table creation script with insert test data ******//****** Object: Table [dbo].[CustomerColor] Script Date: 02/12/2013 11:34:53 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[CustomerColor]( [CustomerColorID] [tinyint] IDENTITY(1,1) NOT NULL, [CustomerSubject] [nvarchar](50) NOT NULL, [Color] [nvarchar](50) NOT NULL, CONSTRAINT [PK_CustomerColor] PRIMARY KEY CLUSTERED ( [CustomerColorID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[Customer] Script Date: 02/12/2013 11:34:53 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Customer]( [CustomerID] [int] IDENTITY(10,1) NOT NULL, [EshterakTypeID] [tinyint] NOT NULL, [MahaleID] [int] NULL, [KnowID] [smallint] NULL, [CustomerColorID] [tinyint] NULL, [FullName] [nvarchar](50) NOT NULL, [Bestankary] [int] NOT NULL, [Etebar] [int] NOT NULL, [Address] [nvarchar](250) NOT NULL, [Telephone1] [varchar](11) NOT NULL, [Telephone2] [varchar](11) NULL, [Telephone3] [varchar](11) NULL, [Mobile] [varchar](11) NULL, [CustomerSabtDate] [datetime] NULL, [Describtion] [nvarchar](500) NULL, [GetService] [bit] NOT NULL, [PardakhtType] [bit] NOT NULL, CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ( [CustomerID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], CONSTRAINT [IX_Customer] UNIQUE NONCLUSTERED ( [FullName] ASC, [Telephone1] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOSET IDENTITY_INSERT [dbo].[Customer] ONINSERT [dbo].[Customer] ([CustomerID], [EshterakTypeID], [MahaleID], [KnowID], [CustomerColorID], [FullName], [Bestankary], [Etebar], [Address], [Telephone1], [Telephone2], [Telephone3], [Mobile], [CustomerSabtDate], [Describtion], [GetService], [PardakhtType]) VALUES (12, 1, 2, 3, NULL, N'اکبری', 1200, 0, N'شیراز 1', N'8343513', N'', N'', N'09362928936', CAST(0x0000A13600000000 AS DateTime), N'توضیحات1 ', 1, 1)INSERT [dbo].[Customer] ([CustomerID], [EshterakTypeID], [MahaleID], [KnowID], [CustomerColorID], [FullName], [Bestankary], [Etebar], [Address], [Telephone1], [Telephone2], [Telephone3], [Mobile], [CustomerSabtDate], [Describtion], [GetService], [PardakhtType]) VALUES (13, 1, 2, 3, NULL, N'توکلی', 0, 0, N'شیراز', N'8353313', N'', N'', N'09362928936', CAST(0x0000A13600000000 AS DateTime), N'توضیحات', 1, 1)INSERT [dbo].[Customer] ([CustomerID], [EshterakTypeID], [MahaleID], [KnowID], [CustomerColorID], [FullName], [Bestankary], [Etebar], [Address], [Telephone1], [Telephone2], [Telephone3], [Mobile], [CustomerSabtDate], [Describtion], [GetService], [PardakhtType]) VALUES (15, 1, 2, 3, NULL, N'رحمانی', 0, 0, N'شیراز', N'8343313', N'', N'', N'09362928936', CAST(0x0000A13400000000 AS DateTime), N'توضیحات', 1, 1)SET IDENTITY_INSERT [dbo].[Customer] OFF/****** Object: Table [dbo].[Service] Script Date: 02/12/2013 11:34:53 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Service]( [ServiceID] [bigint] IDENTITY(1,1) NOT NULL, [CustomerID] [int] NOT NULL, [DriverID] [int] NOT NULL, [khadamatID] [smallint] NULL, [EndUserName] [nvarchar](20) NOT NULL, [FactorEditDelID] [smallint] NULL, [Mabda] [nvarchar](250) NULL, [Maghsad] [nvarchar](250) NOT NULL, [SendTime] [datetime] NULL, [ReturnTime] [datetime] NULL, [Describtion] [nvarchar](500) NULL, [SabtDate] [datetime] NULL, [GetPorsant] [bit] NOT NULL, [DakhelShahr] [bit] NULL, [Gheymat] [int] NULL, [GetKeraye] [bit] NOT NULL, [Editor] [nvarchar](20) NULL, [EditDate] [datetime] NULL, CONSTRAINT [PK_Service] PRIMARY KEY CLUSTERED ( [ServiceID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET IDENTITY_INSERT [dbo].[Service] ONINSERT [dbo].[Service] ([ServiceID], [CustomerID], [DriverID], [khadamatID], [EndUserName], [FactorEditDelID], [Mabda], [Maghsad], [SendTime], [ReturnTime], [Describtion], [SabtDate], [GetPorsant], [DakhelShahr], [Gheymat], [GetKeraye], [Editor], [EditDate]) VALUES (4, 12, 12, 2, N'SA', 1, N'شیراز', N'شیراز2', CAST(0x0000A1600046DCA0 AS DateTime), CAST(0x0000A16000BDC9F0 AS DateTime), N'', CAST(0x0000A15B011CAD84 AS DateTime), 1, 1, 120000, 1, N'SA', CAST(0x0000A16100F09B0D AS DateTime))INSERT [dbo].[Service] ([ServiceID], [CustomerID], [DriverID], [khadamatID], [EndUserName], [FactorEditDelID], [Mabda], [Maghsad], [SendTime], [ReturnTime], [Describtion], [SabtDate], [GetPorsant], [DakhelShahr], [Gheymat], [GetKeraye], [Editor], [EditDate]) VALUES (5, 15, 13, NULL, N'SA', 1, N'شیراز', N'گلستان', CAST(0x0000A15C00AF3B60 AS DateTime), CAST(0x0000A16100B54640 AS DateTime), N'dsfsd', CAST(0x0000A15B011D6CD6 AS DateTime), 1, 1, 45000, 1, N'SA', CAST(0x0000A16100F17462 AS DateTime))SET IDENTITY_INSERT [dbo].[Service] OFF/****** Object: ForeignKey [FK_Service_Customer] Script Date: 02/12/2013 11:34:53 ******/ALTER TABLE [dbo].[Service] WITH CHECK ADD CONSTRAINT [FK_Service_Customer] FOREIGN KEY([CustomerID])REFERENCES [dbo].[Customer] ([CustomerID])GOALTER TABLE [dbo].[Service] CHECK CONSTRAINT [FK_Service_Customer]GO/****** Object: ForeignKey [FK_Customer_CustomerColor] Script Date: 02/12/2013 11:34:53 ******/ALTER TABLE [dbo].[Customer] WITH CHECK ADD CONSTRAINT [FK_Customer_CustomerColor] FOREIGN KEY([CustomerColorID])REFERENCES [dbo].[CustomerColor] ([CustomerColorID])GOALTER TABLE [dbo].[Customer] CHECK CONSTRAINT [FK_Customer_CustomerColor]GO/****** query ******/select distinct customer.*,CustomerColor.Color,CustomerColor.CustomerSubject,b.AkharinKeraye,b.Karkard,b.Lcallfrom customerleft outer join CustomerColor on Customer.CustomerColorID=CustomerColor.CustomerColorID,(select [Service].CustomerID,max([Service].SabtDate) as Lcall,SUM(Service.Gheymat) as Karkard,(select s.Gheymat from [Service] as s where s.SabtDate=max([Service].SabtDate)) as AkharinKerayefrom [Service] group by [Service].CustomerID )border by Customer.CustomerID,Customer.FullName [/code] |