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 |
DanielS
Starting Member
32 Posts |
Posted - 2013-06-26 : 22:06:24
|
Hi there,I am looking to get a few pieces of data and make a comparison.I have a FROM_DATE and a PRICE field in a table called HOLDINGS.I want to get the PRICE for last week and compare to today's PRICE, and I want to get the PRICE for the end of last month and compare to today's PRICE.So, I haveSELECT DATEADD(day,-7,max(FROM_DATE)) FROM HOLDINGSas last weeks date, andSELECT DATEADD(day,-1,DATEADD(month, DATEDIFF(month,0,max(FROM_DATE)),0)) FROM HOLDINGSas last months date.What code do I need to get results which look something along the lines of;FROM_DATE, PRICE, FROM_DATE_WEEK, PRICE_WEEK, PRICE_CHANGE_WEEK, FROM_DATE_MONTH, PRICE_MONTH, PRICE_CHANGE_MONTH |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-06-26 : 22:14:11
|
How does your data looks like ?You have a PRICE record for everyday ? KH[spoiler]Time is always against us[/spoiler] |
|
|
DanielS
Starting Member
32 Posts |
Posted - 2013-06-26 : 23:07:16
|
Yes, data is daily.quote: Originally posted by khtan How does your data looks like ?You have a PRICE record for everyday ? KH[spoiler]Time is always against us[/spoiler]
|
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-06-26 : 23:12:52
|
so you are comparing TODAY with LAST WEEK THIS DAY and END OF LAST MONTH ?can you post a sample of your data from the HOLDINGS table ? KH[spoiler]Time is always against us[/spoiler] |
|
|
DanielS
Starting Member
32 Posts |
Posted - 2013-06-26 : 23:18:25
|
Here is a cut of the data:FROM_DATE F_ISIN PRICE30/05/2013 AU3FN0014916 104.34999831/05/2013 AU3FN0014916 104.3481/06/2013 AU3FN0014916 104.3482/06/2013 AU3FN0014916 104.3483/06/2013 AU3FN0014916 104.3460014/06/2013 AU3FN0014916 104.3040015/06/2013 AU3FN0014916 104.2289966/06/2013 AU3FN0014916 104.1630027/06/2013 AU3FN0014916 104.1989978/06/2013 AU3FN0014916 104.1989979/06/2013 AU3FN0014916 104.19899710/06/2013 AU3FN0014916 104.19899711/06/2013 AU3FN0014916 104.18112/06/2013 AU3FN0014916 104.17400413/06/2013 AU3FN0014916 103.95999914/06/2013 AU3FN0014916 103.85299715/06/2013 AU3FN0014916 103.85299716/06/2013 AU3FN0014916 103.85299717/06/2013 AU3FN0014916 103.77718/06/2013 AU3FN0014916 103.73899819/06/2013 AU3FN0014916 103.77200320/06/2013 AU3FN0014916 103.72499821/06/2013 AU3FN0014916 103.57499722/06/2013 AU3FN0014916 103.57499723/06/2013 AU3FN0014916 103.57499724/06/2013 AU3FN0014916 103.45300325/06/2013 AU3FN0014916 103.45600126/06/2013 AU3FN0014916 103.656998quote: Originally posted by khtan so you are comparing TODAY with LAST WEEK THIS DAY and END OF LAST MONTH ?can you post a sample of your data from the HOLDINGS table ? KH[spoiler]Time is always against us[/spoiler]
|
|
|
DanielS
Starting Member
32 Posts |
Posted - 2013-06-26 : 23:25:04
|
And yes, compare today to last week and end of last month.So compare 26/7 price to 19/7 amd 26/7 to 31/5.quote: Originally posted by DanielS Here is a cut of the data:FROM_DATE F_ISIN PRICE30/05/2013 AU3FN0014916 104.34999831/05/2013 AU3FN0014916 104.3481/06/2013 AU3FN0014916 104.3482/06/2013 AU3FN0014916 104.3483/06/2013 AU3FN0014916 104.3460014/06/2013 AU3FN0014916 104.3040015/06/2013 AU3FN0014916 104.2289966/06/2013 AU3FN0014916 104.1630027/06/2013 AU3FN0014916 104.1989978/06/2013 AU3FN0014916 104.1989979/06/2013 AU3FN0014916 104.19899710/06/2013 AU3FN0014916 104.19899711/06/2013 AU3FN0014916 104.18112/06/2013 AU3FN0014916 104.17400413/06/2013 AU3FN0014916 103.95999914/06/2013 AU3FN0014916 103.85299715/06/2013 AU3FN0014916 103.85299716/06/2013 AU3FN0014916 103.85299717/06/2013 AU3FN0014916 103.77718/06/2013 AU3FN0014916 103.73899819/06/2013 AU3FN0014916 103.77200320/06/2013 AU3FN0014916 103.72499821/06/2013 AU3FN0014916 103.57499722/06/2013 AU3FN0014916 103.57499723/06/2013 AU3FN0014916 103.57499724/06/2013 AU3FN0014916 103.45300325/06/2013 AU3FN0014916 103.45600126/06/2013 AU3FN0014916 103.656998quote: Originally posted by khtan so you are comparing TODAY with LAST WEEK THIS DAY and END OF LAST MONTH ?can you post a sample of your data from the HOLDINGS table ? KH[spoiler]Time is always against us[/spoiler]
|
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-06-26 : 23:32:24
|
[code]declare @today date, @last_week_today date, @end_of_last_mth dateselect @today = getdate(), @last_week_today = dateadd(week, -1, getdate()), @end_of_last_mth = dateadd(month, datediff(month, 0, getdate()), -1)select h.F_ISIN, FROM_DATE = max(case when FROM_DATE = @today then FROM_DATE end), PRICE = max(case when FROM_DATE = @today then PRICE end), FROM_DATE_WEEK = max(case when FROM_DATE = @last_week_today then FROM_DATE end), PRICE_WEEK = max(case when FROM_DATE = @last_week_today then PRICE end), FROM_DATE_MONTH = max(case when FROM_DATE = @end_of_last_mth then FROM_DATE end), PRICE_MONTH = max(case when FROM_DATE = @end_of_last_mth then PRICE end)from HOLDINGS hwhere h.FROM_DATE in (@today, @last_week_today, @end_of_last_mth)group by h.F_ISIN[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-06-26 : 23:35:57
|
if today's data is not available yet, change to yesterdayselect @today = getdate() - 1, @last_week_today = dateadd(week, -1, getdate() - 1), @end_of_last_mth = dateadd(month, datediff(month, 0, getdate() - 1), -1) KH[spoiler]Time is always against us[/spoiler] |
|
|
DanielS
Starting Member
32 Posts |
Posted - 2013-06-27 : 00:08:02
|
Thanks, this looks good.Now what if I wanted to 'paramaterize' the FROM_DATE, ie I will be putting this code into SSRS and then building a report with FROM_DATE as the parameter. So the user should be able to select any date in time, eg 13/7/12 and get the prices for 6/7/12 (week prior) and 30/6/12 (prior month end).quote: Originally posted by khtan if today's data is not available yet, change to yesterdayselect @today = getdate() - 1, @last_week_today = dateadd(week, -1, getdate() - 1), @end_of_last_mth = dateadd(month, datediff(month, 0, getdate() - 1), -1) KH[spoiler]Time is always against us[/spoiler]
|
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2013-06-27 : 00:12:09
|
i am not familiar with SSRS, but you can change the following, the @last_week_today & @end_of_last_mth will be based on the @today to calculate the dateselect @today = getdate() - 1select @last_week_today = dateadd(week, -1, @today - 1), @end_of_last_mth = dateadd(month, datediff(month, 0, @today - 1), -1) KH[spoiler]Time is always against us[/spoiler] |
|
|
|
|
|
|
|