SELECT: Zufällige Wiedergabe einer Wertes

dortmund88

Neuer Benutzer
Beiträge
3
Ich habe eine Tabelle mit

  • Artikelnummern
  • Zusatzinformationen

als Spalten. Es kann zu einer Artikelnummer unendliche Zusatzinformationen geben.

Jetzt möchte ich mir willkürlich eine Zusatzinformation ausgeben mit Gruppierung auf die Artikelnummer.

Wie mache ich das? DANKE!
 
Werbung:
für eine spezifische Artikelnummer?

Zeig mal ein Beispiel, oder vielleicht hilft ja schon das folgende:

Code:
test=*# select * from dortmund88 ;
 artikel | info  
---------+-------
  1 | Info1
  1 | Info2
  2 | Info3
  2 | Info4
  2 | Info5
(5 Zeilen)

test=*# select artikel, array_agg(info) from dortmund88 group by artikel;
 artikel |  array_agg   
---------+---------------------
  1 | {Info1,Info2}
  2 | {Info3,Info4,Info5}
(2 Zeilen)

test=*# select artikel, array_agg(info order by random()) from dortmund88 group by artikel;
 artikel |  array_agg   
---------+---------------------
  1 | {Info1,Info2}
  2 | {Info4,Info3,Info5}
(2 Zeilen)

test=*# select artikel, array_agg(info order by random()) from dortmund88 group by artikel;
 artikel |  array_agg   
---------+---------------------
  1 | {Info2,Info1}
  2 | {Info5,Info3,Info4}
(2 Zeilen)

test=*#
 
Mhm bei mir kommt
ORA-00904: "ARRAY_AGG": ungültiger Bezeichner

Also ich bräuchte z. B. für Artikel 1 nur Info 2 (oder halt Info 1) usw. kein LISTAGG oder so :)
 
okay. array_agg() ist eine Aggregatsfuktion, hat Oraggle sicher auch (bei dem, was das kostet ...)

aber es geht auch anders:

Code:
test=*# with foo as (select artikel, info, row_number() over (partition by artikel order by random()) from dortmund88) select artikel, info from foo where row_number = 1 ;
 artikel | info  
---------+-------
  1 | Info1
  2 | Info5
(2 Zeilen)

test=*# with foo as (select artikel, info, row_number() over (partition by artikel order by random()) from dortmund88) select artikel, info from foo where row_number = 1 ;
 artikel | info  
---------+-------
  1 | Info2
  2 | Info5
(2 Zeilen)

test=*#
 
Werbung:
der Vollständigkeit halber noch die ARRAY-Lösung:

Code:
test=*# select artikel, array_agg[1] from (select artikel, array_agg(info order by random()) from dortmund88 group by artikel) foo;
 artikel | array_agg
---------+-----------
  1 | Info1
  2 | Info4
(2 Zeilen)

test=*# select artikel, array_agg[1] from (select artikel, array_agg(info order by random()) from dortmund88 group by artikel) foo;
 artikel | array_agg
---------+-----------
  1 | Info1
  2 | Info5
(2 Zeilen)

test=*# select artikel, array_agg[1] from (select artikel, array_agg(info order by random()) from dortmund88 group by artikel) foo;
 artikel | array_agg
---------+-----------
  1 | Info2
  2 | Info3
(2 Zeilen)

test=*#
 
Zurück
Oben