Solved

SQL sp help.

Posted on 2007-04-06
6
889 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Order by but want it in specific order 2 36
Error when saving to sql table a '/' 5 31
Help  needed 3 43
SQL Server - Set Field Values ito Zero Based on Related Table 4 44
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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