LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2005, 10:28 AM   #1
jabfinger
Member
 
Registered: Jan 2004
Location: USA, Pa
Distribution: Gentoo, Fedora Core 3, Mepis, Vector
Posts: 83

Rep: Reputation: 15
MySQL head scramble


Hi,

I have data in a table like this:

(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

IF for Paul, I subtract:

week1 Sold(120) - week0 Sold( 0 ) = week1 Sold(120)
week2 Sold(245) - week1 Sold(120) = week2 Sold(125)
week3 Sold(375) - week2 Sold(245) = week3 Sold(130)

If for Fran, I subtract:

week1 Sold(105) - week0 Sold( 0 ) = week1 (105)
week2 Sold(250) - week1 Sold(105) = week2 (145)
week3 Sold(380) - week2 Sold(250) = week3 (130)

I need data like this:

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
 
Old 07-28-2005, 11:09 AM   #2
nouse66
Member
 
Registered: Feb 2003
Location: O.C., CA
Distribution: Kubuntu
Posts: 70

Rep: Reputation: 15
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.

Last edited by nouse66; 07-28-2005 at 11:10 AM.
 
Old 07-28-2005, 11:31 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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...
 
Old 07-28-2005, 11:58 AM   #4
nouse66
Member
 
Registered: Feb 2003
Location: O.C., CA
Distribution: Kubuntu
Posts: 70

Rep: Reputation: 15
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
 
Old 07-28-2005, 12:29 PM   #5
jabfinger
Member
 
Registered: Jan 2004
Location: USA, Pa
Distribution: Gentoo, Fedora Core 3, Mepis, Vector
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks Guys,

The week is actualy a timestamp in the real table.
I should have mentioned that.
I will attempt your suggestions.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My head is up MySQL riluve Linux - Newbie 3 01-21-2005 06:38 PM
Multi-monitor Issues on RH9, Geforce 4 Ti Dual Head + TNT2 Single Head the letter b Linux - Newbie 3 12-04-2004 11:23 PM
can any one give a hand on CSS problem(Content Scramble System) ypx Linux - Software 0 12-04-2004 04:10 AM
can any one give a hand on CSS problem(Content Scramble System) ypx Linux - Software 0 12-03-2004 08:02 PM
x86 Solaris 9 XSun and Matrox G550 dual-head... one head down, one to go. finegan Solaris / OpenSolaris 4 03-11-2003 12:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration