Solved

SQL sp help.

Posted on 2007-04-06
6
887 Views
Last Modified: 2012-06-21
I have a db with 2 tables.  they are related by a specific column.
They each have an id row that corresponds to each other.  The one table holds the company info and then the other table holds pricing info.  I need to run through all the rows in the pricing table for a specifc date or date range.  And then for the max date for the specific id then I have to update that date in the company table in a last date column that is also assoc with that id.  Any ideas on a sp to do this?
0
Comment
Question by:casit
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:ksaul
ID: 18868026
Can you post the schema and some sample data from the two tables?  A possible solution is a join to a subquery that gets the max date.  Something like:

UPDATE company
SET company.LastDate = MaxDates.MaxDate
FROM company
INNER JOIN
  (SELECT ID, MAX(date) MaxDate
  FROM pricing
  WHERE date  = <specific date>
  --WHERE date BETWEEN <date1> AND <date2>
  GROUP BY ID) AS MaxDates ON MaxDates.ID = company.ID
0
 
LVL 10

Expert Comment

by:ksaul
ID: 18882375
Was this any help?  If you give me the schema I can give you a more specific answer.  On 2005 you can also use a CTE (Common table expression) to make the query more readible.  I can give an example of that as well if you would like.
0
 

Author Comment

by:casit
ID: 18885877
Yeah that would be good.  Sry for not getting back to you just other things took a higher priority.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 10

Accepted Solution

by:
ksaul earned 500 total points
ID: 18891882
Here is the CTE example:

WITH MaxDates (ID, MaxDate) AS
 (SELECT ID, MAX(date) MaxDate
  FROM pricing
  WHERE date  = <specific date>
  --WHERE date BETWEEN <date1> AND <date2>
  GROUP BY ID )

UPDATE Company
SET Company.LastDate = MaxDates.MaxDate
FROM Company
INNER JOIN MaxDates on Company.ID = MaxDates.ID
0
 
LVL 10

Expert Comment

by:ksaul
ID: 18952233
Did that work for you?
0
 

Author Comment

by:casit
ID: 18986266
I actually figured this out by using a function in my view to return the latest date.  That way I don't have to change any actual data.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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