Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Ecriture de requete

4 réponses
Avatar
jerome
Bonjour,

J'ai un problème d'écriture de requête.
J'ai une table MTARIF avec
MTAR_ID int ID PK autoincrement
MARC_ID int (id de la marchandise pour ce tarif)
MTAR_DATEDEBUT smalldatetime (date de début de validité du tarif)
MTAR_DATEFIN smalldatetime (date de finde validité du tarif)
MTAR_MONTANT decimal(18,2) valeur du tarif

les tarifs changent et peuvent être saisis à l'avance.

Je souhaiterais connaître pour une date donnée le tarif en cours et le tarif
précédent. EN sachant qu'il peur y avoir plusieurs tarifs précédents et que
je souhaite le dernier plus proche de la date saisie.

J'ai écrit cette procédure

CREATE PROCEDURE dbo.tarif_liste
@date smalldatetime

a.mtar_id as MTAR_ID
,t.marc_id as marc_id
,convert(varchar(20),t.mtar_datedebut,103) as debut
,t.mtar_montant as nouveauMontant
,a.mtar_montant as ancienMontant


from TARIF t

inner join (select mtar_id, marc_id,mtar_montant from MTARIF where
cast(mtar_datedebut as smalldatetime) != cast(convert(varchar(10),@date,103)
as smalldatetime) order by mtar_id desc) a
on t.mtar_marcid=a.mtar_marcid

where cast(mtar_datedebut as
smalldatetime)=cast(convert(varchar(10),@date,103) as smalldatetime)

order by t.mtar_debut asc,t.marc_id asc


Mais cela me retourne deux fois le même tarif (le dernier). J'ai essayé avec
des TOP et des MAX mais je suis un peu perdu.

Si quelqu'un avait une idée.

Merci par avance

4 réponses

Avatar
Med Bouchenafa
Si tu pouvais fournir un DDL et quelques données, cela faciliterait l'aide

--
Bien Cordialement
Med Bouchenafa

"jerome" wrote in message
news:
Bonjour,

J'ai un problème d'écriture de requête.
J'ai une table MTARIF avec
MTAR_ID int ID PK autoincrement
MARC_ID int (id de la marchandise pour ce tarif)
MTAR_DATEDEBUT smalldatetime (date de début de validité du tarif)
MTAR_DATEFIN smalldatetime (date de finde validité du tarif)
MTAR_MONTANT decimal(18,2) valeur du tarif

les tarifs changent et peuvent être saisis à l'avance.

Je souhaiterais connaître pour une date donnée le tarif en cours et le
tarif
précédent. EN sachant qu'il peur y avoir plusieurs tarifs précédents et
que
je souhaite le dernier plus proche de la date saisie.

J'ai écrit cette procédure

CREATE PROCEDURE dbo.tarif_liste
@date smalldatetime

a.mtar_id as MTAR_ID
,t.marc_id as marc_id
,convert(varchar(20),t.mtar_datedebut,103) as debut
,t.mtar_montant as nouveauMontant
,a.mtar_montant as ancienMontant


from TARIF t

inner join (select mtar_id, marc_id,mtar_montant from MTARIF where
cast(mtar_datedebut as smalldatetime) !=
cast(convert(varchar(10),@date,103)
as smalldatetime) order by mtar_id desc) a
on t.mtar_marcid=a.mtar_marcid

where cast(mtar_datedebut as
smalldatetime)Êst(convert(varchar(10),@date,103) as smalldatetime)

order by t.mtar_debut asc,t.marc_id asc


Mais cela me retourne deux fois le même tarif (le dernier). J'ai essayé
avec
des TOP et des MAX mais je suis un peu perdu.

Si quelqu'un avait une idée.

Merci par avance




Avatar
jerome
J'espère que cela ira comme ça.

Merci par avance


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MTARIF](
[MTAR_ID] [int] IDENTITY(1,1) NOT NULL,
[MTAR_MARCID] [int] NOT NULL,
[MTAR_PRIXTARIF] [decimal](18, 2) NULL,
[MTAR_DEBUT] [datetime] NULL,
[MTAR_FIN] [datetime] NULL,
[MTAR_DATECREATION] [datetime] NULL CONSTRAINT [DF_MTARIF_MTAR_CREATION]
DEFAULT (getdate()),
[MTAR_MODIFICATION] [datetime] NULL,
CONSTRAINT [PK_MTARIF] PRIMARY KEY CLUSTERED
(
[MTAR_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (1,20,'01/01/2009','01/04/2009')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (2,10,'01/01/2009','01/01/2010')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (1,22,'02/04/2009','01/06/2009')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (1,25,'02/06/2009','01/01/2010')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (2,5,'01/01/2009','01/06/2009')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (2,8,'02/06/2009','01/01/2100')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (3,100,'01/01/2009','01/01/2100')
GO

"Med Bouchenafa" wrote in message
news:
Si tu pouvais fournir un DDL et quelques données, cela faciliterait l'aide

--
Bien Cordialement
Med Bouchenafa

"jerome" wrote in message
news:
> Bonjour,
>
> J'ai un problème d'écriture de requête.
> J'ai une table MTARIF avec
> MTAR_ID int ID PK autoincrement
> MARC_ID int (id de la marchandise pour ce tarif)
> MTAR_DATEDEBUT smalldatetime (date de début de validité du tarif)
> MTAR_DATEFIN smalldatetime (date de finde validité du tarif)
> MTAR_MONTANT decimal(18,2) valeur du tarif
>
> les tarifs changent et peuvent être saisis à l'avance.
>
> Je souhaiterais connaître pour une date donnée le tarif en cours et le
> tarif
> précédent. EN sachant qu'il peur y avoir plusieurs tarifs précédents et
> que
> je souhaite le dernier plus proche de la date saisie.
>
> J'ai écrit cette procédure
>
> CREATE PROCEDURE dbo.tarif_liste
> @date smalldatetime
>
> a.mtar_id as MTAR_ID
> ,t.marc_id as marc_id
> ,convert(varchar(20),t.mtar_datedebut,103) as debut
> ,t.mtar_montant as nouveauMontant
> ,a.mtar_montant as ancienMontant
>
>
> from TARIF t
>
> inner join (select mtar_id, marc_id,mtar_montant from MTARIF where
> cast(mtar_datedebut as smalldatetime) ! > > cast(convert(varchar(10),@date,103)
> as smalldatetime) order by mtar_id desc) a
> on t.mtar_marcid=a.mtar_marcid
>
> where cast(mtar_datedebut as
> smalldatetime)Êst(convert(varchar(10),@date,103) as smalldatetime)
>
> order by t.mtar_debut asc,t.marc_id asc
>
>
> Mais cela me retourne deux fois le même tarif (le dernier). J'ai essayé
> avec
> des TOP et des MAX mais je suis un peu perdu.
>
> Si quelqu'un avait une idée.
>
> Merci par avance
>
>



Avatar
Med Bouchenafa
DECLARE @DateTemoin DATETIME
SET @DateTemoin = '20090112'


;WITH PriceCTE AS
(
SELECT MTAR_ID,
MTAR_MARCID,
MTAR_DEBUT,
MTAR_FIN,
MTAR_PRIXTARIF,
rowNum = ROW_NUMBER() OVER (Partition BY MTAR_MARCID ORDER
BY MTAR_DEBUT)
FROM MTARIF

)

SELECT C.MTAR_ID,
C.MTAR_MARCID,
C.MTAR_DEBUT,
C.MTAR_FIN,
CurrentPrice = C.MTAR_PRIXTARIF,
PreviousPrice = P.MTAR_PRIXTARIF
FROM PriceCTE C LEFT JOIN PriceCTE P ON C.rowNum = P.rowNum + 1 AND
C.MTAR_MARCID = P.MTAR_MARCID
WHERE @DateTemoin BETWEEN C.MTAR_DEBUT AND C.MTAR_FIN



--
Bien Cordialement
Med Bouchenafa

"jerome" wrote in message
news:%
J'espère que cela ira comme ça.

Merci par avance


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MTARIF](
[MTAR_ID] [int] IDENTITY(1,1) NOT NULL,
[MTAR_MARCID] [int] NOT NULL,
[MTAR_PRIXTARIF] [decimal](18, 2) NULL,
[MTAR_DEBUT] [datetime] NULL,
[MTAR_FIN] [datetime] NULL,
[MTAR_DATECREATION] [datetime] NULL CONSTRAINT [DF_MTARIF_MTAR_CREATION]
DEFAULT (getdate()),
[MTAR_MODIFICATION] [datetime] NULL,
CONSTRAINT [PK_MTARIF] PRIMARY KEY CLUSTERED
(
[MTAR_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY > OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF


insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (1,20,'01/01/2009','01/04/2009')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (2,10,'01/01/2009','01/01/2010')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (1,22,'02/04/2009','01/06/2009')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (1,25,'02/06/2009','01/01/2010')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (2,5,'01/01/2009','01/06/2009')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (2,8,'02/06/2009','01/01/2100')
GO

insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
values (3,100,'01/01/2009','01/01/2100')
GO

"Med Bouchenafa" wrote in message
news:
Si tu pouvais fournir un DDL et quelques données, cela faciliterait
l'aide

--
Bien Cordialement
Med Bouchenafa

"jerome" wrote in message
news:
> Bonjour,
>
> J'ai un problème d'écriture de requête.
> J'ai une table MTARIF avec
> MTAR_ID int ID PK autoincrement
> MARC_ID int (id de la marchandise pour ce tarif)
> MTAR_DATEDEBUT smalldatetime (date de début de validité du tarif)
> MTAR_DATEFIN smalldatetime (date de finde validité du tarif)
> MTAR_MONTANT decimal(18,2) valeur du tarif
>
> les tarifs changent et peuvent être saisis à l'avance.
>
> Je souhaiterais connaître pour une date donnée le tarif en cours et le
> tarif
> précédent. EN sachant qu'il peur y avoir plusieurs tarifs précédents et
> que
> je souhaite le dernier plus proche de la date saisie.
>
> J'ai écrit cette procédure
>
> CREATE PROCEDURE dbo.tarif_liste
> @date smalldatetime
>
> a.mtar_id as MTAR_ID
> ,t.marc_id as marc_id
> ,convert(varchar(20),t.mtar_datedebut,103) as debut
> ,t.mtar_montant as nouveauMontant
> ,a.mtar_montant as ancienMontant
>
>
> from TARIF t
>
> inner join (select mtar_id, marc_id,mtar_montant from MTARIF where
> cast(mtar_datedebut as smalldatetime) ! >> > cast(convert(varchar(10),@date,103)
> as smalldatetime) order by mtar_id desc) a
> on t.mtar_marcid=a.mtar_marcid
>
> where cast(mtar_datedebut as
> smalldatetime)Êst(convert(varchar(10),@date,103) as smalldatetime)
>
> order by t.mtar_debut asc,t.marc_id asc
>
>
> Mais cela me retourne deux fois le même tarif (le dernier). J'ai essayé
> avec
> des TOP et des MAX mais je suis un peu perdu.
>
> Si quelqu'un avait une idée.
>
> Merci par avance
>
>







Avatar
jerome
Merci,

Je n'ai pas tout compris (je vais essayer) mais cela fonctionne..
Sauf le WHERE @DateTemoin BETWEEN C.MTAR_DEBUT AND C.MTAR_FIN
qui me renvoyait trop de lignes (MTAR_DEBUT plus petites que @DateTemoin )

Avec WHERE C.MTAR_DEBUT > @DateTemoin cela fonctionne.

Merci encore

"Med Bouchenafa" wrote in message
news:

DECLARE @DateTemoin DATETIME
SET @DateTemoin = '20090112'


;WITH PriceCTE AS
(
SELECT MTAR_ID,
MTAR_MARCID,
MTAR_DEBUT,
MTAR_FIN,
MTAR_PRIXTARIF,
rowNum = ROW_NUMBER() OVER (Partition BY MTAR_MARCID


ORDER
BY MTAR_DEBUT)
FROM MTARIF

)

SELECT C.MTAR_ID,
C.MTAR_MARCID,
C.MTAR_DEBUT,
C.MTAR_FIN,
CurrentPrice = C.MTAR_PRIXTARIF,
PreviousPrice = P.MTAR_PRIXTARIF
FROM PriceCTE C LEFT JOIN PriceCTE P ON C.rowNum = P.rowNum + 1 AND
C.MTAR_MARCID = P.MTAR_MARCID
WHERE @DateTemoin BETWEEN C.MTAR_DEBUT AND C.MTAR_FIN



--
Bien Cordialement
Med Bouchenafa

"jerome" wrote in message
news:%
> J'espère que cela ira comme ça.
>
> Merci par avance
>
>
> SET ANSI_NULLS ON
> GO
> SET QUOTED_IDENTIFIER ON
> GO
> SET ANSI_PADDING ON
> GO
> CREATE TABLE [dbo].[MTARIF](
> [MTAR_ID] [int] IDENTITY(1,1) NOT NULL,
> [MTAR_MARCID] [int] NOT NULL,
> [MTAR_PRIXTARIF] [decimal](18, 2) NULL,
> [MTAR_DEBUT] [datetime] NULL,
> [MTAR_FIN] [datetime] NULL,
> [MTAR_DATECREATION] [datetime] NULL CONSTRAINT [DF_MTARIF_MTAR_CREATION]
> DEFAULT (getdate()),
> [MTAR_MODIFICATION] [datetime] NULL,
> CONSTRAINT [PK_MTARIF] PRIMARY KEY CLUSTERED
> (
> [MTAR_ID] ASC
> )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY > > OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
> ) ON [PRIMARY]
>
> GO
> SET ANSI_PADDING OFF
>
>
> insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
> values (1,20,'01/01/2009','01/04/2009')
> GO
>
> insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
> values (2,10,'01/01/2009','01/01/2010')
> GO
>
> insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
> values (1,22,'02/04/2009','01/06/2009')
> GO
>
> insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
> values (1,25,'02/06/2009','01/01/2010')
> GO
>
> insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
> values (2,5,'01/01/2009','01/06/2009')
> GO
>
> insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
> values (2,8,'02/06/2009','01/01/2100')
> GO
>
> insert into mtarif (mtar_marcid,mtar_prixtarif,mtar_debut,mtar_fin)
> values (3,100,'01/01/2009','01/01/2100')
> GO
>
> "Med Bouchenafa" wrote in message
> news:
>> Si tu pouvais fournir un DDL et quelques données, cela faciliterait
>> l'aide
>>
>> --
>> Bien Cordialement
>> Med Bouchenafa
>>
>> "jerome" wrote in message
>> news:
>> > Bonjour,
>> >
>> > J'ai un problème d'écriture de requête.
>> > J'ai une table MTARIF avec
>> > MTAR_ID int ID PK autoincrement
>> > MARC_ID int (id de la marchandise pour ce tarif)
>> > MTAR_DATEDEBUT smalldatetime (date de début de validité du tarif)
>> > MTAR_DATEFIN smalldatetime (date de finde validité du tarif)
>> > MTAR_MONTANT decimal(18,2) valeur du tarif
>> >
>> > les tarifs changent et peuvent être saisis à l'avance.
>> >
>> > Je souhaiterais connaître pour une date donnée le tarif en cours et


le
>> > tarif
>> > précédent. EN sachant qu'il peur y avoir plusieurs tarifs précédents


et
>> > que
>> > je souhaite le dernier plus proche de la date saisie.
>> >
>> > J'ai écrit cette procédure
>> >
>> > CREATE PROCEDURE dbo.tarif_liste
>> > @date smalldatetime
>> >
>> > a.mtar_id as MTAR_ID
>> > ,t.marc_id as marc_id
>> > ,convert(varchar(20),t.mtar_datedebut,103) as debut
>> > ,t.mtar_montant as nouveauMontant
>> > ,a.mtar_montant as ancienMontant
>> >
>> >
>> > from TARIF t
>> >
>> > inner join (select mtar_id, marc_id,mtar_montant from MTARIF where
>> > cast(mtar_datedebut as smalldatetime) ! > >> > cast(convert(varchar(10),@date,103)
>> > as smalldatetime) order by mtar_id desc) a
>> > on t.mtar_marcid=a.mtar_marcid
>> >
>> > where cast(mtar_datedebut as
>> > smalldatetime)Êst(convert(varchar(10),@date,103) as smalldatetime)
>> >
>> > order by t.mtar_debut asc,t.marc_id asc
>> >
>> >
>> > Mais cela me retourne deux fois le même tarif (le dernier). J'ai


essayé
>> > avec
>> > des TOP et des MAX mais je suis un peu perdu.
>> >
>> > Si quelqu'un avait une idée.
>> >
>> > Merci par avance
>> >
>> >
>>
>
>