Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with query against AdventureWorks database - Update sales price

Posted on 2010-08-28
8
Medium Priority
?
612 Views
Last Modified: 2012-05-10
Need a hand with this query:

Update the Sale Price to the Recommended Sale Price of those Sales occurring between 6/10/2004 and 6/20/2004.

I've got this so far:

SELECT DISTINCT p.ProductID, p.Name, p.ListPrice, sd.UnitPrice As 'Selling Price'
FROM Sales.SalesOrderDetail AS sd
JOIN Production.Product AS p
ON sd.ProductID = p.ProductID AND sd.UnitPrice < p.ListPrice
WHERE ...

Thanks!
0
Comment
Question by:John500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33551267
You would use an update with a JOIN:


{deleted by Mod_MarlEE, 2010-08-29 18:20}

Open in new window

0
 

Author Comment

by:John500
ID: 33551386
Ok, great - I'll look at this
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 33551390
Good.  Once you understand this, it should help you with some of the other queries you are trying to figure out ...

This one is in the AdventureWorks structure, so should run "as-is".  The important thing to gather here is that datetime conversion of '2004-06-10' is '2004-06-10 00:00:00' which is midnight on June 10.  The best way to be assured you get all the timestamps of a given day is to look at rows that are < midnight of the next day; therefore, you wanted '2004-06-20', but what I have written is < '2004-06-21' for that reason.

Hope that makes sense.
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33557457
John,

Just out of curiosity, are these homework questions?  Given how long have been on this site (unless you are sharing the login), are you embarking on re-education?
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 2000 total points
ID: 33561654
Hmmmm.... they are most interesting questions. At least this one has signs of an attempted answer by John500

Need to include SalesOrderHeader to get the OrderDate (or one of the other dates) into the equation.

So, a starting point would be :


SELECT p.ProductID, p.Name, p.ListPrice, sd.UnitPrice As 'Selling Price'
FROM Sales.SalesOrderDetail AS sd
INNER JOIN Sales.SalesOrderHeader AS sh on sh.salesorderid = sd.salesorderid
INNER JOIN Production.Product AS p ON sd.ProductID = p.ProductID
WHERE sh.orderdate between '20040610' and '20040620'


Now, there happens to be no price difference from the list price, so the extra qualifiers simply return an empty result set.

To change that from a select to an update, then just change the select line...

update sd set sd.UnitPrice = p.ListPrice
FROM Sales.SalesOrderDetail AS sd
INNER JOIN Sales.SalesOrderHeader AS sh on sh.salesorderid = sd.salesorderid
INNER JOIN Production.Product AS p ON sd.ProductID = p.ProductID
WHERE sh.orderdate between '20040610' and '20040620'

0
 

Author Comment

by:John500
ID: 34135301
In regard to the 'recommended sale price' aspect of this question, it appears a referrence is being made to the Database Engine Tuning Advisor.  The following link points to a discussion which illustrates how recommendations are made:

http://sqlblog.com/blogs/ben_nevarez/archive/2009/11/11/database-engine-tuning-advisor-and-the-query-optimizer.aspx

As stated in the discussion, "When the DTA analysis finishes, run this query to inspect the contents of the msdb..DTA_reports_query table:  select * from msdb..DTA_reports_query"

StatementString                                    |      CurrentCost       |       Recommended Cost
-----------------------------------------------------------------------------------------------------
select * from dbo.SalesOrderDetail             1.2434                       0.00328799
where productid = 897

Although this discussion reveals where the recommended price comes from, I'm not sure how to incorporate the recommendation into and UPDATE statement.

Any input on this?  Thanks !




0
 

Author Comment

by:John500
ID: 34135579
Scratch that last post.  I realize now this recommendation deals with indexes and query execution time...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question