SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to change this select statement?


How to change this select statement?

Author
Message
rpringnitz
rpringnitz
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 75
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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26159 Visits: 17539
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 Modens 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)
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