Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Query


SQL Query

Author
Message
pwalter83
pwalter83
Say Hey Kid
Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)

Group: General Forum Members
Points: 682 Visits: 2131
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
Attachments
ddl.txt (13 views, 2.00 KB)
Andy Hyslop
Andy Hyslop
Say Hey Kid
Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)Say Hey Kid (706 reputation)

Group: General Forum Members
Points: 706 Visits: 3023
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
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
pwalter83
pwalter83
Say Hey Kid
Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)

Group: General Forum Members
Points: 682 Visits: 2131
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.
pwalter83
pwalter83
Say Hey Kid
Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)

Group: General Forum Members
Points: 682 Visits: 2131
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
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
pwalter83
pwalter83
Say Hey Kid
Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)

Group: General Forum Members
Points: 682 Visits: 2131
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.
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
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
pwalter83
pwalter83
Say Hey Kid
Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)Say Hey Kid (682 reputation)

Group: General Forum Members
Points: 682 Visits: 2131
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.
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9025 Visits: 19036
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search