Help with a tricky/impossible query...

2005-04-14 Thread Paul B van den Berg
Hi,

In SQL you need to define the data that you want to work with:

create table z ( z int(5) not null primary key);
insert into z values
 
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17),(18),(19),(20),(21),(22);

If you need more values you could use a simple perl looping construct:

for ($i=0; $i = $max; $i++) {
 $dbh-do( q{  insert into z set z = $i });
}

Once you have the table filled, it's easy to explode the x/y ranges by seq:
 
select seq, z
from wibble, z
where z between x and y

Then the rows with seq=1 are:
| seq | z  |
+-++
|   1 |  5 |
|   1 |  6 |
|   1 |  7 |
|   1 |  8 |
|   1 |  9 |
|   1 | 10 |
The rest is as you wanted.

Regards, Paul 

On 14/4/05 1:11 am, Andrew Braithwaite [EMAIL PROTECTED] wrote:

 Here's what I need to do...
 
 create table wibble(
 seq int(3) auto_increment primary key,
 x int(5),
 y int(5)
 );
 
 insert into wibble set x=5, y=10;
 insert into wibble set x=1, y=3;
 insert into wibble set x=17, y=22;
 
 mysql select * from wibble;
 +-+--+--+
 | seq | x| y|
 +-+--+--+
 |   1 |5 |   10 |
 |   2 |1 |3 |
 |   3 |   17 |   22 |
 +-+--+--+
 3 rows in set (0.09 sec)
 
 So I want to run a query to explode the x/y ranges by seq.
 
 The required output is:
 
 mysql select some clever things from wibble where some clever stuff happens
 here;
 +-+--+
 | seq | z|
 +-+--+
 |   1 |1 |
 |   1 |2 |
 |   1 |3 |
 |   1 |4 |
 |   1 |5 |
 |   2 |1 |
 |   2 |2 |
 |   2 |3 |
 |   3 |   17 |
 |   3 |   18 |
 |   3 |   19 |
 |   3 |   20 |
 |   3 |   21 |
 |   3 |   22 |
 +-+--+
 14 rows in set (0.17 sec)
 
 Can anyone help me to achieve this result?
 

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



RE: Help with a tricky/impossible query...

2005-04-14 Thread Andrew Braithwaite
Thanks for the idea,

Unfortunately I can't do that as the ranges involved are unknown and
will be from 1 to several billion at lease.  I can't have another table
that needs to be augmented each time my ranges change.

Any other ideas?

Cheers,

Andrew

 

-Original Message-
From: Paul B van den Berg [mailto:[EMAIL PROTECTED] 
Sent: Thu 14 April 2005 10:47
To: MySQL
Cc: Andrew Braithwaite
Subject: Help with a tricky/impossible query...

Hi,

In SQL you need to define the data that you want to work with:

create table z ( z int(5) not null primary key); insert into z values
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(
17),(18),(19),(20),(21),(22);

If you need more values you could use a simple perl looping construct:

for ($i=0; $i = $max; $i++) {
 $dbh-do( q{  insert into z set z = $i });
}

Once you have the table filled, it's easy to explode the x/y ranges by
seq:
 
select seq, z
from wibble, z
where z between x and y

Then the rows with seq=1 are:
| seq | z  |
+-++
|   1 |  5 |
|   1 |  6 |
|   1 |  7 |
|   1 |  8 |
|   1 |  9 |
|   1 | 10 |
The rest is as you wanted.

Regards, Paul 

On 14/4/05 1:11 am, Andrew Braithwaite [EMAIL PROTECTED] wrote:

 Here's what I need to do...
 
 create table wibble(
 seq int(3) auto_increment primary key, x int(5), y int(5) );
 
 insert into wibble set x=5, y=10;
 insert into wibble set x=1, y=3;
 insert into wibble set x=17, y=22;
 
 mysql select * from wibble;
 +-+--+--+
 | seq | x| y|
 +-+--+--+
 |   1 |5 |   10 |
 |   2 |1 |3 |
 |   3 |   17 |   22 |
 +-+--+--+
 3 rows in set (0.09 sec)
 
 So I want to run a query to explode the x/y ranges by seq.
 
 The required output is:
 
 mysql select some clever things from wibble where some clever stuff 
 mysql happens
 here;
 +-+--+
 | seq | z|
 +-+--+
 |   1 |1 |
 |   1 |2 |
 |   1 |3 |
 |   1 |4 |
 |   1 |5 |
 |   2 |1 |
 |   2 |2 |
 |   2 |3 |
 |   3 |   17 |
 |   3 |   18 |
 |   3 |   19 |
 |   3 |   20 |
 |   3 |   21 |
 |   3 |   22 |
 +-+--+
 14 rows in set (0.17 sec)
 
 Can anyone help me to achieve this result?
 



--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: Help with a tricky/impossible query...

2005-04-14 Thread Paul B van den Berg
On Thu, 14 Apr 2005 11:57:50 +0100
Andrew Braithwaite [EMAIL PROTECTED] wrote:

 Thanks for the idea,
 
 Unfortunately I can't do that as the ranges involved are unknown and
 will be from 1 to several billion at lease.  I can't have another table
 that needs to be augmented each time my ranges change.
 
 Any other ideas?
 
 Cheers,
 
 Andrew

I Think you really need the second table, but you can fill it as needed:

my $x=1700;
my $y=2200;
$dbh-do( q{ insert into wibble set x=$x, y=$y });
for ($i=$x; $i =$y ; $i++) {
 $dbh-do( q{  insert ignore into z set z = $i });
}

Regards, Paul

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



RE: Help with a tricky/impossible query...

2005-04-14 Thread Gordon
One way would be to build a count table with one column starting with
value 1 and incrementing by 1 up to say 500 rows or how many your max y
value is. Then just 
   select seq,val from wibble,count where val between x and y

create table count (val INT unsigned default '0' not null primary key)

-Original Message-
From: Andrew Braithwaite [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, April 13, 2005 7:33 PM
To: MySQL
Subject: Re: Help with a tricky/impossible query...

I should mention that I'm constrained to version 4.0.n so no sub queries for
me!

Andrew


On 14/4/05 1:11 am, Andrew Braithwaite [EMAIL PROTECTED] wrote:

 Hi,
 
 I need some help with a tricky query.  Before anyone asks, I cannot bring
 this functionality back to the application layer (as much as I'd like to).
 
 Here's what I need to do...
 
 create table wibble(
 seq int(3) auto_increment primary key,
 x int(5),
 y int(5)
 );
 
 insert into wibble set x=5, y=10;
 insert into wibble set x=1, y=3;
 insert into wibble set x=17, y=22;
 
 mysql select * from wibble;
 +-+--+--+
 | seq | x| y|
 +-+--+--+
 |   1 |5 |   10 |
 |   2 |1 |3 |
 |   3 |   17 |   22 |
 +-+--+--+
 3 rows in set (0.09 sec)
 
 So I want to run a query to explode the x/y ranges by seq.
 
 The required output is:
 
 mysql select some clever things from wibble where some clever stuff
happens
 here;
 +-+--+
 | seq | z|
 +-+--+
 |   1 |1 |
 |   1 |2 |
 |   1 |3 |
 |   1 |4 |
 |   1 |5 |
 |   2 |1 |
 |   2 |2 |
 |   2 |3 |
 |   3 |   17 |
 |   3 |   18 |
 |   3 |   19 |
 |   3 |   20 |
 |   3 |   21 |
 |   3 |   22 |
 +-+--+
 14 rows in set (0.17 sec)
 
 Can anyone help me to achieve this result?
 
 Thanks,
 
 Andrew
 
 SQL, Query
 
 
 



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Help with a tricky/impossible query...

2005-04-13 Thread Andrew Braithwaite
Hi,

I need some help with a tricky query.  Before anyone asks, I cannot bring
this functionality back to the application layer (as much as I'd like to).

Here's what I need to do...

create table wibble(
seq int(3) auto_increment primary key,
x int(5),
y int(5)
);

insert into wibble set x=5, y=10;
insert into wibble set x=1, y=3;
insert into wibble set x=17, y=22;

mysql select * from wibble;
+-+--+--+
| seq | x| y|
+-+--+--+
|   1 |5 |   10 |
|   2 |1 |3 |
|   3 |   17 |   22 |
+-+--+--+
3 rows in set (0.09 sec)

So I want to run a query to explode the x/y ranges by seq.

The required output is:

mysql select some clever things from wibble where some clever stuff happens
here;
+-+--+
| seq | z|
+-+--+
|   1 |1 |
|   1 |2 |
|   1 |3 |
|   1 |4 |
|   1 |5 |
|   2 |1 |
|   2 |2 |
|   2 |3 |
|   3 |   17 |
|   3 |   18 |
|   3 |   19 |
|   3 |   20 |
|   3 |   21 |
|   3 |   22 |
+-+--+
14 rows in set (0.17 sec)

Can anyone help me to achieve this result?

Thanks,

Andrew

SQL, Query




-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: Help with a tricky/impossible query...

2005-04-13 Thread Andrew Braithwaite
I should mention that I'm constrained to version 4.0.n so no sub queries for
me!

Andrew


On 14/4/05 1:11 am, Andrew Braithwaite [EMAIL PROTECTED] wrote:

 Hi,
 
 I need some help with a tricky query.  Before anyone asks, I cannot bring
 this functionality back to the application layer (as much as I'd like to).
 
 Here's what I need to do...
 
 create table wibble(
 seq int(3) auto_increment primary key,
 x int(5),
 y int(5)
 );
 
 insert into wibble set x=5, y=10;
 insert into wibble set x=1, y=3;
 insert into wibble set x=17, y=22;
 
 mysql select * from wibble;
 +-+--+--+
 | seq | x| y|
 +-+--+--+
 |   1 |5 |   10 |
 |   2 |1 |3 |
 |   3 |   17 |   22 |
 +-+--+--+
 3 rows in set (0.09 sec)
 
 So I want to run a query to explode the x/y ranges by seq.
 
 The required output is:
 
 mysql select some clever things from wibble where some clever stuff happens
 here;
 +-+--+
 | seq | z|
 +-+--+
 |   1 |1 |
 |   1 |2 |
 |   1 |3 |
 |   1 |4 |
 |   1 |5 |
 |   2 |1 |
 |   2 |2 |
 |   2 |3 |
 |   3 |   17 |
 |   3 |   18 |
 |   3 |   19 |
 |   3 |   20 |
 |   3 |   21 |
 |   3 |   22 |
 +-+--+
 14 rows in set (0.17 sec)
 
 Can anyone help me to achieve this result?
 
 Thanks,
 
 Andrew
 
 SQL, Query
 
 
 



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



RE: Impossible query??

2003-06-15 Thread Michael Scott
I don't think that solves the problem. There are multiple test chains with
Id's less than 7.

ie
  7-6-4-3-2
  5
  1

and your query looking for history on testId=7

SELECT * FROM tests WHERE testID=7 AND connect0;

could return testId's 5 and 1 as well if they were part of longer chains

What is needed (and does not exist) is a recursive query that can be started
on a given testId, follows the connect-testId chain and terminates when it
reaches a record with connect=0

A single query can not do this. You will be forced to solve this
programatically by executing a query that selects a single record based on
the previous record's connect field until you reach a connect=0.  (Caution,
if ever a connect value points back into the chain you will enter an endless
loop!)

Or

Re-Design your table (and inserting code ) by adding a column called
baseTestId that stores the original testId.  Every time a new test is added
that is an extension of a previous test the previous testId is copied into
the new record and the previous baseTestId is also copied to the new record
baseTestID. (If a test is the first one it uses it's own id as the
baseTestID).

Your sample table would look like this...

assuming three chains exist

  7-6-4-3-2
  5
  1

+-+--++--+
| testId  | connect  | baseTestId | result   |
+-+--++--+
| 1   | 0| 1  | ok   |
| 2   | 0| 2  | nok  |
| 3   | 2| 2  | nok  |
| 4   | 3| 2  | nok  |
| 5   | 0| 5  | ok   |
| 6   | 4| 2  | nok  |
| 7   | 6| 2  | ok   |
| 8   | 0| 8  | ok   |
+-+--+|--+

note how all the records that are in a chain have the same baseTestID!

Then this single query will do what you want

SELECT B.* FROM myTable AS A
LEFT OUTER JOIN myTable AS B ON(A.baseTesID=B.baseTestID)
WHERE (A.testId=7)

It returns
++-++--+
| testId | connect | baseTestId | result   |
++-++--+
|  2 |   0 |  2 | nok  |
|  3 |   2 |  2 | nok  |
|  4 |   3 |  2 | nok  |
|  6 |   4 |  2 | nok  |
|  7 |   6 |  2 | ok   |
++-++--+

The idea here is that you need a single piece of data that can relate all
the records in a chain of tests together. The where clause selects the test
Id in question and the join collects all records whose baseTestID is the
same as the record selected by the where clause.

This query would return all tests in the chain no matter which id you used.
If you queried on testId 4 the same set of data would be returned, basically
all tests in the chain.

Now if you already have tons of data in place you will need to update the
table data so that the baseTestID field reflects these new rules.


Mike Scott, Ind. Contractor

-Original Message-
From: Becoming Digital [mailto:[EMAIL PROTECTED]
Sent: Saturday, June 14, 2003 4:13 PM
To: [EMAIL PROTECTED]
Subject: Re: Impossible query??


Your query seems relatively easy if you don't need the first test (in this
case,
testID 2) explicitly printed.

mysql SELECT * FROM tests WHERE testID=7 AND connect0;
+-+--+--+
| testId  | connect  | result   |
+-+--+--+
| 7   | 6| ok   |
| 6   | 4| nok  |
| 4   | 3| nok  |
| 3   | 2| nok  |
+-+--+--+

The history of testID 2 is implied by your table structure, such that you
know
testID 2 passed because it is the last connect value.

The problem I see with this occurs when you have a repeating data set.  A
query for your next failure (after testID 7) will also return testID 7 and
its
history, as would be the case with the below data.  Repairing this would
require
adding an additional parameter to the WHERE statement, probably using
BETWEEN.
+-+--+--+
| testId  | connect  | result   |
+-+--+--+
| 1   | 0| ok   |
| 2   | 0| nok  |
| 3   | 2| nok  |
| 4   | 3| nok  |
| 5   | 0| ok   |
| 6   | 4| nok  |
| 7   | 6| ok   |
| 8   | 0| ok   |
| 9   | 0| nok  |
| 10  | 9| nok  |
| 11

Impossible query??

2003-06-14 Thread trashMan
Hi, sorry for my bad english.

I've a table where i store the result of a quality test.
If the test is passed then the result=ok and connect=0
If test fail then result=nok and, if it's the first test for the
product, connect=0
When i retest a failed product i've  connect= previous testid of the
product failed
It's possible to do a query for print the history of one id?? 

Exemple 

mysql select * from mytable;

+-+--+--+
| testId  | connect  | result   | 
+-+--+--+
| 1   | 0| ok   |
| 2   | 0| nok  |
| 3   | 2| nok  |
| 4   | 3| nok  |
| 5   | 0| ok   |
| 6   | 4| nok  |
| 7   | 6| ok   |
| 8   | 0| ok  |
+-+--+--+


I want the history of  testId=7
mysql  ??

+-+--+--+
| testId  | connect  | result   | 
+-+--+--+
| 7   | 6| ok   |
| 6   | 4| nok  |
| 4   | 3| nok  |
| 3   | 2| nok  |
| 2   | 0| ok   |
+-+--+--+


Thanks a lot!

Max (trashman)



-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Re: Impossible query??

2003-06-14 Thread Becoming Digital
Your query seems relatively easy if you don't need the first test (in this case,
testID 2) explicitly printed.

mysql SELECT * FROM tests WHERE testID=7 AND connect0;
+-+--+--+
| testId  | connect  | result   |
+-+--+--+
| 7   | 6| ok   |
| 6   | 4| nok  |
| 4   | 3| nok  |
| 3   | 2| nok  |
+-+--+--+

The history of testID 2 is implied by your table structure, such that you know
testID 2 passed because it is the last connect value.

The problem I see with this occurs when you have a repeating data set.  A
query for your next failure (after testID 7) will also return testID 7 and its
history, as would be the case with the below data.  Repairing this would require
adding an additional parameter to the WHERE statement, probably using BETWEEN.
+-+--+--+
| testId  | connect  | result   |
+-+--+--+
| 1   | 0| ok   |
| 2   | 0| nok  |
| 3   | 2| nok  |
| 4   | 3| nok  |
| 5   | 0| ok   |
| 6   | 4| nok  |
| 7   | 6| ok   |
| 8   | 0| ok   |
| 9   | 0| nok  |
| 10  | 9| nok  |
| 11  | 10   | ok   |
+-+--+--+


Edward Dudlik
Becoming Digital
www.becomingdigital.com


- Original Message -
From: trashMan [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Saturday, 14 June, 2003 11:28
Subject: Impossible query??


Hi, sorry for my bad english.

I've a table where i store the result of a quality test.
If the test is passed then the result=ok and connect=0
If test fail then result=nok and, if it's the first test for the
product, connect=0
When i retest a failed product i've  connect= previous testid of the
product failed
It's possible to do a query for print the history of one id??

Exemple

mysql select * from mytable;

+-+--+--+
| testId  | connect  | result   |
+-+--+--+
| 1   | 0| ok   |
| 2   | 0| nok  |
| 3   | 2| nok  |
| 4   | 3| nok  |
| 5   | 0| ok   |
| 6   | 4| nok  |
| 7   | 6| ok   |
| 8   | 0| ok  |
+-+--+--+


I want the history of  testId=7
mysql  ??

+-+--+--+
| testId  | connect  | result   |
+-+--+--+
| 7   | 6| ok   |
| 6   | 4| nok  |
| 4   | 3| nok  |
| 3   | 2| nok  |
| 2   | 0| ok   |
+-+--+--+


Thanks a lot!

Max (trashman)



--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]





-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]



Impossible Query?

2002-07-11 Thread Daren Cotter

I have three tables, affiliates, clients, and sales.

The affiliates table stores all of the information about affiliates,
clients about clients, sales about sales. In the clients table, there is
a field for affiliate_id (affiliates refer clients), and in the sales
table there is a field for client_id.

I need a query that will show me a list of all affiliates and the number
of sales each affiliate has generated. I know this will involve a left
join, but I can't figure it out, since it involves that third table.

Which actually brings up another question: would be be better to store
the affiliate_id in the sales table? The reason I do it this way, is
because if an affiliate refers a client, and the client is involved in
numerous sales, the affiliate should be credited each time.

TIA!


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Impossible Query?

2002-07-11 Thread Daren Cotter

I have three tables, affiliates, clients, and sales.

The affiliates table stores all of the information about affiliates,
clients about clients, sales about sales. In the clients table, there is
a field for affiliate_id (affiliates refer clients), and in the sales
table there is a field for client_id.

I need a query that will show me a list of all affiliates and the number
of sales each affiliate has generated. I know this will involve a left
join, but I can't figure it out, since it involves that third table.

Which actually brings up another question: would be be better to store
the affiliate_id in the sales table? The reason I do it this way, is
because if an affiliate refers a client, and the client is involved in
numerous sales, the affiliate should be credited each time.

TIA!


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: Impossible Query?

2002-07-11 Thread Witness

I don't think it's impossible. It might be nicer if you could use a
sub-select. But here's join.

SELECT  DISTINCT affiliates.id, DISTINCT
sales.client_id,SUM(sales.client_id)
FROM affiliates,clients,sales
WHERE affiliates.id = clients.affiliate_id AND sales.client_id =
clients.id;

Would this do it? That should tell you how many sales each affiliate has
made to each client. Sum that up, and you will get what you probably
want in the end - a total referral, but at the least it should show you
how the join would go.

Benjamen R. Meyer

 -Original Message-
 From: Daren Cotter [mailto:[EMAIL PROTECTED]]
 Sent: Thursday, July 11, 2002 5:03 PM
 To: [EMAIL PROTECTED]
 Subject: Impossible Query?


 I have three tables, affiliates, clients, and sales.

 The affiliates table stores all of the information about affiliates,
 clients about clients, sales about sales. In the clients
 table, there is
 a field for affiliate_id (affiliates refer clients), and in the sales
 table there is a field for client_id.

 I need a query that will show me a list of all affiliates and
 the number
 of sales each affiliate has generated. I know this will involve a left
 join, but I can't figure it out, since it involves that third table.

 Which actually brings up another question: would be be better to store
 the affiliate_id in the sales table? The reason I do it this way, is
 because if an affiliate refers a client, and the client is involved in
 numerous sales, the affiliate should be credited each time.

 TIA!


 -
 Before posting, please check:
http://www.mysql.com/manual.php   (the manual)
http://lists.mysql.com/   (the list archive)

 To request this thread, e-mail [EMAIL PROTECTED]
 To unsubscribe, e-mail
 [EMAIL PROTECTED]
 Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php



-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




RE: Impossible Query?

2002-07-11 Thread Keith C. Ivey

On 11 Jul 2002, at 17:41, Witness wrote:

 SELECT  DISTINCT affiliates.id, DISTINCT
 sales.client_id,SUM(sales.client_id)
  FROM affiliates,clients,sales
  WHERE affiliates.id = clients.affiliate_id AND sales.client_id =
 clients.id;

I don't think that's what Daren wanted.  For one thing, I doubt it's 
meaningful to sum client IDs.  Maybe something more like this:

   SELECT a.id as affiliate_id, COUNT(s.id) as sales_count
   FROM affiliates a LEFT JOIN clients c ON a.id = c.affiliate_id
LEFT JOIN sales s ON c.id = s.client_id
   GROUP BY a.id;

[Filter fodder: SQL]

-- 
Keith C. Ivey [EMAIL PROTECTED]
Tobacco Documents Online
http://tobaccodocuments.org

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




strange or impossible query?

2002-06-07 Thread Fabrizio Tivano



Hello dear all, 


i have a table like this:

field_machine, field_date, field_time, field_function.

field_function can be =TT or +BT

well i need to perform a select query, witch display on one line

field_machine, 
(field_date, field_time where field_function =TT) AS start, 
(field_date, fiel_time where field_function =BT) AS stop from table;




is possible to do that?
...and if yes, how?

thanks in advance and Regards,

fabrizio

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: strange or impossible query?

2002-06-07 Thread Rob

I'm afraid I'm not quite clear on what you're trying to do; do you want one
result per row, with one NULL column and one non-NULL column, or do you want
one row per distinct 'machine', with both a start and a stop date?

In the first case, this can be implemented with a simple IF():
IF(field_function='TT',field_date,NULL)
which will make the field NULL if field_function is not set to the correct
value.
If you want two separate fields (both date and time), then use two IFs with
the same test.

If you want one row for each machine with both start and end dates, there
are several ways to do it. Which you choose depends largely on your
integrity contraints; are you sure that you've got exactly one TT and one BT
for each machine, or do you have 'pairs', or do you want every comination?
The simplest hack is just to use the above, but wrap every query in a max()
function, and group by the field_machine:

select
max(field_machine) as fieldMachine,
max(if(field_function='TT',field_date,NULL)) as startDate,
max(if(field_function='TT',field_time,NULL)) as startTime,
max(if(field_function='BT',field_date,NULL)) as stopDate,
max(if(field_function='BT',field_time,NULL)) as stopTime
from theTable
group by fieldMachine

I consider this ugly, but it's simple to do.
If you're really trying to relate each machine to its start and end times,
then I would write the query that way: get a table of distinct machines (if
it exists already, great; if not,
create temporary table theMachines select distinct field_machine from
theTable;
) and then join that table against your start times and against your end
times:

select theMachines.field_machine,
theStart.field_date, theStart.field_time,
theEnd.field_date, theStart.field_time
from theMachines, theTable as theStart, theTable as theEnd
where theMachines.field_machine = theStart.field_machine
and theStart.field_function = 'TT'
and theMachines.field_machine = theEnd.field_machine
and theEnd.field_function = 'BT'

(Just banging this out quickly, but you get the idea.)

-rob

On 7/6/02 at 12:59 pm, Fabrizio Tivano [EMAIL PROTECTED] wrote:

 
 
 Hello dear all, 
 
 
 i have a table like this:
 
 field_machine, field_date, field_time, field_function.
 
 field_function can be =TT or +BT
 
 well i need to perform a select query, witch display on one line
 
 field_machine, 
 (field_date, field_time where field_function =TT) AS start, 
 (field_date, fiel_time where field_function =BT) AS stop from table;
 
 
 
 
 is possible to do that?
 and if yes, how?
 
 thanks in advance and Regards,
 
 fabrizio
 
 -
 Before posting, please check:
http://www.mysql.com/manual.php   (the manual)
http://lists.mysql.com/   (the list archive)
 
 To request this thread, e-mail [EMAIL PROTECTED]
 To unsubscribe, e-mail [EMAIL PROTECTED]
 Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
 
 


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php