473,466 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with some PHP/mySQL

Hello,

I'll try to explain what I'm trying to accomplish here, hopefully not
too bad a task. Basically I have an existing table with data that will
be operated on to form new records etc...

for example:
id name slat slon elat elon dist
1 65 -10.32 136.64 -10.18 136.69 6556.52
2 65 -10.18 136.69 -10.16 136.72 1701.68
3 65 -10.16 136.72 -10.11 136.72 2106.7
4 66 -10.13 136.73 -10.05 136.79 4536.58
5 66 -10.05 136.79 -10.06 136.82 1082.5
6 66 -10.06 136.82 -10.01 136.84 2238.2

So what I need to do here is basically sum all of the values in the
dist column that correspond to any value with the same name value (eg,
only 65's), plus take both the slat & slon values from the first
distinct record (eg, the 1st 65 slat & slon combined with the last 65
elat & elon values....where 1st would be id#1, last would be id#3
etc.) and combine them with the last elat & elon values...

So from above, my resulting query would give me 2 results looking like
so...
id name slat slon elat elon totaldist
1 65 -10.32 136.64 -10.11 136.72 10364.9
2 66 -10.13 136.73 -10.01 136.84 7857.28
any help would be greatly appreciated, thanks!
Jul 25 '08 #1
5 1418
inexion wrote:
Hello,

I'll try to explain what I'm trying to accomplish here, hopefully not
too bad a task. Basically I have an existing table with data that will
be operated on to form new records etc...

for example:
id name slat slon elat elon dist
1 65 -10.32 136.64 -10.18 136.69 6556.52
2 65 -10.18 136.69 -10.16 136.72 1701.68
3 65 -10.16 136.72 -10.11 136.72 2106.7
4 66 -10.13 136.73 -10.05 136.79 4536.58
5 66 -10.05 136.79 -10.06 136.82 1082.5
6 66 -10.06 136.82 -10.01 136.84 2238.2

So what I need to do here is basically sum all of the values in the
dist column that correspond to any value with the same name value (eg,
only 65's), plus take both the slat & slon values from the first
distinct record (eg, the 1st 65 slat & slon combined with the last 65
elat & elon values....where 1st would be id#1, last would be id#3
etc.) and combine them with the last elat & elon values...

So from above, my resulting query would give me 2 results looking like
so...
id name slat slon elat elon totaldist
1 65 -10.32 136.64 -10.11 136.72 10364.9
2 66 -10.13 136.73 -10.01 136.84 7857.28
any help would be greatly appreciated, thanks!
Much easier to do it in SQL. Try comp.databases.mysql.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 25 '08 #2
Jerry Stuckle <js*******@attglobal.netwrote:
>inexion wrote:
>Hello,

I'll try to explain what I'm trying to accomplish here, hopefully not
too bad a task. Basically I have an existing table with data that will
be operated on to form new records etc...

for example:
id name slat slon elat elon dist
1 65 -10.32 136.64 -10.18 136.69 6556.52
2 65 -10.18 136.69 -10.16 136.72 1701.68
3 65 -10.16 136.72 -10.11 136.72 2106.7
4 66 -10.13 136.73 -10.05 136.79 4536.58
5 66 -10.05 136.79 -10.06 136.82 1082.5
6 66 -10.06 136.82 -10.01 136.84 2238.2

So what I need to do here is basically sum all of the values in the
dist column that correspond to any value with the same name value (eg,
only 65's), plus take both the slat & slon values from the first
distinct record (eg, the 1st 65 slat & slon combined with the last 65
elat & elon values....where 1st would be id#1, last would be id#3
etc.) and combine them with the last elat & elon values...

So from above, my resulting query would give me 2 results looking like
so...
id name slat slon elat elon totaldist
1 65 -10.32 136.64 -10.11 136.72 10364.9
2 66 -10.13 136.73 -10.01 136.84 7857.28
any help would be greatly appreciated, thanks!

Much easier to do it in SQL. Try comp.databases.mysql.
Hmm, I'm not sure I would say it is "much easier". It's certainly possible
to write a union query for this, but it's probably easier to maintain by
doing a simple query in SQL and the math in PHP.

This works in Postgres.

SELECT name,SUM(slat) AS slat,SUM(slon) AS slon,
SUM(elat) AS elat,SUM(elon) AS elon,SUM(dist) AS dist FROM
(
SELECT name,slat,slon,0 AS elat,0 AS elon,0 AS dist
FROM geo
WHERE id IN
(SELECT MIN(id) AS id FROM geo GROUP BY name)
UNION
SELECT name,0,0,elat,elon,0
FROM geo
WHERE id IN
(SELECT MAX(id) AS id FROM geo GROUP BY name)
UNION
SELECT name,0,0,0,0,SUM(dist)
FROM geo
GROUP BY name
) AS foo
GROUP BY name;
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 27 '08 #3
Tim Roberts wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
>inexion wrote:
>>Hello,

I'll try to explain what I'm trying to accomplish here, hopefully not
too bad a task. Basically I have an existing table with data that will
be operated on to form new records etc...

for example:
id name slat slon elat elon dist
1 65 -10.32 136.64 -10.18 136.69 6556.52
2 65 -10.18 136.69 -10.16 136.72 1701.68
3 65 -10.16 136.72 -10.11 136.72 2106.7
4 66 -10.13 136.73 -10.05 136.79 4536.58
5 66 -10.05 136.79 -10.06 136.82 1082.5
6 66 -10.06 136.82 -10.01 136.84 2238.2

So what I need to do here is basically sum all of the values in the
dist column that correspond to any value with the same name value (eg,
only 65's), plus take both the slat & slon values from the first
distinct record (eg, the 1st 65 slat & slon combined with the last 65
elat & elon values....where 1st would be id#1, last would be id#3
etc.) and combine them with the last elat & elon values...

So from above, my resulting query would give me 2 results looking like
so...
id name slat slon elat elon totaldist
1 65 -10.32 136.64 -10.11 136.72 10364.9
2 66 -10.13 136.73 -10.01 136.84 7857.28
any help would be greatly appreciated, thanks!
Much easier to do it in SQL. Try comp.databases.mysql.

Hmm, I'm not sure I would say it is "much easier". It's certainly possible
to write a union query for this, but it's probably easier to maintain by
doing a simple query in SQL and the math in PHP.

This works in Postgres.

SELECT name,SUM(slat) AS slat,SUM(slon) AS slon,
SUM(elat) AS elat,SUM(elon) AS elon,SUM(dist) AS dist FROM
(
SELECT name,slat,slon,0 AS elat,0 AS elon,0 AS dist
FROM geo
WHERE id IN
(SELECT MIN(id) AS id FROM geo GROUP BY name)
UNION
SELECT name,0,0,elat,elon,0
FROM geo
WHERE id IN
(SELECT MAX(id) AS id FROM geo GROUP BY name)
UNION
SELECT name,0,0,0,0,SUM(dist)
FROM geo
GROUP BY name
) AS foo
GROUP BY name;

Sorry to hear you need a UNION to do it in PostGres. I thought PostGres
was better than that. MySQL can do it without such nonsense.

But this is a PHP newsgroup, not a SQL one. If he's using MySQL, others
(and I) will be glad to help him on comp.databases.mysq.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 27 '08 #4
Jerry Stuckle <js*******@attglobal.netwrote:
>
Sorry to hear you need a UNION to do it in PostGres. I thought PostGres
was better than that. MySQL can do it without such nonsense.
I'm not convinced that it can be done in standard SQL without a UNION, or
an even worse set of nested SEELCTs.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 29 '08 #5
Tim Roberts wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
>Sorry to hear you need a UNION to do it in PostGres. I thought PostGres
was better than that. MySQL can do it without such nonsense.

I'm not convinced that it can be done in standard SQL without a UNION, or
an even worse set of nested SEELCTs.
Which is still off topic in this newsgroup.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 29 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dafella | last post by:
The following is code from Xeoport. It is suppose to access my pop3 account and load email into Mysql database. It's not inserting and there is no log or anything to tell me why. Here is the...
2
by: pancho | last post by:
Greetings, I need help configuring/building PHP3 with MySQL as a DSO on a Solaris 8 box - this module is needed to host some existing sites I will be migrating Note. I built PHP4 from source and...
0
by: Richard Gabriel | last post by:
Hi everyone, Since we upgraded to MySQL 4.0.13 from 3.23, we have been getting table corruption often. It happens about twice per week (with about 500 queries per second average). I have even...
0
by: Mark Adams | last post by:
I really need some help with this. MySQL will not start on boot despite everything I've done to make sure that it is set to do so. When I start it as root from a terminal with...
0
by: Mike Chirico | last post by:
Hopefully this will help someone... Helpful Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Last Updated: Fri Apr 16 11:47:34 EDT 2004 The latest version of this...
2
by: pratchaya | last post by:
This is my sample error in my MySQL Log New value of fp=(nil) failed sanity check, terminating stack trace! Please read http://www.mysql.com/doc/en/Using_stack_trace.html and follow instructions...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: Mark | last post by:
the Following bit of code doesn't work. It seems to respond to the second, starting with 'add iif statement for Good Practice', but not to the first, starting 'add iif statement for archived' ...
6
by: sheree | last post by:
I would like to create a query where one of the columns of the queries comes from a combo list box on a form. For example, if my table has the following fields: id name interest1 interest2...
1
by: DarkGiank | last post by:
Hi, im new to csharp and im trying to create a class that can change the application database without no rewriting all connection code... but cause some reason it is not working... it tells me that...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.