Vai al contenuto


Foto

SQL (PostgreSQL)


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

#21 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:45

Potrebbe anche andare ma ho un 3° campo che c'è solo nella tabella B e ciò invalida la union:


SELECT 

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

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

   B."MultipleValue"

FROM 

   "HaiClientPluginOption"  A

RIGHT OUTER JOIN 

   "PluginOption"  B

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

WHERE B."Id" = 1;


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

 


#22 LilithSChild

LilithSChild

    Schiavo

  • Donatori di sperma
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.764 Messaggi:

Inviato 19 ottobre 2011 - 13:49

Potrebbe anche andare ma ho un 3° campo che c'è solo nella tabella B e ciò invalida la union:


SELECT 

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

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

   B."MultipleValue"

FROM 

   "HaiClientPluginOption"  A

RIGHT OUTER JOIN 

   "PluginOption"  B

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

WHERE B."Id" = 1;


ah ok allora che la join sia con te :challenge:


ah e comunque:

SELECT 

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

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

   B."MultipleValue"

FROM 

   "HaiClientPluginOption"  A 

		RIGHT OUTER JOIN "PluginOption"  B ( ON A."Option" = B."Option" )

WHERE 

	B."Id" = 1;


Curiamo l'indentazione per cortesia :lol:

#23 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 19 ottobre 2011 - 13:54

Ma va a cagher! indenta il traffico di Roma prima di parlare :challenge:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#24 LilithSChild

LilithSChild

    Schiavo

  • Donatori di sperma
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 12.764 Messaggi:

Inviato 19 ottobre 2011 - 14:08

Ma va a cagher! indenta il traffico di Roma prima di parlare :lol:


no dai roma perderebbe metà del suo fascino senza le smart sopra gli alberi :challenge:

#25 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 

 


#26 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...

#27 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 

 


#28 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...

#29 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:

#30 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 

 


#31 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 

 


#32 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...

#33 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 

 


#34 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...

#35 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 

 


#36 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...

#37 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 

 


#38 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...

#39 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 

 


#40 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...