
/***************************************
* STORED stp_ParticolariOverview
*
* restituisce solo la riga con il num max di celle
*
* Steamware, S.E.L.
* mod: 2010.06.02
*
****************************************/
CREATE PROCEDURE [dbo].[stp_magazzinoOverview_maxCelle]
(
	@CodCS VARCHAR(2)
)
AS

WITH cte_celle AS(
	SELECT     b.CodBlocco, c.IdxTipoCella, COUNT(c.IdxCella) * t.Capienza AS numCelle
	FROM         Celle AS c INNER JOIN
						  Blocchi AS b ON c.IdxBlocco = b.IdxBlocco INNER JOIN
						  TipoCella AS t ON c.IdxTipoCella = t.IdxTipoCella
	WHERE     (b.CodCS = @CodCS)
	GROUP BY b.CodBlocco, c.IdxTipoCella, t.Capienza
)


	SELECT		TOP (1) AnagMag.CodCS, AnagMag.CodMag, AnagMag.DescMag, b.IdxBlocco, b.CodBlocco, b.DescBlocco,
				ISNULL(c.numCelle,ISNULL(b.NumX * b.NumY * b.NumZ,1)) AS TotCelle, 
	            0 AS CelleAttive, 0 AS CelleOccupate, 0 AS CelleLibere, GETDATE() AS Oldest, GETDATE() AS Newest
	FROM        AnagMag INNER JOIN Blocchi b ON AnagMag.CodMag = b.CodMag AND AnagMag.CodCS = b.CodCS INNER JOIN cte_celle c ON c.CodBlocco = b.CodBlocco
	WHERE       (AnagMag.CodCS = @CodCS)
	ORDER BY	TotCelle DESC

RETURN
