Pour une besoin specifique, je me retrouve avec une
requete (qui fonctionne) mais qui est epouvantable. Si
qq'un pouvait me dire comment la simplifier ce serait
avec plaisir.
select a.reference,b.libelle,a.matiere,a.couleur,a.taille,
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate()) * 100 +
datepart(wk,getdate())),0),
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate() + 7) *
100 + datepart(wk,getdate() + 7)),0),
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate() + 14) *
100 + datepart(wk,getdate() + 14)),0),
.... je vous fait grace des 18 autres coalesce pour
recuperer les 18 autres semaines ....
from planproduction a , articles b where site = @site and
b.reference = a.reference
group by
a.reference,b.libelle,a.matiere,a.couleur,a.taille
order by
a.reference,b.libelle,a.matiere,a.couleur,a.taille
sur la table
CREATE TABLE [dbo].[planproduction] (
[site] [smallint] NOT NULL ,
[reference] [char] (15) COLLATE Latin1_General_BIN NOT
NULL ,
[matiere] [char] (15) COLLATE Latin1_General_BIN NOT
NULL ,
[couleur] [char] (3) COLLATE Latin1_General_BIN NOT NULL ,
[taille] [char] (5) COLLATE Latin1_General_BIN NOT NULL ,
[quantieme] [int] NOT NULL ,
[quantite] [decimal](15, 3) NOT NULL ,
[lck] [smallint] NOT NULL
) ON [PRIMARY]
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
Fred BROUARD
créer uen table des semaines faire une jointure de cette table des semaines avec planproduction et en calculer les somme pour chaque semaine intégrer cela à ta requête.
A +
Philip a écrit:
Pour une besoin specifique, je me retrouve avec une requete (qui fonctionne) mais qui est epouvantable. Si qq'un pouvait me dire comment la simplifier ce serait avec plaisir.
select a.reference,b.libelle,a.matiere,a.couleur,a.taille, coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate()) * 100 + datepart(wk,getdate())),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 7) * 100 + datepart(wk,getdate() + 7)),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 14) * 100 + datepart(wk,getdate() + 14)),0),
.... je vous fait grace des 18 autres coalesce pour recuperer les 18 autres semaines ....
from planproduction a , articles b where site = @site and b.reference = a.reference group by a.reference,b.libelle,a.matiere,a.couleur,a.taille order by a.reference,b.libelle,a.matiere,a.couleur,a.taille sur la table CREATE TABLE [dbo].[planproduction] ( [site] [smallint] NOT NULL , [reference] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [matiere] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [couleur] [char] (3) COLLATE Latin1_General_BIN NOT NULL , [taille] [char] (5) COLLATE Latin1_General_BIN NOT NULL , [quantieme] [int] NOT NULL , [quantite] [decimal](15, 3) NOT NULL , [lck] [smallint] NOT NULL ) ON [PRIMARY]
Par avance merci. Salutations. Philip
-- Frédéric BROUARD, MVP MS SQL Server. Expert Langage SQL / Delphi / web Livre SQL - col. Référence : http://sqlpro.developpez.com/bookSQL.html Le site du SQL, pour débutants et pros : http://sqlpro.developpez.com ************************ www.datasapiens.com *************************
créer uen table des semaines
faire une jointure de cette table des semaines avec planproduction et en calculer les somme pour chaque semaine
intégrer cela à ta requête.
A +
Philip a écrit:
Pour une besoin specifique, je me retrouve avec une
requete (qui fonctionne) mais qui est epouvantable. Si
qq'un pouvait me dire comment la simplifier ce serait
avec plaisir.
select a.reference,b.libelle,a.matiere,a.couleur,a.taille,
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate()) * 100 +
datepart(wk,getdate())),0),
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate() + 7) *
100 + datepart(wk,getdate() + 7)),0),
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate() + 14) *
100 + datepart(wk,getdate() + 14)),0),
.... je vous fait grace des 18 autres coalesce pour
recuperer les 18 autres semaines ....
from planproduction a , articles b where site = @site and
b.reference = a.reference
group by
a.reference,b.libelle,a.matiere,a.couleur,a.taille
order by
a.reference,b.libelle,a.matiere,a.couleur,a.taille
sur la table
CREATE TABLE [dbo].[planproduction] (
[site] [smallint] NOT NULL ,
[reference] [char] (15) COLLATE Latin1_General_BIN NOT
NULL ,
[matiere] [char] (15) COLLATE Latin1_General_BIN NOT
NULL ,
[couleur] [char] (3) COLLATE Latin1_General_BIN NOT NULL ,
[taille] [char] (5) COLLATE Latin1_General_BIN NOT NULL ,
[quantieme] [int] NOT NULL ,
[quantite] [decimal](15, 3) NOT NULL ,
[lck] [smallint] NOT NULL
) ON [PRIMARY]
Par avance merci.
Salutations.
Philip
--
Frédéric BROUARD, MVP MS SQL Server. Expert Langage SQL / Delphi / web
Livre SQL - col. Référence : http://sqlpro.developpez.com/bookSQL.html
Le site du SQL, pour débutants et pros : http://sqlpro.developpez.com
************************ www.datasapiens.com *************************
créer uen table des semaines faire une jointure de cette table des semaines avec planproduction et en calculer les somme pour chaque semaine intégrer cela à ta requête.
A +
Philip a écrit:
Pour une besoin specifique, je me retrouve avec une requete (qui fonctionne) mais qui est epouvantable. Si qq'un pouvait me dire comment la simplifier ce serait avec plaisir.
select a.reference,b.libelle,a.matiere,a.couleur,a.taille, coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate()) * 100 + datepart(wk,getdate())),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 7) * 100 + datepart(wk,getdate() + 7)),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 14) * 100 + datepart(wk,getdate() + 14)),0),
.... je vous fait grace des 18 autres coalesce pour recuperer les 18 autres semaines ....
from planproduction a , articles b where site = @site and b.reference = a.reference group by a.reference,b.libelle,a.matiere,a.couleur,a.taille order by a.reference,b.libelle,a.matiere,a.couleur,a.taille sur la table CREATE TABLE [dbo].[planproduction] ( [site] [smallint] NOT NULL , [reference] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [matiere] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [couleur] [char] (3) COLLATE Latin1_General_BIN NOT NULL , [taille] [char] (5) COLLATE Latin1_General_BIN NOT NULL , [quantieme] [int] NOT NULL , [quantite] [decimal](15, 3) NOT NULL , [lck] [smallint] NOT NULL ) ON [PRIMARY]
Par avance merci. Salutations. Philip
-- Frédéric BROUARD, MVP MS SQL Server. Expert Langage SQL / Delphi / web Livre SQL - col. Référence : http://sqlpro.developpez.com/bookSQL.html Le site du SQL, pour débutants et pros : http://sqlpro.developpez.com ************************ www.datasapiens.com *************************
Philip
Merci pour ta soluce. (j'essayai de voir s'il n'y avait pas plus simple) Salutations Philip
Merci pour ta soluce. (j'essayai de voir s'il n'y avait
pas plus simple)
Salutations
Philip
Merci pour ta soluce. (j'essayai de voir s'il n'y avait pas plus simple) Salutations Philip
Steve Kass
Philip,
Cette requête est-elle l'équivalente?
select a.reference, b.libelle, a.matiere, a.couleur, a.taille, sum(case when quantieme = Q1 then quantite else 0 end), sum(case when quantieme = Q2 then quantite else 0 end), sum(case when quantieme = Q3 then quantite else 0 end), sum(case when quantieme = Q4 then quantite else 0 end), ...
from ( select datepart(yy,getdate())*100 + datepart(wk,getdate()) as Q1, datepart(yy,getdate()+7) * 100 + datepart(wk,getdate()+7) as Q2, datepart(yy,getdate()+14) * 100 + datepart(wk,getdate()+14) as Q3, datepart(yy,getdate()+21) * 100 + datepart(wk,getdate()+21) as Q4, ... ) W, planproduction a, articles b where site = @site and b.reference = a.reference group by a.reference, b.libelle, a.matiere, a.couleur, a.taille order by a.reference, b.libelle, a.matiere, a.couleur, a.taille
Steve Kass Drew University
Philip wrote:
Pour une besoin specifique, je me retrouve avec une requete (qui fonctionne) mais qui est epouvantable. Si qq'un pouvait me dire comment la simplifier ce serait avec plaisir.
select a.reference,b.libelle,a.matiere,a.couleur,a.taille, coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate()) * 100 + datepart(wk,getdate())),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 7) * 100 + datepart(wk,getdate() + 7)),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 14) * 100 + datepart(wk,getdate() + 14)),0),
.... je vous fait grace des 18 autres coalesce pour recuperer les 18 autres semaines ....
from planproduction a , articles b where site = @site and b.reference = a.reference group by a.reference,b.libelle,a.matiere,a.couleur,a.taille order by a.reference,b.libelle,a.matiere,a.couleur,a.taille sur la table CREATE TABLE [dbo].[planproduction] ( [site] [smallint] NOT NULL , [reference] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [matiere] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [couleur] [char] (3) COLLATE Latin1_General_BIN NOT NULL , [taille] [char] (5) COLLATE Latin1_General_BIN NOT NULL , [quantieme] [int] NOT NULL , [quantite] [decimal](15, 3) NOT NULL , [lck] [smallint] NOT NULL ) ON [PRIMARY]
Par avance merci. Salutations. Philip
Philip,
Cette requête est-elle l'équivalente?
select
a.reference,
b.libelle,
a.matiere,
a.couleur,
a.taille,
sum(case when quantieme = Q1 then quantite else 0 end),
sum(case when quantieme = Q2 then quantite else 0 end),
sum(case when quantieme = Q3 then quantite else 0 end),
sum(case when quantieme = Q4 then quantite else 0 end),
...
from (
select
datepart(yy,getdate())*100 + datepart(wk,getdate()) as Q1,
datepart(yy,getdate()+7) * 100 + datepart(wk,getdate()+7) as Q2,
datepart(yy,getdate()+14) * 100 + datepart(wk,getdate()+14) as Q3,
datepart(yy,getdate()+21) * 100 + datepart(wk,getdate()+21) as Q4,
...
) W, planproduction a, articles b
where site = @site
and b.reference = a.reference
group by a.reference, b.libelle, a.matiere, a.couleur, a.taille
order by a.reference, b.libelle, a.matiere, a.couleur, a.taille
Steve Kass
Drew University
Philip wrote:
Pour une besoin specifique, je me retrouve avec une
requete (qui fonctionne) mais qui est epouvantable. Si
qq'un pouvait me dire comment la simplifier ce serait
avec plaisir.
select a.reference,b.libelle,a.matiere,a.couleur,a.taille,
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate()) * 100 +
datepart(wk,getdate())),0),
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate() + 7) *
100 + datepart(wk,getdate() + 7)),0),
coalesce ((select sum(quantite) from planproduction
where site = @site and reference = a.reference and
matiere = a.matiere and couleur = a.couleur and taille =
a.taille and quantieme = datepart(yy,getdate() + 14) *
100 + datepart(wk,getdate() + 14)),0),
.... je vous fait grace des 18 autres coalesce pour
recuperer les 18 autres semaines ....
from planproduction a , articles b where site = @site and
b.reference = a.reference
group by
a.reference,b.libelle,a.matiere,a.couleur,a.taille
order by
a.reference,b.libelle,a.matiere,a.couleur,a.taille
sur la table
CREATE TABLE [dbo].[planproduction] (
[site] [smallint] NOT NULL ,
[reference] [char] (15) COLLATE Latin1_General_BIN NOT
NULL ,
[matiere] [char] (15) COLLATE Latin1_General_BIN NOT
NULL ,
[couleur] [char] (3) COLLATE Latin1_General_BIN NOT NULL ,
[taille] [char] (5) COLLATE Latin1_General_BIN NOT NULL ,
[quantieme] [int] NOT NULL ,
[quantite] [decimal](15, 3) NOT NULL ,
[lck] [smallint] NOT NULL
) ON [PRIMARY]
select a.reference, b.libelle, a.matiere, a.couleur, a.taille, sum(case when quantieme = Q1 then quantite else 0 end), sum(case when quantieme = Q2 then quantite else 0 end), sum(case when quantieme = Q3 then quantite else 0 end), sum(case when quantieme = Q4 then quantite else 0 end), ...
from ( select datepart(yy,getdate())*100 + datepart(wk,getdate()) as Q1, datepart(yy,getdate()+7) * 100 + datepart(wk,getdate()+7) as Q2, datepart(yy,getdate()+14) * 100 + datepart(wk,getdate()+14) as Q3, datepart(yy,getdate()+21) * 100 + datepart(wk,getdate()+21) as Q4, ... ) W, planproduction a, articles b where site = @site and b.reference = a.reference group by a.reference, b.libelle, a.matiere, a.couleur, a.taille order by a.reference, b.libelle, a.matiere, a.couleur, a.taille
Steve Kass Drew University
Philip wrote:
Pour une besoin specifique, je me retrouve avec une requete (qui fonctionne) mais qui est epouvantable. Si qq'un pouvait me dire comment la simplifier ce serait avec plaisir.
select a.reference,b.libelle,a.matiere,a.couleur,a.taille, coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate()) * 100 + datepart(wk,getdate())),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 7) * 100 + datepart(wk,getdate() + 7)),0), coalesce ((select sum(quantite) from planproduction where site = @site and reference = a.reference and matiere = a.matiere and couleur = a.couleur and taille = a.taille and quantieme = datepart(yy,getdate() + 14) * 100 + datepart(wk,getdate() + 14)),0),
.... je vous fait grace des 18 autres coalesce pour recuperer les 18 autres semaines ....
from planproduction a , articles b where site = @site and b.reference = a.reference group by a.reference,b.libelle,a.matiere,a.couleur,a.taille order by a.reference,b.libelle,a.matiere,a.couleur,a.taille sur la table CREATE TABLE [dbo].[planproduction] ( [site] [smallint] NOT NULL , [reference] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [matiere] [char] (15) COLLATE Latin1_General_BIN NOT NULL , [couleur] [char] (3) COLLATE Latin1_General_BIN NOT NULL , [taille] [char] (5) COLLATE Latin1_General_BIN NOT NULL , [quantieme] [int] NOT NULL , [quantite] [decimal](15, 3) NOT NULL , [lck] [smallint] NOT NULL ) ON [PRIMARY]
Par avance merci. Salutations. Philip
Philip
Bonjour, Oui et en plus elle est legerement plus rapide et il y a moins de lectures logiques, encore merci, @+ salutations Philip
Bonjour,
Oui et en plus elle est legerement plus rapide et il y a
moins de lectures logiques,
encore merci,
@+
salutations
Philip