Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Reply
 
LinkBack Thread Tools Display Modes
Old 07-01-2007, 9:32 PM   #1
jared.pohl@gmail.com
 
jared.pohl@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

Hi Guys,

I am currently working on a putting together an sql query that
calculates, given an index, the percentage returns over a given period.



I have written the qeury that can calculate the percentage return.
However, the problem that I have is trying to combine these queries
into one large query that will return the date and the different
returns (1month, 3month, 6month......5year) at that point in time..


The following SQL returns the portfolio code, the date and the 3 month
return, (the sql was written to work in ms access.. but a mysql answer
would be fine..)


SELECT t1.PORTFOLIO_CODE, t1.DATE,
((t1.GROSS_INDEX-t2.GROSS_INDEX)/t2.GROSS_INDEX) AS 3MONTH_RETURN
FROM PORTFOLIO_PERFORMANCE AS t1
LEFT JOIN (SELECT PORTFOLIO_CODE, DATE AS ORIG_DATE_T2,
dateadd('d',-1,dateserial(Year(ORIG_DATE_T2), MONTH(ORIG_DATE_T2)+4,
1)) AS NEW_DATE_T2, GROSS_INDEX FROM PORTFOLIO_PERFORMANCE) AS t2
ON (t1.DATE=t2.new_DATE_T2) AND (t1.PORTFOLIO_CODE=t2.PORTFOLIO_CODE);


I know that for each return, i need to effectively offset the table,
portfolio_performance with itself by the period im calculating the
returns for and then join them. but how do i do this multiple times?


Hope this question makes sense, any help would be appreciated, thanks
alot!!!

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Advertisements
Old 07-01-2007, 9:32 PM   #2
Bill Karwin
 
Bill Karwin's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

jared.pohl@gmail.com wrote:
> SELECT t1.PORTFOLIO_CODE, t1.DATE,
> ((t1.GROSS_INDEX-t2.GROSS_INDEX)/t2.GROSS_INDEX) AS 3MONTH_RETURN
> FROM PORTFOLIO_PERFORMANCE AS t1
> LEFT JOIN (SELECT PORTFOLIO_CODE, DATE AS ORIG_DATE_T2,
> dateadd('d',-1,dateserial(Year(ORIG_DATE_T2), MONTH(ORIG_DATE_T2)+4,
> 1)) AS NEW_DATE_T2, GROSS_INDEX FROM PORTFOLIO_PERFORMANCE) AS t2
> ON (t1.DATE=t2.new_DATE_T2) AND (t1.PORTFOLIO_CODE=t2.PORTFOLIO_CODE);


FWIW, this uses some MS Access specific functions. DATEADD() and
DATESERIAL() do not exist in this form in MySQL.

Also, what happens if there does not exist a row for the date 3 months
ago? The subquery for t2 will return an empty set, and the LEFT JOIN
will make that return NULL for t2 columns. Thus your calculations
involving t2 columns will yield NULL. That may be okay, but I wanted to
bring your attention to it.

> I know that for each return, i need to effectively offset the table,
> portfolio_performance with itself by the period im calculating the
> returns for and then join them. but how do i do this multiple times?


Any time you need to base calculations on more than one row from the
same table, you need to do a self-join for each additional row.

SELECT curr.portfolio_code, curr.`date`,
(curr.gross_index-mo1.gross_index)/mo1.gross_index AS 1MONTH_RETURN,
(curr.gross_index-mo3.gross_index)/mo3.gross_index AS 3MONTH_RETURN,
(curr.gross_index-mo6.gross_index)/mo6.gross_index AS 6MONTH_RETURN,
(curr.gross_index-yr1.gross_index)/yr1.gross_index AS 1YEAR_RETURN,
(curr.gross_index-yr5.gross_index)/yr5.gross_index AS 5YEAR_RETURN
FROM portfolio_performance AS curr
LEFT OUTER JOIN portfolio_performance AS mo1
ON curr.`date` = mo1.`date` + INTERVAL 1 MONTH
LEFT OUTER JOIN portfolio_performance AS mo3
ON curr.`date` = mo3.`date` + INTERVAL 3 MONTH
LEFT OUTER JOIN portfolio_performance AS mo6
ON curr.`date` = mo6.`date` + INTERVAL 6 MONTH
LEFT OUTER JOIN portfolio_performance AS yr1
ON curr.`date` = yr1.`date` + INTERVAL 5 YEAR
LEFT OUTER JOIN portfolio_performance AS yr5
ON curr.`date` = yr5.`date` + INTERVAL 5 YEAR

This uses plain ANSI SQL syntax, and no subqueries, so it should be
pretty portable. Except for the use of back-ticks for MySQL delimited
identifiers.

Regards,
Bill K.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #3
jared.pohl@gmail.com
 
jared.pohl@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

bill

thanks for the help!! i can see that it isnt that complex of a query...
however i doesnt seem that i can add more than one outer join because
it says my syntax is incorrect.. despite copying and pasting correct
syntax in!!

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #4
Bill Karwin
 
Bill Karwin's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

jared.pohl@gmail.com wrote:
> however i doesnt seem that i can add more than one outer join because
> it says my syntax is incorrect.. despite copying and pasting correct
> syntax in!!


What version of MySQL are you using?

Can you show the exact error message, so we can see what part of the
statement causes the syntax error?

Regards,
Bill K.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #5
jared.pohl@gmail.com
 
jared.pohl@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

Im using microsoft access at the moment ( eww, i know!) to develop this
database then once all the reports are constructed (using crystal) we
are going to port it to a mysql server..

either way this works..

SELECT
curr.portfolio_code,
curr.date,
(curr.gross_index-mo1.gross_index)/mo1.gross_index AS 1MONTH_RETURN
FROM portfolio_performance AS curr
LEFT OUTER JOIN portfolio_performance AS mo1
ON (curr.date =
dateserial(year(dateadd('d',-1,dateserial(Year(mo1.date),
MONTH(mo1.date)+2, 1))),
month(dateadd('d',-1,dateserial(Year(mo1.date), MONTH(mo1.date)+2,
1))),day(dateadd('d',-1,dateserial(Year(mo1.date),MONTH(mo1.date)+2,
1)))))
AND (curr.portfolio_code = mo1.portfolio_code);

but the second i add the second left outer join (using copy + paste,
then changing the relevent name references, it freaks out..

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #6
jared.pohl@gmail.com
 
jared.pohl@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

Im using microsoft access at the moment ( eww, i know!) to develop this
database then once all the reports are constructed (using crystal) we
are going to port it to a mysql server..

either way this works..

SELECT
curr.portfolio_code,
curr.date,
(curr.gross_index-mo1.gross_index)/mo1.gross_index AS 1MONTH_RETURN
FROM portfolio_performance AS curr
LEFT OUTER JOIN portfolio_performance AS mo1
ON (curr.date =
dateserial(year(dateadd('d',-1,dateserial(Year(mo1.date),
MONTH(mo1.date)+2, 1))),
month(dateadd('d',-1,dateserial(Year(mo1.date), MONTH(mo1.date)+2,
1))),day(dateadd('d',-1,dateserial(Year(mo1.date),MONTH(mo1.date)+2,
1)))))
AND (curr.portfolio_code = mo1.portfolio_code);

but the second i add the second left outer join (using copy + paste,
then changing the relevent name references, it freaks out..

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #7
Bill Karwin
 
Bill Karwin's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

jared.pohl@gmail.com wrote:
> Im using microsoft access at the moment ( eww, i know!)


This is a MySQL newsgroup. You might want to ask Access-specific
questions on an Access newsgroup. You're more likely to get expert help
and accurate answers there.

When I google, I see that there are a bunch of weird behaviors in Access
regarding outer joins, that make its behavior different from MySQL. But
I can't discern a pattern. It might also be Access version dependent.

Regards,
Bill K.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 07-01-2007, 9:32 PM   #8
jared.pohl@gmail.com
 
jared.pohl@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Financial Returns

thanks bill. I have googled, and posted on numerous newsgroups
however, you are the only person whos helped me..

thanks for your help anyway!

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Featured Websites
Free Space
Free Space
Free Space Free Space
Reply
Tags: ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Financial Freedom Success Managers RJE Consulting Affiliate Programs 0 06-12-2007 8:19 PM
Leading Financial Resources & Software >> Finance Confidential Building An Internet Business 0 05-29-2007 3:10 AM
<== Click your way to FINANCIAL FREEDOM ==> Green Notes Building An Internet Business 0 05-29-2007 2:28 AM
Financial Security Samantha Building An Internet Business 0 05-29-2007 1:41 AM
Free content feed on financial keywords - loans, mortgages... etc FURiON Building An Internet Business 0 08-13-2006 2:29 AM


Featured Websites




All times are GMT +1. The time now is 5:14 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98