SQL Query

  • 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

  • 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

  • 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

  • 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.

  • 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

  • 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

  • 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.

  • 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

  • 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.

  • 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

  • ChrisM@Work (1/11/2013)


    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'

    Thanks for your reply.

    Your query shows the SUM of TEU as NULL, it should be 4. Am I missing something ?

  • pwalter83 (1/11/2013)


    ChrisM@Work (1/11/2013)


    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'

    Thanks for your reply.

    Your query shows the SUM of TEU as NULL, it should be 4. Am I missing something ?

    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'.

    “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

  • ChrisM@Work (1/11/2013)


    pwalter83 (1/11/2013)


    ChrisM@Work (1/11/2013)


    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'

    Thanks for your reply.

    Your query shows the SUM of TEU as NULL, it should be 4. Am I missing something ?

    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'.

    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.

  • Hi Paul

    I'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!

    Cheers

    ChrisM

    “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

  • ChrisM@Work (1/11/2013)


    Hi Paul

    I'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!

    Cheers

    ChrisM

    Thanks a lot Chris, whenever you have the time.

    take care and Cheers !!!

    gotta have some beer too when I get back home.

Viewing 15 posts - 1 through 15 (of 21 total)

You must be logged in to reply to this topic. Login to reply