Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

SQL Query Expand / Collapse
Author
Message
Posted Friday, January 11, 2013 5:35 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 5:14 AM
Points: 530, Visits: 1,643
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 TEU

FROM NCV_BL NCV

JOIN 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_ID
and 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


  Post Attachments 
ddl.txt (13 views, 2.84 KB)
Post #1405934
Posted Friday, January 11, 2013 6:14 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 5:34 AM
Points: 689, Visits: 2,753
Hi

Your DDL seems to be missing VOYAGE_NUM in the table MG_VSLVOY_HEADER

Andy


==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
Post #1405964
Posted Friday, January 11, 2013 7:12 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 7,114, Visits: 13,472
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.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1406001
Posted Friday, January 11, 2013 7:19 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 5:14 AM
Points: 530, Visits: 1,643
Andy Hyslop (1/11/2013)
Hi

Your DDL seems to be missing VOYAGE_NUM in the table MG_VSLVOY_HEADER

Andy


Sorry about that, its there now.
Post #1406016
Posted Friday, January 11, 2013 7:24 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 5:14 AM
Points: 530, Visits: 1,643
ChrisM@Work (1/11/2013)
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.


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
Post #1406021
Posted Friday, January 11, 2013 7:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 7,114, Visits: 13,472
pwalter83 (1/11/2013)
ChrisM@Work (1/11/2013)
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.


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


Yes - SELECT teu FROM NCV_BL

Have you posted the whole query?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1406023
Posted Friday, January 11, 2013 7:48 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 5:14 AM
Points: 530, Visits: 1,643
ChrisM@Work (1/11/2013)
pwalter83 (1/11/2013)
ChrisM@Work (1/11/2013)
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.


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


Yes - SELECT teu FROM NCV_BL

Have you posted the whole query?



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.

Post #1406038
Posted Friday, January 11, 2013 8:06 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 7,114, Visits: 13,472
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?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1406053
Posted Friday, January 11, 2013 8:14 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 5:14 AM
Points: 530, Visits: 1,643
ChrisM@Work (1/11/2013)
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?


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.
Post #1406057
Posted Friday, January 11, 2013 8:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 7,114, Visits: 13,472
I think this better fits the spec:

SELECT SUM(teu) AS TEU

FROM NCV_BL ncv

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

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'



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1406071
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse