pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
need query advice
2+ messages / 2 participants
[nested] [flat]

* need query advice
@ 2004-10-16 09:08  Stephan Fiebrandt <bsd@kuehlbox.de>
  0 siblings, 1 reply; 2+ messages in thread

From: Stephan Fiebrandt @ 2004-10-16 09:08 UTC (permalink / raw)
  To: pgsql-sql

Hello all,

i've got a psql database, with a table including 6 rows named "zahl1 
zahl2 zahl3 zahl4 zahl5 and zahl6" type integer

There are about  10.000 entries in the table. In every entry, the 
numbers are uniq to each other. That means, if zahl1=1, then zahl2-6 
cannot be also 1. Example:

1, 10, 78, 43, 2, 67              (all 6 numbers are different in the 
same line, never a dup)
56, 34, 78, 1, 77, 99            (but repeating in the db tousand times 
of course)
1, 2, 3, 4, 5, 6
2, 3, 4, 99, 5, 6
...


$a ... $f are the variables, of the 6 numbers i am searching: The 
numbers that are given to search, are also uniq of course, or it would 
not make sense.
I need all hits, where at leased 5 of the given 6 numbers are maching in 
every entry:


select * from zaehlerzuordnung_snapshots where ((zahl1=$a or zahl2=$a or 
zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a) and (zahl1=$b or zahl2=$b 
or zahl3=$b or zahl4=$b or zahl5=$b or zahl6=$b) and (zahl1=$c or 
zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c or zahl6=$c) and (zahl1=$d 
or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d or zahl6=$d) and 
(zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or zahl5=$e or zahl6=$e))

or

((zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or zahl6=$b) 
and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c or 
zahl6=$c) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d 
or zahl6=$d) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or 
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f 
or zahl5=$f or zahl6=$f))

or

((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a) 
and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c or 
zahl6=$c) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d 
or zahl6=$d) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or 
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f 
or zahl5=$f or zahl6=$f))

or

((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a) 
and (zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or 
zahl6=$b) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or zahl5=$d 
or zahl6=$d) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or 
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f 
or zahl5=$f or zahl6=$f))

or

((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a) 
and (zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or 
zahl6=$b) and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c 
or zahl6=$c) and (zahl1=$e or zahl2=$e or zahl3=$e or zahl4=$e or 
zahl5=$e or zahl6=$e) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f 
or zahl5=$f or zahl6=$f))

or


((zahl1=$a or zahl2=$a or zahl3=$a or zahl4=$a or zahl5=$a or zahl6=$a) 
and (zahl1=$b or zahl2=$b or zahl3=$b or zahl4=$b or zahl5=$b or 
zahl6=$b) and (zahl1=$c or zahl2=$c or zahl3=$c or zahl4=$c or zahl5=$c 
or zahl6=$c) and (zahl1=$d or zahl2=$d or zahl3=$d or zahl4=$d or 
zahl5=$d or zahl6=$d) and (zahl1=$f or zahl2=$f or zahl3=$f or zahl4=$f 
or zahl5=$f or zahl6=$f)) ;


well.. this query is working. But for sure totaly ineffictive written. I 
need the advice of your professionals to more simply that query.
As next problem i have to search if already 4 of the given 6 numbers are 
machting the 6 numbers per entrie.
But to write a query like i did above.. wount do the trick i guess. Any 
hints? i really need it soon :(

greets,

Yze



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

* Re: need query advice
@ 2004-10-20 00:14  Pierre-Frédéric Caillaud <lists@boutiquenumerique.com>
  parent: Stephan Fiebrandt <bsd@kuehlbox.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Pierre-Frédéric Caillaud @ 2004-10-20 00:14 UTC (permalink / raw)
  To: pgsql-sql


	argh, you could use contribs/intarray with a gist index...
	instead of N columns use an integer[] and gist-index it, then write the  
equivalent of :

	where (intersection of the search array with the data array) has at least  
5 elements
	(or 4 elements)

	(or at least 4 elements order by the number of elements desc)

	in your table defs, you can express your unicity constraint on the array  
with :
	CHECK( uniq(yourarrau) = yourarrau AND length(yourarrau)=6 )





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


end of thread, other threads:[~2004-10-20 00:14 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-10-16 09:08 need query advice Stephan Fiebrandt <bsd@kuehlbox.de>
2004-10-20 00:14 ` Pierre-Frédéric Caillaud <lists@boutiquenumerique.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