
/***************************************
* STORED stp_paretoDettMagByParticolare
*
* restituisce elenco giacenze a magazzino in logica pareto (quantità) per un dato particolare
*
* Steamware, S.E.L.
* mod: 2010.06.04
*
****************************************/
CREATE PROCEDURE [dbo].[stp_paretoDettMagByParticolare]
(
	@CodCS VARCHAR(2),
	@Particolare VARCHAR(20)
)
AS

	SELECT	TOP (100) PERCENT am.CodCS, am.CodMag, am.DescMag, b.IdxBlocco, b.CodBlocco, ec.Particolare, 
				ec.Esponente, COUNT(puc.UDC) AS NumUDC, ISNULL(MIN(puc.DataRif), 
				GETDATE()) AS Oldest, ISNULL(MAX(puc.DataRif), GETDATE()) AS Newest, SUM(ec.Qta) AS QtaTotPz, 
				asp.DescStato, ec.IdxPosizione AS CodMagLog
	FROM		AnagMag am INNER JOIN
				Blocchi b ON am.CodMag = b.CodMag AND am.CodCS = b.CodCS INNER JOIN
				Celle c INNER JOIN
				ElencoCartellini ec LEFT OUTER JOIN
				PosizioneUdcCorrente puc ON ec.UDC = puc.UDC ON c.IdxCella = puc.IdxCella ON 
				b.IdxBlocco = c.IdxBlocco INNER JOIN
				AnagStatiProdotto asp ON ec.CodStato = asp.CodStato
	WHERE		(c.Attiva = 1) AND (am.CodCS = @CodCS) AND (ec.Particolare = @Particolare)
	GROUP BY	am.CodCS, am.CodMag, am.DescMag, b.IdxBlocco, b.CodBlocco, b.NumX, b.NumY, b.NumZ, 
	            ec.Particolare, ec.Esponente, ec.IdxPosizione, asp.DescStato
	ORDER BY	QtaTotPz DESC

	
RETURN

