Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


function help


function help

Author
Message
SQL_Surfer
SQL_Surfer
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 1076
19table has following fields and data as follows

ClientNo ItemCode NewPrice PriceChangedDate
1 1234 205.00 8/1/2013
1 1234 200 7/30/2013
1 1234 195 7/2/2013
1 1234 190 6/30/2013

Whenever there is price changes, it records new price in this table.

I need to return the NewPrice for the given clientNo, ItemCode and InvoicedDate.

For e.g for InvocedDate of 7/1/2013, there may not be any records in the table with the PriceChanedDate.
However, it should retrun NewPrice of 190 as Price from 6/30 to 7/1 remained 190. So it should return 190
Any idea best way to write a function for this?
SQL_Surfer
SQL_Surfer
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 1076
19Table is a Typo. I meant MyTable.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16616 Visits: 17024
SQL_Surfer (8/12/2013)
19table has following fields and data as follows

ClientNo ItemCode NewPrice PriceChangedDate
1 1234 205.00 8/1/2013
1 1234 200 7/30/2013
1 1234 195 7/2/2013
1 1234 190 6/30/2013

Whenever there is price changes, it records new price in this table.

I need to return the NewPrice for the given clientNo, ItemCode and InvoicedDate.

For e.g for InvocedDate of 7/1/2013, there may not be any records in the table with the PriceChanedDate.
However, it should retrun NewPrice of 190 as Price from 6/30 to 7/1 remained 190. So it should return 190
Any idea best way to write a function for this?


Pretty sparse on details. You have been around here long enough to know we need more details than this.

Maybe it is as simple order by PriceChangedDate? Hard to know what you are looking for.

_______________________________________________________________

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)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24217 Visits: 37978
Based on the very vague details provided this is the in-line table valued function I would use:



CREATE FUNCTION dbo.ClientProductPrice (
@ClientNo INT,
@ItemCode INT,
@InvoiceDate DATETIME -- in SQL Server 2008 and later can be DATE
)
RETURNS TABLE
AS
RETURN(
WITH PriceRec AS (
SELECT
rn = ROW_NUMBER() OVER (ORDER BY PriceChangedDate DESC),
NewPrice
FROM
MyTable
WHERE
ClientNo = @ClientNo AND
ItemCode = @ItemCode AND
PriceChangedDate <= @InvoiceDate
)
SELECT NewPrice FROM PriceRec WHERE rn = 1
);




No promises that this will work as I didn't create a table to do any testing.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Snigdha Vartak
Snigdha Vartak
Mr or Mrs. 500
Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)Mr or Mrs. 500 (526 reputation)

Group: General Forum Members
Points: 526 Visits: 247
It works. I've tested it on SQL Server 2005 :-)
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