Nous utilisons des cookies pour vous garantir la meilleure expérience sur notre site. Si vous continuez à utiliser ce dernier, nous considèrerons que vous acceptez l'utilisation des cookies. J'ai compris ! ou En savoir plus !.
banniere

Le portail francophone de la géomatique


Toujours pas inscrit ? Mot de passe oublié ?
Nom d'utilisateur    Mot de passe              Toujours pas inscrit ?   Mot de passe oublié ?

Annonce

Printemps des cartes 2024

#1 Tue 30 June 2015 16:43

Pierre.Brochard
Participant occasionnel
Date d'inscription: 8 Apr 2015
Messages: 33

Union - Order by

Bonjour,

Je souhaite faire la jointure de mes 3 tables. Chaque requête fonctionne indépendamment, mais j'ai un syntax error au niveau de l'opérateur UNION. Vous sauriez pourquoi ?
Merci


Pierre



--  Produit, Prestation, Année 2013
select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt between   '01/01/2013 00:00:00' and  '31/12/2013 23:59:59'
order by refte1, prdname, prdcrdt


UNION all
--  Produit, Prestation, Année 2014
select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt between   '01/01/2014 00:00:00' and  '31/12/2014 23:59:59'
[b]order by refte1, prdname, prdcrdt[/b]


UNION all

-- Prestation, produits, année 2015
select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt >  '01/01/2015 00:00:00'
order by refte1, prdname, prdcrdt

Hors ligne

 

#2 Tue 30 June 2015 17:10

MathieuB
Membre du bureau
Lieu: Montpellier
Date d'inscription: 18 Jan 2006
Messages: 1220
Site web

Re: Union - Order by

Bonjour,

l'erreur vient de la clause ORDER BY dans les requêtes que vous voulez unir.

Pour y remédier et faire votre tri :

Code:

with foo AS (
select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt between   '01/01/2013 00:00:00' and  '31/12/2013 23:59:59'

UNION all

--  Produit, Prestation, Année 2014
select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt between   '01/01/2014 00:00:00' and  '31/12/2014 23:59:59'

UNION all

-- Prestation, produits, année 2015
select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt >  '01/01/2015 00:00:00'
)
SELECT * FROM foo ORDER BY refte1, prdname, prdcrdt

Dernière modification par MathieuB (Tue 30 June 2015 17:10)


Mathieu BOSSAERT
Association GeoRezo

Hors ligne

 

#3 Tue 30 June 2015 17:14

MathieuB
Membre du bureau
Lieu: Montpellier
Date d'inscription: 18 Jan 2006
Messages: 1220
Site web

Re: Union - Order by

Par contre je ne comprends pas l'intérêt de vos UNION. La requête ci-dessous fait le même travail :

Code:

SELECT refte1 AS type_produit, prdname AS prestation, to_char(prdcrdt, 'dd/MM/YYYY') AS date_création
FROM product, actor, refvalues
WHERE prdcreid = actid AND
prdtypologieid_ = refid AND (refte1 like '%Produit Prestation%'  OR refte1 like '%Produit Abonnement%')
AND ((prdcrdt between '01/01/2013 00:00:00' AND  '31/12/2013 23:59:59') 
OR (prdcrdt between   '01/01/2014 00:00:00' AND  '31/12/2014 23:59:59')
OR prdcrdt >  '01/01/2015 00:00:00')
ORDER BY by refte1, prdname, prdcrdt;

Dernière modification par MathieuB (Tue 30 June 2015 17:15)


Mathieu BOSSAERT
Association GeoRezo

Hors ligne

 

#4 Tue 30 June 2015 17:15

Benoit91
Participant assidu
Date d'inscription: 2 Oct 2008
Messages: 263

Re: Union - Order by

Bonjour

Il me semble que ce sont les order by qui pose problème.
testé en les enlevant de vos requetes select et d'en mettre un seul à la fin de vos requêtes union

exemple:

Code:

select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt between   '01/01/2013 00:00:00' and  '31/12/2013 23:59:59'

UNION all

select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt between   '01/01/2014 00:00:00' and  '31/12/2014 23:59:59'

UNION all

select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt >  '01/01/2015 00:00:00'

order by refte1, prdname, prdcrdt

Cordialement.

EDIT: Mathieu a été plus rapide et avec une réponse plus appropriée.

Dernière modification par Benoit91 (Tue 30 June 2015 17:17)

Hors ligne

 

#5 Tue 30 June 2015 17:20

MathieuB
Membre du bureau
Lieu: Montpellier
Date d'inscription: 18 Jan 2006
Messages: 1220
Site web

Re: Union - Order by

Benoit91 a écrit:

EDIT: Mathieu a été plus rapide et avec une réponse plus appropriée.


Mais sans la CTE, c'est plus simple :-)


Mathieu BOSSAERT
Association GeoRezo

Hors ligne

 

#6 Wed 08 July 2015 16:42

Pierre.Brochard
Participant occasionnel
Date d'inscription: 8 Apr 2015
Messages: 33

Re: Union - Order by

Excusez moi de ne vous répondre que maintenant.


En fait oui, il s"agissait d'un problème de parenthèse. Assez vicieux, je dirais, tel que :

(select refte1 as type_produit, prdname as prestation, to_char(prdcrdt, 'dd/MM/YYYY') as date_création
from product, actor, refvalues
where prdcreid = actid and
prdtypologieid_ = refid and (refte1 like '%Produit Prestation%'  or refte1 like '%Produit Abonnement%')
and prdcrdt between   '01/01/2014 00:00:00' and  '31/12/2014 23:59:59'
group by prdcrdt, refte1, prdname
order by prdcrdt, refte1, prdname)

J'avais bien essayé d'enlever les order by mais je n'avais plus le résultat voulu.

Cordialement.

Hors ligne

 

#7 Thu 09 July 2015 11:08

Pierre.Brochard
Participant occasionnel
Date d'inscription: 8 Apr 2015
Messages: 33

Re: Union - Order by

Merci pour votre réponse Mathieu, le regroupement est primordial dans la suite de ma démarche.

Cordialement,

Pierre Brochard

Hors ligne

 

#8 Thu 09 July 2015 11:11

Pierre.Brochard
Participant occasionnel
Date d'inscription: 8 Apr 2015
Messages: 33

Re: Union - Order by

Merci pour votre réponse Mathieu, le regroupement est primordial dans la suite de ma démarche.

Cordialement,

Pierre Brochard

Hors ligne

 

Pied de page des forums

Powered by FluxBB