Ich versuche, einige DATEDIFF-Berechnungen durchzuführen, aber mir fehlt einfach das Wissen, um die Aufgabe abzuschließen.Datum erhalten, wenn der Preis geändert wurde
Ich habe eine Tabelle, die Aufzeichnungen der Artikel mit Datum verkauft, als der Vorgang stattfand und historische Preise des Artikels. Da ich in einem Land mit 30% jährlicher Inflation lebe. Ich muss diesen Faktor berücksichtigen, um das Einkommen für jeden verkauften Artikel zu berechnen.
Alles funktioniert gut, während ich den Kaufpreis gespeichert habe und das Datum, an dem eine Produktpreisänderung stattfand. Aber wenn diese Daten nicht als Näherung gespeichert wurden, würde ich gerne das Datum verwenden, an dem die Operation mit einem anderen Preis stattfand. Als Ergebnis der folgenden Auswahloperationen:
USE Rusich
DECLARE
@IDNegocio AS INT,
@UsarIDProducto AS BIT,
@IDProducto AS VARCHAR(50),
@FechaInicio AS DATE,
@FechaFinal AS DATE,
@TamañoMinimoDeMuestra AS INT,
@SeVendeMinimoCadaXDias AS INT,
@Inflacion AS DECIMAL(18,3),
@AuxMargen AS DECIMAL(18,2),
@AuxDifDias AS INT;
SET @FechaInicio = '01/02/10';
SET @FechaFinal = '28/02/18';
SET @IDNegocio = 1;
SET @IDProducto = '6910101404918'; /*<<<< Cambiar a NULL segun necesidad*/
SET @TamañoMinimoDeMuestra = 1;
SET @SeVendeMinimoCadaXDias = 90;
SET @Inflacion = 0.083;
SELECT
AUXFechas.IDProducto,
AUXFechas.[$ Precio de Venta Historico],
AUXFechas.[Fecha Precio],
AUXFechas.[Fecha Venta],
AUXFechas.FechaAnterior,
AUXFechas.DiasDiferencia,
CAST(
CASE WHEN
AUXFechas.IDProducto NOT IN ('112', '113' , '114')
AND AUXFechas.[Fecha Precio] <= AUXFechas.[Fecha Venta]
AND StockDetalles.[Precio de Compra] <> 0
AND StockDetalles.[Precio de Compra] IS NOT NULL THEN
(1 - StockDetalles.[Precio de Compra]/AUXFechas.[$ Precio de Venta Historico]) * 100 - (AUXFechas.[$ Descuento]/100) - DATEDIFF(DAY, AUXFechas.[Fecha Precio], AUXFechas.[Fecha Venta]) * @Inflacion
ELSE
NULL
END
AS DECIMAL(18,2)) AS [% Margen Ganancia Real]
FROM
StockDetalles JOIN (
SELECT
Stock.ID AS IDProducto,
Stock.Cantidad AS #Disponible,
Recibos.Cantidad as [#Vendida],
Recibos.Precio AS [$ Precio de Venta Historico],
Recibos.Descuento AS [$ Descuento],
Stock.[Precio de Venta] AS [$ Precio],
Stock.[Fecha Actualizacion de Precio] AS [Fecha Precio],
CAST(RecibosRegistros.Fecha AS DATE) AS [Fecha Venta],
ISNULL(LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha), Stock.[Fecha Actualizacion de Precio]) AS FechaAnterior,
/*Considera el caso cuando no existe un registro anterior y usa la fecha de actualizacion de precio si es posible*/
CASE WHEN (LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha) IS NULL) AND CAST(RecibosRegistros.Fecha AS DATE) > Stock.[Fecha Actualizacion de Precio] THEN
(DATEDIFF(DAY, CAST(RecibosRegistros.Fecha AS DATE),
/*PARTITION BY PARTICIONA POR COLUMNA*/
ISNULL(LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha), [Fecha Actualizacion de Precio]))/Recibos.Cantidad) * -1
ELSE
(DATEDIFF(DAY, CAST(RecibosRegistros.Fecha AS DATE),
/*PARTITION BY PARTICIONA POR COLUMNA*/
LAG(CAST(RecibosRegistros.Fecha AS DATE), 1) OVER (PARTITION BY Recibos.IDProducto ORDER BY RecibosRegistros.Fecha))/Recibos.Cantidad) * -1
END AS DiasDiferencia
FROM
RecibosRegistros
JOIN Recibos ON RecibosRegistros.IDRecibo = Recibos.IDRecibo
JOIN Stock ON Recibos.IDProducto = Stock.ID
WHERE
RecibosRegistros.NegocioID = @IDNegocio
AND Stock.IDNegocio = @IDNegocio
AND Stock.ID != '111'
AND Stock.ID != '112'
AND Stock.ID != '113'
AND Stock.ID != '114'
AND Stock.ID = COALESCE(@IDProducto,Stock.ID)) AS AUXFechas ON StockDetalles.ID = AUXFechas.IDProducto
JOIN Categorias ON StockDetalles.CategoriaID = Categorias.ID
Im Erhalt dieses Ergebnisses:
IDProducto $ Precio de Venta Historico Fecha Precio Fecha Venta FechaAnterior DiasDiferencia % Margen Ganancia Real
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6910101404918 15,00 2012-10-18 2012-07-23 2012-10-18 NULL NULL
6910101404918 15,00 2012-10-18 2012-09-01 2012-07-23 40 NULL
6910101404918 21,00 2012-10-18 2013-07-01 2012-09-01 303 NULL
6910101404918 21,00 2012-10-18 2013-09-02 2013-07-01 63 NULL
6910101404918 21,00 2012-10-18 2013-09-24 2013-09-02 22 NULL
0200002003867 28,00 2014-03-05 2014-07-08 2014-03-05 125 NULL
0200002003867 28,00 2014-03-05 2014-07-08 2014-07-08 0 NULL
0200002003867 28,00 2014-03-05 2014-10-23 2014-07-08 107 NULL
0200002003867 28,00 2014-03-05 2015-01-21 2014-10-23 90 NULL
0200002003867 28,00 2014-03-05 2015-04-06 2015-01-21 75 NULL
Was ich tun muß, ist eine Spalte mit Datum (Fecha Preis), wo [$ Preis von Venta Historico bekommen ] geändert.
Ausgang sollte sein:
IDProducto $ Precio de Venta Historico Fecha Precio Fecha Venta FechaAnterior DiasDiferencia % Margen Ganancia Real Fecha Cambio
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6910101404918 15,00 2012-10-18 2012-07-23 2012-10-18 NULL NULL NULL
6910101404918 15,00 2012-10-18 2012-09-01 2012-07-23 40 NULL 2012-10-18
6910101404918 21,00 2012-10-18 2013-07-01 2012-09-01 303 NULL NULL
6910101404918 21,00 2012-10-18 2013-09-02 2013-07-01 63 NULL 2012-09-01
6910101404918 21,00 2012-10-18 2013-09-24 2013-09-02 22 NULL 2012-09-01
0200002003867 28,00 2014-03-05 2014-07-08 2014-03-05 125 NULL NULL
0200002003867 28,00 2014-03-05 2014-07-08 2014-07-08 0 NULL 2014-07-08
0200002003867 28,00 2014-03-05 2014-10-23 2014-07-08 107 NULL 2014-07-08
0200002003867 28,00 2014-03-05 2015-01-21 2014-10-23 90 NULL 2014-07-08
0200002003867 28,00 2014-03-05 2015-04-06 2015-01-21 75 NULL 2014-07-08
Meine Vermutung ist, dass die Lösung mit LAG/LEAD und PartitionBY IDProducto in Beziehung gesetzt werden müssen, aber ich weiß nicht, wie es zu tun. Bitte Hilfe
Berechnung von 30% Inflation - das ist eine einzigartige Geschäftsregel !! Wenn ich richtig verstanden habe, haben Sie den aktuellen Ausgang und die benötigte Ausgabe gepostet, aber nicht die Eingabe - ist das richtig? Es könnte helfen, die Eingabe zu posten. Außerdem könnte es das Entfernen nicht benötigter Spalten aus den Beispieldatensätzen erleichtern. Es gibt in SQL 2008 keinen Vorlauf/Nachlauf. Die Art und Weise, wie dies erreicht wird, ist für _every_ transaction, dass Sie die letzte gültige Transaktion bestimmen, sich ihr anschließen und _optional_ sie verwenden. –