﻿<?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 / SQL Server 2008 - General  / SQL Query / 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>Sat, 18 May 2013 18:11:23 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/15/2013)[/b][hr][quote][b]pwalter83 (1/15/2013)[/b][hr][quote][b]ChrisM@Work (1/11/2013)[/b][hr]Hi PaulI'll try to have a look over the weekend. Meantime, it's the last 5 mins of my last day here - and it's BEERTIME!CheersChrisM[/quote]Hi Chris,I am still struggling with this query. Any possibility you could have a look whenever you get a chance ?[/quote]Try this, Paul:[code="sql"]SELECT ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD, vh.VSLVOY_HEADER_ID, TEUFROM NCV_BL ncvINNER JOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD WHERE BL_ID = '17231410'	AND NOT EXISTS (		SELECT vs.PORT_CD, vpc.VSLVOY_HEADER_ID  		FROM MG_VSLVOY_PORT_CONTROL vpc -- 12 rows per row in vh		INNER JOIN MG_VSLVOY_SCHEDULE vs 			ON vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID 		WHERE vpc.VSLVOY_HEADER_ID = vh.VSLVOY_HEADER_ID -- correlation			AND vs.PORT_CD IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD) -- correlation	)[/code][/quote]Thanks a lot Chris, it worked perfectly well !!</description><pubDate>Wed, 16 Jan 2013 04:28:00 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]pwalter83 (1/15/2013)[/b][hr][quote][b]ChrisM@Work (1/11/2013)[/b][hr]Hi PaulI'll try to have a look over the weekend. Meantime, it's the last 5 mins of my last day here - and it's BEERTIME!CheersChrisM[/quote]Hi Chris,I am still struggling with this query. Any possibility you could have a look whenever you get a chance ?[/quote]Try this, Paul:[code="sql"]SELECT ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD, vh.VSLVOY_HEADER_ID, TEUFROM NCV_BL ncvINNER JOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD WHERE BL_ID = '17231410'	AND NOT EXISTS (		SELECT vs.PORT_CD, vpc.VSLVOY_HEADER_ID  		FROM MG_VSLVOY_PORT_CONTROL vpc -- 12 rows per row in vh		INNER JOIN MG_VSLVOY_SCHEDULE vs 			ON vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID 		WHERE vpc.VSLVOY_HEADER_ID = vh.VSLVOY_HEADER_ID -- correlation			AND vs.PORT_CD IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD) -- correlation	)[/code]</description><pubDate>Tue, 15 Jan 2013 03:32:30 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr]Hi PaulI'll try to have a look over the weekend. Meantime, it's the last 5 mins of my last day here - and it's BEERTIME!CheersChrisM[/quote]Hi Chris,I am still struggling with this query. Any possibility you could have a look whenever you get a chance ?</description><pubDate>Tue, 15 Jan 2013 02:21:01 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr]Hi PaulI'll try to have a look over the weekend. Meantime, it's the last 5 mins of my last day here - and it's BEERTIME!CheersChrisM[/quote]Hi Chris,Best of luck for your next venture !!!</description><pubDate>Mon, 14 Jan 2013 02:21:38 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@home (1/13/2013)[/b][hr]Hi PaulYour sample data set doesn't have a matching row between MG_VSLVOY_HEADER and MG_VSLVOY_PORT_CONTROL:[code="sql"]SELECT ncv.*,  vh.VSLVOY_HEADER_IDFROM NCV_BL ncvJOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD --JOIN MG_VSLVOY_PORT_CONTROL vpc --	ON vh.VSLVOY_HEADER_ID = vpc.VSLVOY_HEADER_ID --WHERE NOT EXISTS (--	SELECT 1 --	FROM MG_VSLVOY_SCHEDULE vs --	WHERE vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID --	AND vs.PORT_CD NOT IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD)--	)--	AND BL_ID = '17231410' [/code]Here's your sample data script for those who don't like attachments:[code="sql"]-- sample data scriptCREATE TABLE [dbo].[NCV_BL](	[BL_ID] [decimal](10, 0) NOT NULL,	[POL_LOCATION_CD] [nvarchar](5) NULL,	[POD_LOCATION_CD] [nvarchar](5) NULL,	[SAISAN_VESSEL_CD] [nvarchar](10) NULL,	[SAISAN_VOYAGE_CD] [nvarchar](12) NULL,	[SAISAN_LEG_CD] [nvarchar](1) NULL,	[TEU] [decimal] (10,3) null,	[PLACE_OF_RECEIPT] [nvarchar](5) NULL,	[PLACE_OF_DELIVERY] [nvarchar](5) NULL,	[SAISAN_MONTH] [nvarchar](6) NULL) ON [PRIMARY]INSERT INTO [NCV_BL]VALUES ('17231410','DKAAR','IDJKT','HNJAFR','0002E','E', '4','DEYTN','EGPSW','200802')CREATE TABLE [dbo].[MG_VSLVOY_PORT_CONTROL](	[VSLVOY_HEADER_ID] [numeric](10, 0) NOT NULL,	[VSLVOY_SCHEDULE_ID] [numeric](10, 0) NOT NULL) ON [PRIMARY]INSERT INTO [MG_VSLVOY_PORT_CONTROL]VALUES ('1629990', '1342190'),('1629991', '1342191'),('1629992', '1342192'),	('1629993', '1342193'),('1629994', '1342194'),('1629995', '1342195'),	('1629996', '1342196'),('1629997', '1342197'),('1630001', '1342201'),	('1629998', '1379563'),('1629999', '1379564'),('1630000', '1379565')		CREATE TABLE [dbo].[MG_VSLVOY_SCHEDULE](	[VSLVOY_SCHEDULE_ID] [numeric](10, 0) NOT NULL,	[PORT_CD] [varchar](5) NOT NULL) ON [PRIMARY]INSERT INTO [MG_VSLVOY_SCHEDULE]VALUES ('1342190','DEHAM'),('1342191','NLRTM'),('1342192','FRLEH'),('1342193','ESALG'),('1342194','EGSUZ'),('1342195','SGSIN'),	('1342196','CNYTN'),('1342197','HKHKG'),('1342201','CNSHA'),('1379563','CNTXG'),('1379564','KRKAN'),('1379565','KRPUS')		CREATE TABLE [dbo].[MG_VSLVOY_HEADER](	[VSLVOY_HEADER_ID] [numeric](10, 0) NOT NULL,	[VESSEL_CD] [nvarchar](10) NOT NULL,	[VOYAGE_NUM] [nvarchar](12) NULL,	[LEG_CD] [nvarchar](1) NULL) ON [PRIMARY]INSERT INTO [MG_VSLVOY_HEADER]VALUES ('762476','HNJAFR','0002E','E'),('696131','COSHEL','0003W','W'),('669246','HJPA','0007R','R'),('761970','ALSAB','0008Q','Q')[/code][/quote]Hi Chris,I am really sorry for this blunder and for wasting your precious time. Please find the updated ddl and sample data attached.</description><pubDate>Mon, 14 Jan 2013 02:10:47 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr]Hi PaulI'll try to have a look over the weekend. Meantime, it's the last 5 mins of my last day here - and it's BEERTIME!CheersChrisM[/quote]Voluntary job change? Or???</description><pubDate>Sun, 13 Jan 2013 21:49:22 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>Hi PaulYour sample data set doesn't have a matching row between MG_VSLVOY_HEADER and MG_VSLVOY_PORT_CONTROL:[code="sql"]SELECT ncv.*,  vh.VSLVOY_HEADER_IDFROM NCV_BL ncvJOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD --JOIN MG_VSLVOY_PORT_CONTROL vpc --	ON vh.VSLVOY_HEADER_ID = vpc.VSLVOY_HEADER_ID --WHERE NOT EXISTS (--	SELECT 1 --	FROM MG_VSLVOY_SCHEDULE vs --	WHERE vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID --	AND vs.PORT_CD NOT IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD)--	)--	AND BL_ID = '17231410' [/code]Here's your sample data script for those who don't like attachments:[code="sql"]-- sample data scriptCREATE TABLE [dbo].[NCV_BL](	[BL_ID] [decimal](10, 0) NOT NULL,	[POL_LOCATION_CD] [nvarchar](5) NULL,	[POD_LOCATION_CD] [nvarchar](5) NULL,	[SAISAN_VESSEL_CD] [nvarchar](10) NULL,	[SAISAN_VOYAGE_CD] [nvarchar](12) NULL,	[SAISAN_LEG_CD] [nvarchar](1) NULL,	[TEU] [decimal] (10,3) null,	[PLACE_OF_RECEIPT] [nvarchar](5) NULL,	[PLACE_OF_DELIVERY] [nvarchar](5) NULL,	[SAISAN_MONTH] [nvarchar](6) NULL) ON [PRIMARY]INSERT INTO [NCV_BL]VALUES ('17231410','DKAAR','IDJKT','HNJAFR','0002E','E', '4','DEYTN','EGPSW','200802')CREATE TABLE [dbo].[MG_VSLVOY_PORT_CONTROL](	[VSLVOY_HEADER_ID] [numeric](10, 0) NOT NULL,	[VSLVOY_SCHEDULE_ID] [numeric](10, 0) NOT NULL) ON [PRIMARY]INSERT INTO [MG_VSLVOY_PORT_CONTROL]VALUES ('1629990', '1342190'),('1629991', '1342191'),('1629992', '1342192'),	('1629993', '1342193'),('1629994', '1342194'),('1629995', '1342195'),	('1629996', '1342196'),('1629997', '1342197'),('1630001', '1342201'),	('1629998', '1379563'),('1629999', '1379564'),('1630000', '1379565')		CREATE TABLE [dbo].[MG_VSLVOY_SCHEDULE](	[VSLVOY_SCHEDULE_ID] [numeric](10, 0) NOT NULL,	[PORT_CD] [varchar](5) NOT NULL) ON [PRIMARY]INSERT INTO [MG_VSLVOY_SCHEDULE]VALUES ('1342190','DEHAM'),('1342191','NLRTM'),('1342192','FRLEH'),('1342193','ESALG'),('1342194','EGSUZ'),('1342195','SGSIN'),	('1342196','CNYTN'),('1342197','HKHKG'),('1342201','CNSHA'),('1379563','CNTXG'),('1379564','KRKAN'),('1379565','KRPUS')		CREATE TABLE [dbo].[MG_VSLVOY_HEADER](	[VSLVOY_HEADER_ID] [numeric](10, 0) NOT NULL,	[VESSEL_CD] [nvarchar](10) NOT NULL,	[VOYAGE_NUM] [nvarchar](12) NULL,	[LEG_CD] [nvarchar](1) NULL) ON [PRIMARY]INSERT INTO [MG_VSLVOY_HEADER]VALUES ('762476','HNJAFR','0002E','E'),('696131','COSHEL','0003W','W'),('669246','HJPA','0007R','R'),('761970','ALSAB','0008Q','Q')[/code]</description><pubDate>Sun, 13 Jan 2013 02:06:24 GMT</pubDate><dc:creator>ChrisM@home</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr]Hi PaulI'll try to have a look over the weekend. Meantime, it's the last 5 mins of my last day here - and it's BEERTIME!CheersChrisM[/quote]Thanks a lot Chris, whenever you have the time. take care and Cheers !!!gotta have some beer too when I get back home.</description><pubDate>Fri, 11 Jan 2013 09:46:24 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>Hi PaulI'll try to have a look over the weekend. Meantime, it's the last 5 mins of my last day here - and it's BEERTIME!CheersChrisM</description><pubDate>Fri, 11 Jan 2013 09:40:58 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr][quote][b]pwalter83 (1/11/2013)[/b][hr][quote][b]ChrisM@Work (1/11/2013)[/b][hr]I think this better fits the spec:[code="sql"]SELECT SUM(teu) AS TEUFROM NCV_BL ncvJOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD JOIN MG_VSLVOY_PORT_CONTROL vpc 	ON vh.VSLVOY_HEADER_ID = vpc.VSLVOY_HEADER_ID WHERE NOT EXISTS (	SELECT 1 	FROM MG_VSLVOY_SCHEDULE vs 	WHERE vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID 	AND vs.PORT_CD NOT IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD)	)	AND BL_ID = '17231410' [/code][/quote]Thanks for your reply.Your query shows the SUM of TEU as NULL, it should be 4. Am I missing something ?[/quote]Can't tell without access to your data. This is how I'd do it. Work your way through the tables, using sometablealias.BL_ID = '17231410'.[/quote]Thanks Chris,I tried but could'nt get around to change the result of TEU as per your message. It still shows up as NULL for whatever reason and should come out to 4 as per the sample data in my attached query.</description><pubDate>Fri, 11 Jan 2013 09:39:01 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]pwalter83 (1/11/2013)[/b][hr][quote][b]ChrisM@Work (1/11/2013)[/b][hr]I think this better fits the spec:[code="sql"]SELECT SUM(teu) AS TEUFROM NCV_BL ncvJOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD JOIN MG_VSLVOY_PORT_CONTROL vpc 	ON vh.VSLVOY_HEADER_ID = vpc.VSLVOY_HEADER_ID WHERE NOT EXISTS (	SELECT 1 	FROM MG_VSLVOY_SCHEDULE vs 	WHERE vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID 	AND vs.PORT_CD NOT IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD)	)	AND BL_ID = '17231410' [/code][/quote]Thanks for your reply.Your query shows the SUM of TEU as NULL, it should be 4. Am I missing something ?[/quote]Can't tell without access to your data. This is how I'd do it. Work your way through the tables, using sometablealias.BL_ID = '17231410'.</description><pubDate>Fri, 11 Jan 2013 08:50:51 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr]I think this better fits the spec:[code="sql"]SELECT SUM(teu) AS TEUFROM NCV_BL ncvJOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD JOIN MG_VSLVOY_PORT_CONTROL vpc 	ON vh.VSLVOY_HEADER_ID = vpc.VSLVOY_HEADER_ID WHERE NOT EXISTS (	SELECT 1 	FROM MG_VSLVOY_SCHEDULE vs 	WHERE vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID 	AND vs.PORT_CD NOT IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD)	)	AND BL_ID = '17231410' [/code][/quote]Thanks for your reply.Your query shows the SUM of TEU as NULL, it should be 4. Am I missing something ?</description><pubDate>Fri, 11 Jan 2013 08:33:08 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>I think this better fits the spec:[code="sql"]SELECT SUM(teu) AS TEUFROM NCV_BL ncvJOIN MG_VSLVOY_HEADER vh 	ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD	AND ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM	AND ncv.saisan_LEG_CD = vh.LEG_CD JOIN MG_VSLVOY_PORT_CONTROL vpc 	ON vh.VSLVOY_HEADER_ID = vpc.VSLVOY_HEADER_ID WHERE NOT EXISTS (	SELECT 1 	FROM MG_VSLVOY_SCHEDULE vs 	WHERE vs.VSLVOY_SCHEDULE_ID = vpc.VSLVOY_SCHEDULE_ID 	AND vs.PORT_CD NOT IN (ncv.POL_LOCATION_CD, ncv.POD_LOCATION_CD)	)	AND BL_ID = '17231410' [/code]</description><pubDate>Fri, 11 Jan 2013 08:23:45 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr]Why do you need to join to the other tables if they aren't contributing to the query, either as output or as a filter?[/quote]This is because of the following requirement:Calculate the SUM of NCV_BL.TEU where NCV_BL.POL_LOCATION_CD or NCV_BL.POD_LOCATION_CD is not present in MG_VSLVOY_SCHEDULE.PORT_CD for the SAISAN_VESSEL_CD, SAISAN_VOYAGE_CD and SAISAN_LEG_CD in NCV_BL.It would have been simpler otherwise.</description><pubDate>Fri, 11 Jan 2013 08:14:03 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>Why do you need to join to the other tables if they aren't contributing to the query, either as output or as a filter?</description><pubDate>Fri, 11 Jan 2013 08:06:06 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr][quote][b]pwalter83 (1/11/2013)[/b][hr][quote][b]ChrisM@Work (1/11/2013)[/b][hr]Paul, which table contains column 'teu'? (and 'BL_ID')Put that table first in the FROM list and inner join the other tables. Comment out all of the joins to the other tables and run the query, then uncomment one by one. This should help you to identify if one or more joins have missing criteria. Since you are only working with between 4 and 48 rows, it might help you to include some columns from the other tables in the output list. Start with the columns you are joining on and don't forget to remove the aggregate function from the output or you will get an error.[/quote]Thanks Chris, The thing is NCV_BL table has only one row of data and it includes the column - TEU with the value 4. So if a SUM of TEU is done from this table it should only show as 4 but as I am joining to some the tables as per the requirement, I get the SUM as 48 (duplicate values due to 12 rows in the MG_VSLVOY_SCHEDULE table - 4*12 = 48).Is there a way to handle this query so that only the actual TEU is accounted for and not the duplicates ?Thanks,Paul[/quote]Yes - SELECT teu FROM NCV_BL Have you posted the whole query?[/quote]This wont work as it would show up 12 rows of data which is not what I want. I still need to join to to other tables as per the requirement.This query is a example and is only a part of the bigger report, the other parts are working okay. </description><pubDate>Fri, 11 Jan 2013 07:48:41 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]pwalter83 (1/11/2013)[/b][hr][quote][b]ChrisM@Work (1/11/2013)[/b][hr]Paul, which table contains column 'teu'? (and 'BL_ID')Put that table first in the FROM list and inner join the other tables. Comment out all of the joins to the other tables and run the query, then uncomment one by one. This should help you to identify if one or more joins have missing criteria. Since you are only working with between 4 and 48 rows, it might help you to include some columns from the other tables in the output list. Start with the columns you are joining on and don't forget to remove the aggregate function from the output or you will get an error.[/quote]Thanks Chris, The thing is NCV_BL table has only one row of data and it includes the column - TEU with the value 4. So if a SUM of TEU is done from this table it should only show as 4 but as I am joining to some the tables as per the requirement, I get the SUM as 48 (duplicate values due to 12 rows in the MG_VSLVOY_SCHEDULE table - 4*12 = 48).Is there a way to handle this query so that only the actual TEU is accounted for and not the duplicates ?Thanks,Paul[/quote]Yes - SELECT teu FROM NCV_BL Have you posted the whole query?</description><pubDate>Fri, 11 Jan 2013 07:30:10 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]ChrisM@Work (1/11/2013)[/b][hr]Paul, which table contains column 'teu'? (and 'BL_ID')Put that table first in the FROM list and inner join the other tables. Comment out all of the joins to the other tables and run the query, then uncomment one by one. This should help you to identify if one or more joins have missing criteria. Since you are only working with between 4 and 48 rows, it might help you to include some columns from the other tables in the output list. Start with the columns you are joining on and don't forget to remove the aggregate function from the output or you will get an error.[/quote]Thanks Chris, The thing is NCV_BL table has only one row of data and it includes the column - TEU with the value 4. So if a SUM of TEU is done from this table it should only show as 4 but as I am joining to some the tables as per the requirement, I get the SUM as 48 (duplicate values due to 12 rows in the MG_VSLVOY_SCHEDULE table - 4*12 = 48).Is there a way to handle this query so that only the actual TEU is accounted for and not the duplicates ?Thanks,Paul</description><pubDate>Fri, 11 Jan 2013 07:24:53 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>[quote][b]Andy Hyslop (1/11/2013)[/b][hr]HiYour DDL seems to be missing VOYAGE_NUM in the table MG_VSLVOY_HEADER Andy[/quote]Sorry about that, its there now.</description><pubDate>Fri, 11 Jan 2013 07:19:52 GMT</pubDate><dc:creator>pwalter83</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>Paul, which table contains column 'teu'? (and 'BL_ID')Put that table first in the FROM list and inner join the other tables. Comment out all of the joins to the other tables and run the query, then uncomment one by one. This should help you to identify if one or more joins have missing criteria. Since you are only working with between 4 and 48 rows, it might help you to include some columns from the other tables in the output list. Start with the columns you are joining on and don't forget to remove the aggregate function from the output or you will get an error.</description><pubDate>Fri, 11 Jan 2013 07:12:22 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>HiYour DDL seems to be missing VOYAGE_NUM in the table MG_VSLVOY_HEADER Andy</description><pubDate>Fri, 11 Jan 2013 06:14:48 GMT</pubDate><dc:creator>Andy Hyslop</dc:creator></item><item><title>SQL Query</title><link>http://www.sqlservercentral.com/Forums/Topic1405934-391-1.aspx</link><description>Hi,I have an issue with the SQL query I have created. The result that this query should generate should be TEU = 4 instead of the 48 that it shows:-----------------------------------------------------------select SUM(teu) as TEUFROM NCV_BL NCVJOIN         MG_VSLVOY_HEADER AS vh                ON ncv.saisan_VESSEL_CD = vh.VESSEL_CD              and ncv.saisan_VOYAGE_CD = vh.VOYAGE_NUM              and ncv.saisan_LEG_CD = vh.LEG_CD                                   JOIN    MG_VSLVOY_PORT_CONTROL AS vpc ON vh.VSLVOY_HEADER_ID = vpc.VSLVOY_HEADER_ID  JOIN    MG_VSLVOY_SCHEDULE AS vs ON vpc.VSLVOY_SCHEDULE_ID   = vs.VSLVOY_SCHEDULE_IDand     NCV.POL_LOCATION_CD  NOT IN (PORT_CD) --or NCV.POD_LOCATION_CD NOT IN (PORT_CD))and  BL_ID = '17231410'    ----------------------------------------------Would somone please tell what changes I need to make in the above query to get the correct result ? Please find the ddl and sample data for the relevant tables attached.Thanks,Paul</description><pubDate>Fri, 11 Jan 2013 05:35:30 GMT</pubDate><dc:creator>pwalter83</dc:creator></item></channel></rss>