﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / problem with join T-Sql / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 02:39:30 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>Just to clarify, that inner join is causing the cross. Is this the intended behaviour? If so could you post how this solves your problem?Thanks.</description><pubDate>Wed, 01 May 2013 11:54:40 GMT</pubDate><dc:creator>mmartin1</dc:creator></item><item><title>RE: problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>Why would you want to cross join this data, it makes no sense to have Customer Y's service records against customer X, besides theres an impicit link between service data and customer.</description><pubDate>Tue, 12 Feb 2013 04:48:52 GMT</pubDate><dc:creator>Jason-299789</dc:creator></item><item><title>RE: problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>Jason-299789 that work but i dont know how to mark it as answer</description><pubDate>Tue, 12 Feb 2013 04:46:01 GMT</pubDate><dc:creator>vahid.arr</dc:creator></item><item><title>RE: problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>i think it is valid syntax, its just giving a cross join because there isn't a join in the WHERE clause.[code="sql"]select*fromtable1 t1,(select ...) t2where[b]t1.col1 = t2.col1[/b][/code]jasons example is a good modification, though. i would go with his update.</description><pubDate>Tue, 12 Feb 2013 03:15:42 GMT</pubDate><dc:creator>davidandrews13</dc:creator></item><item><title>RE: problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>Having reformated the query at the bottom of the code you submited [code="sql"]select distinct  	customer.*	,CustomerColor.Color	,CustomerColor.CustomerSubject	,b.AkharinKeraye	,b.Karkard	,b.Lcallfrom  customer	left 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 AkharinKeraye	from [Service] 	group by [Service].CustomerID ) border by Customer.CustomerID,Customer.FullName [/code]as far as im aware the Sub select after the left join is invalid syntax.You will need to do a join I would suggest something like this as a starting point[code="sql"]select distinct  	customer.*	,CustomerColor.Color	,CustomerColor.CustomerSubject	,b.AkharinKeraye	,b.Karkard	,b.Lcallfrom  customer	left outer join CustomerColor on Customer.CustomerColorID=CustomerColor.CustomerColorID,	INNER JOIN (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 AkharinKeraye			from [Service] 			group by [Service].CustomerID) b on b.CustomerID=customer.customerIdorder by Customer.CustomerID,Customer.FullName [/code]though you still have a possible issue with the AkharinKeraye correlated sub query which could return more than one row. I'm not sure about the JOIN i added it might need to be a LEFT OUTER.</description><pubDate>Tue, 12 Feb 2013 02:16:51 GMT</pubDate><dc:creator>Jason-299789</dc:creator></item><item><title>RE: problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>I edit My question with creation table and insert test data query and say all the detail</description><pubDate>Tue, 12 Feb 2013 01:15:31 GMT</pubDate><dc:creator>vahid.arr</dc:creator></item><item><title>RE: problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>Please take the time to read the first link in my signature about best practices when posting questions. If you post ddl, sample data and desired output in a format that makes it easy for people to work with you will find lots of people digging in to help you.</description><pubDate>Mon, 11 Feb 2013 14:44:41 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>problem with join T-Sql</title><link>http://www.sqlservercentral.com/Forums/Topic1418636-392-1.aspx</link><description>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]</description><pubDate>Mon, 11 Feb 2013 14:40:13 GMT</pubDate><dc:creator>vahid.arr</dc:creator></item></channel></rss>