pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Help with a query
16+ messages / 14 participants
[nested] [flat]

* Help with a query
@ 2003-04-25 07:41 jose antonio leo <jaleo8@storelandia.com>
  2003-04-25 08:38 ` Re: Help with a query Richard Huxton <dev@archonet.com>
  0 siblings, 1 reply; 16+ messages in thread

From: jose antonio leo @ 2003-04-25 07:41 UTC (permalink / raw)
  To: pgsql-general


Hi guys!

I need make a join query.
I have two tables with the next fields:

aec_cua_man:
ac1-ac2-ac3
1-1-1
1-1-2
1-1-4
1-2-2
....

aecoc:
ac1-ac2-ac3-description
1-0-0-des1
1-1-0-des2
1-1-1-des3
1-1-2-des4
1-1-3-des5
1-1-4-des6
1-1-5-des7
1-2-0-des8
1-2-1-des9
1-2-2-des10
....

I need the record of the first table with the fields ac1, ac2 and ac3 equals
than second table but in addition the fiels of the second table with a nul
in the fiels ac2 and ac3.

the table result will be:
1-0-0-des1
1-1-0-des2
1-1-1-des3
1-1-2-des4
1-1-4-des6
1-2-0-des8
1-2-2-des10

Could you help me?


Jose Antonio




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Help with a query
  2003-04-25 07:41 Help with a query jose antonio leo <jaleo8@storelandia.com>
@ 2003-04-25 08:38 ` Richard Huxton <dev@archonet.com>
  0 siblings, 0 replies; 16+ messages in thread

From: Richard Huxton @ 2003-04-25 08:38 UTC (permalink / raw)
  To: jose antonio leo <jaleo8@storelandia.com>; pgsql-general

On Friday 25 Apr 2003 8:41 am, jose antonio leo wrote:
> aec_cua_man:
> ac1-ac2-ac3
> 1-1-1
> 1-1-2
> 1-1-4
> 1-2-2
> ....
>
> aecoc:
> ac1-ac2-ac3-description
> 1-0-0-des1
> 1-1-0-des2
> 1-1-1-des3
> 1-1-2-des4
> 1-1-3-des5
> 1-1-4-des6
> 1-1-5-des7
> 1-2-0-des8
> 1-2-1-des9
> 1-2-2-des10
> ....
>
> I need the record of the first table with the fields ac1, ac2 and ac3
> equals than second table but in addition the fiels of the second table with
> a nul in the fiels ac2 and ac3.
>
> the table result will be:
> 1-0-0-des1
> 1-1-0-des2
> 1-1-1-des3
> 1-1-2-des4
> 1-1-4-des6
> 1-2-0-des8
> 1-2-2-des10
>
> Could you help me?

Simplest method might be a union - do the join on the 3 fields and union it 
with a simple select on aecoc where ac3=0 or ac2=0. Syntax is something like:

SELECT ac1,ac2,ac3,description FROM .... (join here)
UNION
SELECT ac1,ac2,ac3,description FROM aecoc... (grab zero descriptions here)

Note that the order of the result columns should be the same in both cases. If 
you know there will be no zeros in aec_cua_man then you might want to use 
UNION ALL.

See the User's guide Ch4 or the SQL SELECT reference for some details.

-- 
  Richard Huxton




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* HELP with a Query
@ 2004-02-21 08:20 Alex <alex@meerkatsoft.com>
  2004-02-21 08:55 ` Re: HELP with a Query Martijn van Oosterhout <kleptog@svana.org>
  0 siblings, 1 reply; 16+ messages in thread

From: Alex @ 2004-02-21 08:20 UTC (permalink / raw)
  To: pgsql-general

Hi,
I need some help with a query.
I use a reference table to lookup some codes. Everything works well if the reference table contains a reference but the query fails if no reference fails (Z.ref_code=123456). 
What I need to is either return 0 or null into Z.prim_exch if no reference can be found. Whats the best way to do that ? I tried a sub query but also did not succeed.


SELECT A.sec_code,A.exch_code,A.price_date,Z.prim_exch,
   CASE
     WHEN A.exch_code = Z.prim_exch THEN 1
     ELSE 0
   END AS is_prim
FROM price_hist AS A, references AS Z
WHERE   A.price_date <= '20040219' AND A.sec_code=123456 AND Z.ref_code=123456 LIMIT 10;

Thanks for any help.
Alex






^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: HELP with a Query
  2004-02-21 08:20 HELP with a Query Alex <alex@meerkatsoft.com>
@ 2004-02-21 08:55 ` Martijn van Oosterhout <kleptog@svana.org>
  0 siblings, 0 replies; 16+ messages in thread

From: Martijn van Oosterhout @ 2004-02-21 08:55 UTC (permalink / raw)
  To: Alex <alex@meerkatsoft.com>; +Cc: pgsql-general

On Sat, Feb 21, 2004 at 05:20:51PM +0900, Alex wrote:
> Hi,
> I need some help with a query.
> I use a reference table to lookup some codes. Everything works well if the 
> reference table contains a reference but the query fails if no reference 
> fails (Z.ref_code=123456). What I need to is either return 0 or null into 
> Z.prim_exch if no reference can be found. Whats the best way to do that ? I 
> tried a sub query but also did not succeed.

Lookup an SQL site about OUTER JOINS. Should do what you want.

-- 
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> If the Catholic church can survive the printing press, science fiction
> will certainly weather the advent of bookwarez.
>    http://craphound.com/ebooksneitherenorbooks.txt - Cory Doctorow

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Help with a query
@ 2004-02-26 21:47 Alexander Cohen <alex@toomuchspace.com>
  2004-02-26 22:08 ` Re: Help with a query Erwin Van de Velde <erwin.vandevelde@ua.ac.be>
  2004-02-26 22:40 ` Re: Help with a query Klint Gore <kg@kgb.une.edu.au>
  0 siblings, 2 replies; 16+ messages in thread

From: Alexander Cohen @ 2004-02-26 21:47 UTC (permalink / raw)
  To: pgsql-general

Hi,

Im trying to get a list of all groups that a certain user is a member 
of. Can anyone help me with the sql to get that?

thanks!


-- 
Alexander Cohen
http://www.toomuchspace.com
(819) 348-9237
(819) 432-3443




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Help with a query
  2004-02-26 21:47 Help with a query Alexander Cohen <alex@toomuchspace.com>
@ 2004-02-26 22:08 ` Erwin Van de Velde <erwin.vandevelde@ua.ac.be>
  1 sibling, 0 replies; 16+ messages in thread

From: Erwin Van de Velde @ 2004-02-26 22:08 UTC (permalink / raw)
  To: Alexander Cohen <alex@toomuchspace.com>; pgsql-general

Hi,

Without further information, this is rather difficult, but assuming that you 
have a table with userdata with a primary key UID and a unique UserName, and 
that you have a table of usergroups with a primary key GID and unique 
GroupName, the best way is to make a third table Users_Usergroups e.g.
where you put the couples (UIDX, GIDX) for a user with UID=UIDX and who is in 
group GIDX.
The query would then be:
SELECT GroupName FROM Users_Usergroups JOIN Users ON Users.UID = 
Users_Usergroups.UID JOIN Usergroups ON Usergroups.GID = Users_Usergroups.GID
WHERE UserName Like 'The User';

Greetings,
Erwin Van de Velde
Student of University of Antwerp
Belgium


On Thursday 26 February 2004 22:47, Alexander Cohen wrote:
> Hi,
>
> Im trying to get a list of all groups that a certain user is a member
> of. Can anyone help me with the sql to get that?
>
> thanks!




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Help with a query
  2004-02-26 21:47 Help with a query Alexander Cohen <alex@toomuchspace.com>
@ 2004-02-26 22:40 ` Klint Gore <kg@kgb.une.edu.au>
  1 sibling, 0 replies; 16+ messages in thread

From: Klint Gore @ 2004-02-26 22:40 UTC (permalink / raw)
  To: Alexander Cohen <alex@toomuchspace.com>; +Cc: pgsql-general

On Thu, 26 Feb 2004 16:47:10 -0500, Alexander Cohen <alex@toomuchspace.com> wrote:
> Im trying to get a list of all groups that a certain user is a member 
> of. Can anyone help me with the sql to get that?

select groname
>from pg_group
where (select usesyside from pg_shadow where usename = 'postgres') =
any(grolist);

klint.

+---------------------------------------+-----------------+
: Klint Gore                            : "Non rhyming    :
: EMail   : kg@kgb.une.edu.au           :  slang - the    :
: Snail   : A.B.R.I.                    :  possibilities  :
: Mail      University of New England   :  are useless"   :
:           Armidale NSW 2351 Australia :     L.J.J.      :
: Fax     : +61 2 6772 5376             :                 :
+---------------------------------------+-----------------+



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Help with a query
@ 2004-02-27 10:32 Bas Scheffers <bas@scheffers.net>
  0 siblings, 0 replies; 16+ messages in thread

From: Bas Scheffers @ 2004-02-27 10:32 UTC (permalink / raw)
  To: pgsql-general; +Cc: alex@toomuchspace.com

If the user/groups you are talking about are postgres users and groups,
this is it:

select * from pg_catalog.pg_group where (select usesysid from
pg_catalog.pg_user where usename = 'user') = any(grolist)

The place to find this kind of thing is the Postgres Internals section
(system catalogs) that desribes the system tables. "any" is an array
function which is needed as the users that belong to a group are an array
of INT user ids.

Hope that helps,
Bas.



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: Help with a query
@ 2004-02-27 12:29 Bas Scheffers <bas@scheffers.net>
  0 siblings, 0 replies; 16+ messages in thread

From: Bas Scheffers @ 2004-02-27 12:29 UTC (permalink / raw)
  To: pgsql-general

Klint,

> select groname from pg_group
> where (select usesyside from pg_shadow where usename = 'postgres') =
> any(grolist);
Unless you are lgged in as superuser (and applications other than pgAdmin
et al shouldn't be) you will get access denied on pg_shadow. (because it
contains passwords) Selecting on pg_user gives you the same result and can
be done by any user.

Bas.



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* help with a query
@ 2006-11-03 20:29 Pedro Doria Meunier <pdoria@netmadeira.com>
  2006-11-05 10:50 ` Re: help with a query Andreas Kretschmer <akretschmer@spamfence.net>
  2006-11-06 00:11 ` Re: help with a query redhog <redhog@redhog.org>
  0 siblings, 2 replies; 16+ messages in thread

From: Pedro Doria Meunier @ 2006-11-03 20:29 UTC (permalink / raw)
  To: pgsql-general

Hi all!

 

This is most certainly a lame question but perhaps someone is gracious
enough to lend me a hand. ;-)

 

I have the following setup in a table:

 

The first record which is to be found (ok easy enough :D) with a timestamp
meets a certain condition (ignition off)

The following record is for the event of ignition on again with a timestamp.

 

So the question here is: how can I compute the time difference between these
two records in a single query?

Better yet: finding all the records that meet the first condition (ignition
off) and the immediately following records as to compute the time
difference. ;-)

 

Thanks in advance!

 

 

Pedro Doria Meunier

(351) 91 302 49 72 - (351) 96 247 99 12

pdoria@netmadeira.com

MSN - pdoriam@hotmail.com

ICQ - 308-182-126

Skype: pdoriam

 

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: help with a query
  2006-11-03 20:29 help with a query Pedro Doria Meunier <pdoria@netmadeira.com>
@ 2006-11-05 10:50 ` Andreas Kretschmer <akretschmer@spamfence.net>
  1 sibling, 0 replies; 16+ messages in thread

From: Andreas Kretschmer @ 2006-11-05 10:50 UTC (permalink / raw)
  To: pgsql-general

Pedro Doria Meunier <pdoria@netmadeira.com> schrieb:

> 
> Hi all!
>  
> This is most certainly a lame question but perhaps someone is gracious enough
> to lend me a hand& ;-)
>  
> I have the following setup in a table:
>  
> The first record which is to be found (ok easy enough :D) with a timestamp
> meets a certain condition (ignition off)
> The following record is for the event of ignition on again with a timestamp.
>  
> So the question here is: how can I compute the time difference between these
> two records in a single query?
> Better yet: finding all the records that meet the first condition (ignition
> off) and the immediately following records as to compute the time difference.
> ;-)

Okay, let me try.

First, i created a similar table:

test=# select * from test;
 id | nr |             ts             | event
----+----+----------------------------+-------
  1 |  1 | 2006-11-05 11:20:34.308945 |     0
  2 |  2 | 2006-11-05 11:20:41.245691 |     0
  3 |  2 | 2006-11-05 11:20:43.630381 |     1
  4 |  1 | 2006-11-05 11:20:49.762882 |     1
  5 |  3 | 2006-11-05 11:20:55.427288 |     0
(5 rows)


As we can see, i have a column nr to identify paired rows. The
event-column is similar to your ignition (off-on -> 0-1).
The rows with id 1 and 4, and 2 and 3 paired.

There are only 0-events and paired 1-events or only a 0-event,
and only one pair for every nr.


Now i want to know the elapsed time for every nr (1 and 2) between the
0 and 1 - event:

test=# select	a.id, 
		a.nr, 
		a.ts as event_off, 
		a.event, 
		b.id, 
		b.ts as event_on, 
		b.ts-a.ts as elapsed 
	from test a, test b 
	where (a.nr=b.nr and a.ts<b.ts);
 id | nr |         event_off          | event | id |          event_on          |     elapsed
----+----+----------------------------+-------+----+----------------------------+-----------------
  1 |  1 | 2006-11-05 11:20:34.308945 |     0 |  4 | 2006-11-05 11:20:49.762882 | 00:00:15.453937
  2 |  2 | 2006-11-05 11:20:41.245691 |     0 |  3 | 2006-11-05 11:20:43.630381 | 00:00:02.38469
(2 rows)






Hope that helps, Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."    (unknow)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: help with a query
  2006-11-03 20:29 help with a query Pedro Doria Meunier <pdoria@netmadeira.com>
@ 2006-11-06 00:11 ` redhog <redhog@redhog.org>
  1 sibling, 0 replies; 16+ messages in thread

From: redhog @ 2006-11-06 00:11 UTC (permalink / raw)
  To: pgsql-general

If you do not want to amend your table with extra information, this is
how you do it:

Suppose you have a table

create table events (
 time timestamp,
 object int refers objects(id), -- The thing that had its ignition
turned on or off at this time
 ignition boolean,
 comment varchar
);

You can then do

select distinct on (e1.time, e1.object, e1.comment)
 e1.time as start,
 e2.time as end,
 e2.time - e1.time as duration,
 e1.object,
 e1.comment
from
 events as e1,
 events as e2
where
 e1.object = e2.object
 and e1.time < e2.time
order by
 e1.time, e1.object, e1.comment, e2.time asc;

The trick here is to sort by e2.time in ascending order, and to not
include e2.time (or any of e2:s  fields) in the distinct condition.
This causes all rows where the values from e1 are the same to be
considered duplicates, and only the first one of them included in the
result - which is the one with the lowest e2.time value, since we did
sort on that key in ascending order.

Note that this trick works since order by is processed before the
distinct clause. This is specifically noted in the PostgreSQL manual.

Note that there is a slightly more "intuitive" way of doing this using
a subquery with a min() aggregate function, but this is considerably
less efficient, especially with larger tables (use "explain" and you'll
understund why).




^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* help with a query
@ 2021-11-13 09:13 yarden tamam <tamamyarden@gmail.com>
  2021-11-15 14:30 ` Re: help with a query Bruce Momjian <bruce@momjian.us>
  2021-11-15 15:23 ` Re: help with a query Francisco Olarte <folarte@peoplecall.com>
  0 siblings, 2 replies; 16+ messages in thread

From: yarden tamam @ 2021-11-13 09:13 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

i have data bast that represents basketball teams. and i'm trying to find
one team that has not won yet but has gain that most points during their
games from the other team who have not won yet.

i wrote a query that is checking if a team has not won at all. but i'm
trying to figure out how to count the points because every team play either
at home or away.

thank you for you help.

here is the query the i have wrote so far:
select team.id, nickname,color
from team,points,game
where (team.tid = game.vtid and (vscore - hscore ) < 0)  or (game.htid =
team.tid and (hscore - vscore )< 0 )
having

^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: help with a query
  2021-11-13 09:13 help with a query yarden tamam <tamamyarden@gmail.com>
@ 2021-11-15 14:30 ` Bruce Momjian <bruce@momjian.us>
  2021-11-15 14:31   ` Re: help with a query Bruce Momjian <bruce@momjian.us>
  1 sibling, 1 reply; 16+ messages in thread

From: Bruce Momjian @ 2021-11-15 14:30 UTC (permalink / raw)
  To: yarden tamam <tamamyarden@gmail.com>; +Cc: pgsql-general@lists.postgresql.org


Due to time constraints, I do not directly answer general PostgreSQL
questions.  For assistance, please join the appropriate mailing list and
post your question:

	https://www.postgresql.org/community

You can also try the #postgresql IRC channel on irc.freenode.net.  See
the PostgreSQL FAQ for more information.

---------------------------------------------------------------------------


On Sat, Nov 13, 2021 at 11:13:25AM +0200, yarden tamam wrote:
> i have data bast that represents basketball teams. and i'm trying to find one
> team that has not won yet but has gain that most points during their games from
> the other team who have not won yet.
> 
> i wrote a query that is checking if a team has not won at all. but i'm trying
> to figure out how to count the points because every team play either at home or
> away.
> 
> thank you for you help.
> 
> here is the query the i have wrote so far:
> select team.id, nickname,color
> from team,points,game
> where (team.tid = game.vtid and (vscore - hscore ) < 0)  or (game.htid =
> team.tid and (hscore - vscore )< 0 )
> having

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.






^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: help with a query
  2021-11-13 09:13 help with a query yarden tamam <tamamyarden@gmail.com>
  2021-11-15 14:30 ` Re: help with a query Bruce Momjian <bruce@momjian.us>
@ 2021-11-15 14:31   ` Bruce Momjian <bruce@momjian.us>
  0 siblings, 0 replies; 16+ messages in thread

From: Bruce Momjian @ 2021-11-15 14:31 UTC (permalink / raw)
  To: yarden tamam <tamamyarden@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

On Mon, Nov 15, 2021 at 09:30:03AM -0500, Bruce Momjian wrote:
> 
> Due to time constraints, I do not directly answer general PostgreSQL
> questions.  For assistance, please join the appropriate mailing list and
> post your question:
> 
> 	https://www.postgresql.org/community
> 
> You can also try the #postgresql IRC channel on irc.freenode.net.  See
> the PostgreSQL FAQ for more information.

Sorry, my apologies for saying the above.  You are asking in the right
place, though the question is so unclear and with no specific Postgres
aspect, you might be better in an SQL forum.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.






^ permalink  raw  reply  [nested|flat] 16+ messages in thread

* Re: help with a query
  2021-11-13 09:13 help with a query yarden tamam <tamamyarden@gmail.com>
@ 2021-11-15 15:23 ` Francisco Olarte <folarte@peoplecall.com>
  1 sibling, 0 replies; 16+ messages in thread

From: Francisco Olarte @ 2021-11-15 15:23 UTC (permalink / raw)
  To: yarden tamam <tamamyarden@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

Yarden:

On Mon, 15 Nov 2021 at 10:30, yarden tamam <tamamyarden@gmail.com> wrote:
> i have data bast that represents basketball teams. and i'm trying to find one team that has not won yet but has gain that most points during their games from the other team who have not won yet.
> i wrote a query that is checking if a team has not won at all. but i'm trying to figure out how to count the points because every team play either at home or away.
> thank you for you help.
> here is the query the i have wrote so far:
> select team.id, nickname,color
> from team,points,game
> where (team.tid = game.vtid and (vscore - hscore ) < 0)  or (game.htid = team.tid and (hscore - vscore )< 0 )
> having

This has the faint smell of homework assignment, but anyway, decompose
the problem a bit and build from it, if I understand your logic you
can do more or less...

     with home_games_total as ( select htid as tid, sum(hscore) as
tscore, sum (vscore as oscore), max(hscore>vscore) as won_any )

This captures total result for home games  for the team, for the other
team, and wether it has won any match ( using the fact that true>false
in max, there are better ways but this is easy to type for an example
). Then you can add....

     with visitor_games_total as ( select vtid as tid, sum(vscore) as
tscore, sum (hscore as oscore), max(vscore>hscore) as won_any )

to capture visitor teams stats, then you just have to combine them with

    with all_games_totals as (select * from home games total union all
select * from visitor_games_total )

note each team will have play twice, one for the home team, one for
the visitor in the first two sum, and the last subquery will have two
rows per team, one for home games, one for visitor. Now just add them
all to get one row per team

   with team_result as (select tid, sum(tscore), sum(oscore),
max(won_any) from all_games_total group by tid)

and there you just have to search for wat you want:

   select * from team_result where not won_any order by tscore desc
limit 1 -- ( or use tscore-oscore, if you want point diffs )



^ permalink  raw  reply  [nested|flat] 16+ messages in thread


end of thread, other threads:[~2021-11-15 15:23 UTC | newest]

Thread overview: 16+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-04-25 07:41 Help with a query jose antonio leo <jaleo8@storelandia.com>
2003-04-25 08:38 ` Richard Huxton <dev@archonet.com>
2004-02-21 08:20 HELP with a Query Alex <alex@meerkatsoft.com>
2004-02-21 08:55 ` Re: HELP with a Query Martijn van Oosterhout <kleptog@svana.org>
2004-02-26 21:47 Help with a query Alexander Cohen <alex@toomuchspace.com>
2004-02-26 22:08 ` Erwin Van de Velde <erwin.vandevelde@ua.ac.be>
2004-02-26 22:40 ` Klint Gore <kg@kgb.une.edu.au>
2004-02-27 10:32 Re: Help with a query Bas Scheffers <bas@scheffers.net>
2004-02-27 12:29 Re: Help with a query Bas Scheffers <bas@scheffers.net>
2006-11-03 20:29 help with a query Pedro Doria Meunier <pdoria@netmadeira.com>
2006-11-05 10:50 ` Re: help with a query Andreas Kretschmer <akretschmer@spamfence.net>
2006-11-06 00:11 ` Re: help with a query redhog <redhog@redhog.org>
2021-11-13 09:13 help with a query yarden tamam <tamamyarden@gmail.com>
2021-11-15 14:30 ` Re: help with a query Bruce Momjian <bruce@momjian.us>
2021-11-15 14:31   ` Re: help with a query Bruce Momjian <bruce@momjian.us>
2021-11-15 15:23 ` Re: help with a query Francisco Olarte <folarte@peoplecall.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox