ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
(where week2 Sold is not the actual amount of week2
Sold but the acumulation of week1 + week2, and
the actual amount of week3 is a acumulation of
week2 + week3 Sold)
ID Name Week Sold
----------------------------
1 Paul week1 120
2 Fran week1 105
3 Paul week2 245
4 Fran week2 250
5 Paul week3 375
6 Fran week3 380
Name Week Sold
--------------------
Paul week1 120
Paul week2 125
Paul week3 130
Fran week1 105
Fran week2 145
Fran week3 130
So basicaly, I think, I have a YTD table
that shows weekly YTD data, and I need to work
backwards to find the weekly amounts that
people Sold.
If I could get the data in the form of weekly
totals it would make my life a lot easier,
but I can not.
I have bought a few books on MySQL and php, and have
read a gazillion tutorials on the web and I havn't
been able to figure this out on my own.
Im not asking for anyone to write the code, but
If someone could point me in the right direction
I would surely be grateful
i'm pretty sure i understand your problem...
what you need to do is loop through the weeks in order and keep track of the previous week so you can subtract. assuming you have an open mysql connection $db, i would do something like this for each person in the table:
PHP Code:
$result = mysql_query("select * from weekly where name='Fran' order by week",$db); $lastweek = 0; $sold = array(); while($row = mysql_fetch_object($result)) { $sold[$row->week] = $row->sold - $lastweek; $lastweek = $row->sold; }
print_r($sold);
you should get 105,145, and 130 in the $sold array.
You might be able to do some joins to get the data in the format you want directly in the SQL query too. This is just off the top of my head and totally untested, but might give you some ideas...
Code:
SELECT T1.Name, T1.Week, T1.Sold - T2.Sold as Sold
FROM Weekly T1
LEFT OUTER JOIN Weekly T2
ON T1.Name= T2.Name AND T2.Week= T1.Week - 1
WHERE T1.Name = 'Paul'
ORDER BY T1.Week
If the week is in a numeric format that you can subtract 1 from, something like the above should work. If it is really in a string form like "week1", then you might have to substitute my subtraction with some creative substring type stuff...
yeah, having a string for the week field really is the kicker. i went quickly to php because trying to think through the substrings in that join was making my head spin
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.