Postgres-sql-php
Postgres-sql-php
От:
"Zied Kharrat" <khzied@gmail.com>
Дата:
Hi Everybody..
Let's present my problem:
I have a table named t1 and i will insert differents values like this :
insert into t1 (num,father,child,age) values ('1','joe','bruce','14',);
insert into t1 (num,father,child,age) values ('1','joe','lei','10',);
insert into t1 (num,father,child,age) values ('1','joe','mike','5',);
when i use select * from t1 i obtain:
num father child age
1 joe bruce 14
1 joe lei 10
1 joe mike 5
i want to have
num father child age
1 joe bruce 14
lei 10
mike 5
what can i do as select request to obtain this capture?
Thanks :)
Let's present my problem:
I have a table named t1 and i will insert differents values like this :
insert into t1 (num,father,child,age) values ('1','joe','bruce','14',);
insert into t1 (num,father,child,age) values ('1','joe','lei','10',);
insert into t1 (num,father,child,age) values ('1','joe','mike','5',);
when i use select * from t1 i obtain:
num father child age
1 joe bruce 14
1 joe lei 10
1 joe mike 5
i want to have
num father child age
1 joe bruce 14
lei 10
mike 5
what can i do as select request to obtain this capture?
Thanks :)
Re: Postgres-sql-php
От:
"Oliveiros Cristina" <oliveiros.cristina@marktest.pt>
Дата:
Howdy, Zied.
The query below outputs the results as you want, but
I suspect you have a more general situation you want to solve.
If you have more than one father, say "manuel", you would want something like this ?
num father child age
1 joe bruce 14
lei 10
mike 5
1 joe bruce 14
lei 10
mike 5
2 manuel child1 35
child2 33
child3 30
Confirm, please .
Also, do you want the output ordered by age? always?
If so , tell me and we can tweak a little the query to best-fit your needs
Best,
Oliveiros
SELECT a.num,a.father,b.child,b.age
FROM
(
SELECT num,father, MAX(age)as maximo
FROM
(
SELECT num,father, MAX(age)as maximo
FROM t1
GROUP BY num,father) a
RIGHT JOIN t1 b
ON b.age = a.maximo
GROUP BY num,father) a
RIGHT JOIN t1 b
ON b.age = a.maximo
----- Original Message -----From: Zied KharratSent: Thursday, October 23, 2008 9:14 AMSubject: [SQL] Postgres-sql-phpHi Everybody..
Let's present my problem:
I have a table named t1 and i will insert differents values like this :
insert into t1 (num,father,child,age) values ('1','joe','bruce','14',);
insert into t1 (num,father,child,age) values ('1','joe','lei','10',);
insert into t1 (num,father,child,age) values ('1','joe','mike','5',);
when i use select * from t1 i obtain:
num father child age
1 joe bruce 14
1 joe lei 10
1 joe mike 5
i want to have
num father child age
1 joe bruce 14
lei 10
mike 5
what can i do as select request to obtain this capture?
Thanks :)
Re: Postgres-sql-php
От:
"Zied Kharrat" <khzied@gmail.com>
Дата:
hi,
i don't want any sort.. just like this example
this is what i want really..
What can be then the sql request without sort with this update.. Thanks :)
i don't want any sort.. just like this example
num father child age col5
1 joe bruce 14 8
lei 10
mike 5
1 joe bruce 14 8
lei 10
mike 5
2 manuel child1 35 16
child2 33
child3 30
this is what i want really..
What can be then the sql request without sort with this update.. Thanks :)
Re: Postgres-sql-php
От:
"Oliveiros Cristina" <oliveiros.cristina@gmail.com>
Дата:
Just add two conditions on the previous query
A particularity of this approach is that the non-null record will always appear with the first child in alphabetical order.
But, according to what you explain, I guess it is OK, and if it doesn't it is easily changed. :-)
Also I've changed the first condition on the right outer join coz it would give trouble if two childs happened to be of the same age.
I guess it will never happen two childs with the same name :p
Best,
Oliveiros
SELECT a.num,a.father,b.child,b.age
FROM
(
SELECT num,father, MIN(child)as primeira
FROM t1
GROUP BY num,father) a
RIGHT JOIN t1 b
ON b.child = a.primeira
AND b.father = a.father
AND a.num = b.num
-- FROM
(
SELECT num,father, MIN(child)as primeira
FROM t1
GROUP BY num,father) a
RIGHT JOIN t1 b
ON b.child = a.primeira
AND b.father = a.father
AND a.num = b.num
even the biggest failure, even the worst mistake, beats the hell out of never trying...
- Meredith Grey
2008/10/23 Zied Kharrat <khzied@gmail.com>
hi,
i don't want any sort.. just like this examplenum father child age col5
1 joe bruce 14 8
lei 10
mike 52 manuel child1 35 16child2 33child3 30
this is what i want really..
What can be then the sql request without sort with this update.. Thanks :)
Re: Postgres-sql-php
От:
"Oliveiros Cristina" <oliveiros.cristina@gmail.com>
Дата:
I guess you can change a little the query to your needs.
The problem is pretty much the same...
I've used c3 column in equality, but if this column
has repeated values, just choose any column or combination of columns which is unique.
Best,
Oliveiros
SELECT a.c1,a.c2,b.c3,b.c4,a.c5,b.c6
FROM
(
SELECT c1,c2,c5, MIN(c3)as primeiraThe problem is pretty much the same...
I've used c3 column in equality, but if this column
has repeated values, just choose any column or combination of columns which is unique.
Best,
Oliveiros
SELECT a.c1,a.c2,b.c3,b.c4,a.c5,b.c6
FROM
(
FROM t1
GROUP BY c1,c2,c5
) a
RIGHT JOIN t1 b
ON b.c3 = a.primeiraGROUP BY c1,c2,c5
) a
RIGHT JOIN t1 b
AND b.c1 = a.c1
AND a.c2 = b.c2
AND a.c5 = b.c5
2008/10/23 Zied Kharrat <khzied@gmail.com>
Really, i have this schema:
c1 c2 c3 c4 c5 c6
v1 v2 v3 v4 v5 v6
v1 v2 v7 v8 v5 v9
v1 v2 v10 v11 v5 v12
how can i do my sql request to obtain this?
c1 c2 c3 c4 c5 c6
v1 v2 v3 v4 v5 v6
v7 v8 v9
v10 v11 v12
Thank u very much :)
--
even the biggest failure, even the worst mistake, beats the hell out of never trying...
- Meredith Grey