Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-07-13 : 08:55:36
|
I have a currency rate table with the following columnst_stdt = startdate for this type of conversiont_tcur = To Currencyt_fcur = From Currencyt_rate = Actual rate.The table keeps all existing rates. So when a new rate is added for example EUR to GBP then a new row is created for this particular type of conversion and old ones are also still kept in the table.So I want to pul out the max startdate for each type (ps am only dealing with four types EUR to GBP, GBP to EUR, USD to GBP and GBP to USD.example data at bottom of pageMy query seems to work and it pulls out only four rows (which is what I want), but i wanted to check I am doing it right.here's the query:-----------------------------------------Select t_stdt as "StartDate",t_tcur as "ToCurrency", t_fcur as "FromCurrency",t_rate as "Rate"From ttcmcs903530where t_crty = '7'and t_stdt in (SelectMAX(t_stdt) as "StartDate"From ttcmcs903530where t_crty = '7'group by t_tcur, t_crty, t_fcur)------------------------------------------2006-11-23 00:00:00.000 EUR GBP 0.6899932007-01-12 00:00:00.000 EUR GBP 0.672006-11-23 00:00:00.000 GBP EUR 1.449292007-01-12 00:00:00.000 GBP EUR 1.4925372007-01-19 00:00:00.000 GBP USD 0.5263162007-01-19 00:00:00.000 USD GBP 1.92007-10-05 00:00:00.000 EUR GBP 1.472007-10-05 00:00:00.000 GBP EUR 0.6802722008-03-11 00:00:00.000 EUR GBP 1.45632008-03-11 00:00:00.000 GBP EUR 0.6866722008-10-07 00:00:00.000 GBP USD 0.5611362008-10-07 00:00:00.000 USD GBP 1.78212008-11-04 00:00:00.000 USD GBP 1.579152008-11-04 00:00:00.000 GBP USD 0.6332522008-12-01 00:00:00.000 USD GBP 1.505222008-12-01 00:00:00.000 GBP USD 0.6643552009-02-03 00:00:00.000 EUR GBP 1.22009-02-03 00:00:00.000 GBP EUR 0.8333332009-02-03 00:00:00.000 USD GBP 1.44162009-02-03 00:00:00.000 GBP USD 0.6936742009-03-02 00:00:00.000 USD GBP 1.43152009-03-02 00:00:00.000 GBP USD 0.6985682010-01-01 00:00:00.000 EUR GBP 1.11152010-01-01 00:00:00.000 GBP EUR 0.8996852010-01-01 00:00:00.000 USD GBP 1.62010-01-01 00:00:00.000 GBP USD 0.6252009-02-04 00:00:00.000 EUR GBP 1.1052009-02-04 00:00:00.000 GBP EUR 0.904977 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-13 : 09:12:48
|
quote: My query seems to work and it pulls out only four rows (which is what I want), but i wanted to check I am doing it right.
try adding another line like and see how your query behave2010-01-02 00:00:00.000 USD GBP 1.7 you should find the max of t_stdt by t_tcur, t_crty, t_fcur and then INNER JOIN back to the original tableSelect t.t_stdt as [StartDate], t.t_tcur as [ToCurrency], t.t_fcur as [FromCurrency], t.t_rate as [Rate]From ttcmcs903530 t inner join ( Select t_crty, t_tcur, t_fcur, MAX(t_stdt) as [StartDate] From ttcmcs903530 group by t_tcur, t_crty, t_fcur ) m on t.t_crty = m.t_crty and t.t_stdt = m.[StartDate] and t.t_tcur = m.t_tcur and t.t_fcur = m.t_fcur KH[spoiler]Time is always against us[/spoiler] |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-13 : 09:15:00
|
since you are using SQL 2005, it will be simple just to use the row_number() with over(); with dataas( Select t.t_stdt as [StartDate], t.t_tcur as [ToCurrency], t.t_fcur as [FromCurrency], t.t_rate as [Rate], row_no = row_number() over (partition by t_crty, t_tcur, t_fcur order by t_stdt desc) From ttcmcs903530 t)select *from datawhere row_no = 1 KH[spoiler]Time is always against us[/spoiler] |
 |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2010-07-13 : 09:20:09
|
Thats really helpfulthanks |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-07-13 : 09:24:23
|
If you are going to do that for a single currency (i.e. "What's the current exchange rate for Currency=X" then I think its fine.If you are going to do multiple rows (e.g. "What was the current exchange rate for this Order, given the Order's Currency field AND a field for the date/time of the Order") then I think it will be inefficient, and you should perhaps look at ROW_NUMBER() ... OVER syntax instead.ROW_NUMBER() ... OVER would also have the benefit (for the single-row query above) of only returning ONE result row in the event that there were two, or more, rows in your [ttcmcs903530] currency rate table with identical date/time values (but you may already be preventing that with Primary Key or Unique constraint/index) |
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-07-13 : 09:26:11
|
|
 |
|
|
|
|
|
|