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

How to change this select statement? Expand / Collapse
Author
Message
Posted Wednesday, August 28, 2013 9:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, June 6, 2014 6:36 AM
Points: 4, Visits: 35
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.
Post #1489300
Posted Wednesday, August 28, 2013 12:57 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:25 AM
Points: 13,274, Visits: 12,106
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/

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1489387
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse