Vai al contenuto


Foto

SQL (PostgreSQL)


Questa discussione e' stata archiviata Questo significa che non e' possibile rispondere
96 risposte a questa discussione

#1 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 12:59

C'è qualcuno esperto di SQL e in particolare di PostgreSQL ?

Nella tabella A ho:


- Option -+--- Value  ------------------------------------------------------------------------------

 jInitFile | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 inputKey  | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3


Nella tabella B ho:

----- Option -----------+--- Value  ------------------------------------------------------------------------------

 inputKey                | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3

 managementKey           | VSS.1.1.2

 jInitFile               | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 resultControl.key       | CMD.5.5.5.5

 recognictionControl.key | CMD.6.6.6.6

 recognictionControl.foo | GOOFY.6.6.6

Quindi nella tabella A ho solo 2 records presenti nella tabella B.
Ora con una sola query vorrei estrarre tutti i records:
se ci sono nella A li prendo dalla A, altrimenti dalla B dove so che ci sono tutti (è una tabella "template").

Dove sbaglio ? (con Oracle userei NVL ma su postgreSQL c'è COALESCE)

SELECT                 

   COALESCE(TRIM(A."Option"), TRIM(B."Option"      )),

   COALESCE(TRIM(A."Value" ), TRIM(B."DefaultValue"))

FROM                    

   "HaiClientPluginOption" A,

   "PluginOption"          B

WHERE

    A."HaiPluginOptionId" = B."Id"

AND B."Id" = 1;


Output:
coalesce  |                                        coalesce                                        

-----------+----------------------------------------------------------------------------------------

 jInitFile | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 inputKey  | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3

 jInitFile | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 inputKey  | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3

 jInitFile | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 inputKey  | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3

 jInitFile | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 inputKey  | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3

 jInitFile | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 inputKey  | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3

 jInitFile | /home/user/workspace/hai2.0/test/linux/asrTwoClientPCMSimplex/client_2/jconf.both

 inputKey  | AUDIO.5.1.1;AUDIO.5.2.2;AUDIO.5.3.3

(12 rows)




ho provato ad aggiungere questa:


AND B."Option" IN 

(

   SELECT "Option" FROM "PluginOption" WHERE "Id" = 1

);


ma nada.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#2 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:00

Mai sentito parlare di JOIN?
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#3 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:01

Mai sentito parlare di JOIN?

Ma si, ho fatto sql a tonnellate in Italia ma son passati anni.

Dai, sputa l'osso please.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#4 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:10

Prova così:

SELECT *
FROM TABELLA1 AS A
LEFT OUTER JOIN TABELLA2 AS B
ON A.OPTION = B.OPTION
WHERE


dove TABELLA1 è quella con meno record.
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#5 LilithSChild

LilithSChild

    Schiavo

  • Donatori di sperma
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.764 Messaggi:

Inviato 19 ottobre 2011 - 13:10

quando torno in ufficio ci butto un occhio ma attento che coalesce e nvl son due cose diverse :challenge:

#6 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:18

Prova così:

SELECT *
FROM TABELLA1 AS A
LEFT OUTER JOIN TABELLA2 AS B
ON A.OPTION = B.OPTION
WHERE


dove TABELLA1 è quella con meno record.

Allora, dovrebbero esserci tutti i record della tabella B quindi, se non ricordo male,
è RIGHT OUTER JOIN.
Mi da 6 records però il campo Option ha solo i 2 record della tabella A ed è vuoto per gli altri 4 che ci sono nella B.
Con LEFT OUTER JOIN invece ottengo solo 2 records.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#7 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:20

quando torno in ufficio ci butto un occhio ma attento che coalesce e nvl son due cose diverse :challenge:

Più o meno non è la stessa cosa ? solo che coalesce accetta più parametri mentre la nvl solo 2.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#8 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:22

L'OUTER JOIN ti restituisce tutti i record, compresi quelli che non hanno corrispondenza.

Non avevo voglia di farmi le tue tabelle qui in ufficio e non ho potuto provare.

Prova la FULL OUTER JOIN.
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#9 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:23

Ok, trovato. Ho mischiato la tua join con la mia coalesce.

SELECT 

   COALESCE(TRIM(A."Option"), TRIM(B."Option")),

   COALESCE(TRIM(A."Value") , TRIM(B."Value"))

FROM 

   "HaiClientPluginOption"  A

RIGHT OUTER JOIN 

   "PluginOption"  B

ON A."Option" = B."Option"

WHERE B."Id" = 1;


Grazie 1000 :challenge:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#10 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:25

Mi devi una birra tedesca alla spina.
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#11 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:27

Non te la posso spedire. Se vieni su te ne offro anche 10 (totale = 15€ al pub universitario :challenge: )
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#12 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:28

Il fatto di consumarla in loco era sottinteso...

:challenge:
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#13 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:30

Quando vuoi (non scherzo).

Ma la nera non te la presento più, è tornata col marito.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#14 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:32

Dopo quello che mi hai scritto manco la volevo conoscere...

Comunque un giretto in Germania me lo farei volentieri.

Tu dove vivi? Io ho delle amiche a Köln...
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#15 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:36

Vicino Erfurt (Ilmenau).

381km da Köln.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#16 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:38

381km da Köln.

Un po' troppo direi... :lol:

Comunque se mi gira lo sghiribizzo ti avviso...

:challenge:
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#17 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:39

Ok, messo birra in frigo :challenge:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#18 LilithSChild

LilithSChild

    Schiavo

  • Donatori di sperma
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.764 Messaggi:

Inviato 19 ottobre 2011 - 13:40

Scusate ma :

Select 

Campo1,

Campo2

FROM Tabella_A

UNION

Select 

Campo1,

Campo2

FROM Tabella_B

Così ti estrae entrambe i campi e se ci sono uguaglianze vengono "distinctate" di default dalla union?

#19 Nex

Nex

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 19.500 Messaggi:

Inviato 19 ottobre 2011 - 13:41

UNION = merda

Meglio le JOIN
Then I went to my pretty rose tree / To tend her by day and by night / But my rose turned away with jealousy / And her thorns were my only delight...

#20 LilithSChild

LilithSChild

    Schiavo

  • Donatori di sperma
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.764 Messaggi:

Inviato 19 ottobre 2011 - 13:43

UNION = merda

Meglio le JOIN


ovvio, ma per una tabella di 4 record con la join ti incasini la vita e basta.