How to change this select statement?

  • SELECT SUM(ISNULL(ord_totalweight, 0)) AS Expr1

    FROM dbo.orderheader WITH (NOLOCK)

    WHERE (mov_number = TripSegment. 'Move Number')) AS [Total Weight],(SELECT SUM(ISNULL(ord_totalpieces, 0)) AS Expr1

    FROM dbo.orderheader WITH (NOLOCK)

    WHERE (mov_number = TripSegment. 'Move Number')) AS [Total Pieces],

    This statement here references the orderheader table to retrieve information about the mov_number's weight information. The weight information here is incomplete and the DB is not normalized - so a better source for the data has been found in a dbo.stops table.

    I have wrote this statement below, which references the stops table that seems to be the ultimate authority for ord_totalweight as far as I can see. It pulls...the right information at least.

    select SUM(ord_totalweight) AS Weight, SUM(ord_totalpieces) AS Pieces from orderheader where ord_hdrnumber in (select distinct ord_hdrnumber from stops where mov_number = '213135')

    How can I rewrite the original statement to reference the stops table? I used mov_number 213135 as an example in my query above. because mov_number 213135 does not have its weight information stored in dbo.orderheader. 213135 does have it stored in dbo.stops though.

  • rpringnitz (8/28/2013)


    SELECT SUM(ISNULL(ord_totalweight, 0)) AS Expr1

    FROM dbo.orderheader WITH (NOLOCK)

    WHERE (mov_number = TripSegment. 'Move Number')) AS [Total Weight],(SELECT SUM(ISNULL(ord_totalpieces, 0)) AS Expr1

    FROM dbo.orderheader WITH (NOLOCK)

    WHERE (mov_number = TripSegment. 'Move Number')) AS [Total Pieces],

    This statement here references the orderheader table to retrieve information about the mov_number's weight information. The weight information here is incomplete and the DB is not normalized - so a better source for the data has been found in a dbo.stops table.

    I have wrote this statement below, which references the stops table that seems to be the ultimate authority for ord_totalweight as far as I can see. It pulls...the right information at least.

    select SUM(ord_totalweight) AS Weight, SUM(ord_totalpieces) AS Pieces from orderheader where ord_hdrnumber in (select distinct ord_hdrnumber from stops where mov_number = '213135')

    How can I rewrite the original statement to reference the stops table? I used mov_number 213135 as an example in my query above. because mov_number 213135 does not have its weight information stored in dbo.orderheader. 213135 does have it stored in dbo.stops though.

    Read what you posted and ask yourself if you honestly think you have provided any level of detail. About all anybody can do is tell you to select from the stops table instead of whichever table you are currently selecting your data from that has the wrong information.

    I would also suggest that you remove those NOLOCK hints. Do you know all the details about what that hint does? Is it ok if your results have missing or duplicate data?

    http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/[/url]

    If you want some actual assistance there are lots of people around here willing and able to help. In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 2 posts - 1 through 1 (of 1 total)

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