IF OBJECT_ID('V_PARCIAL_TOP_LOJAS') IS NOT NULL
	DROP VIEW V_PARCIAL_TOP_LOJAS
GO
CREATE VIEW V_PARCIAL_TOP_LOJAS
AS
SELECT	TOP 10
        CODIGO,DESCRICAO,SUM(QTDE) AS QTDE,SUM(TOTAL) AS TOTAL,ROUND(SUM(TOTAL) / SUM(QTDE),2) AS TICKET
FROM
(
SELECT	
		V.CD_FILIAL AS CODIGO,F.NM_FANT AS DESCRICAO,COUNT(0) AS QTDE,SUM(VLR_LIQ_VD) AS TOTAL
FROM	PDV_VD V
INNER	JOIN PRC_FILIAL F ON V.CD_EMP = F.CD_EMP AND V.CD_FILIAL = F.CD_FILIAL
WHERE	V.DT_VD = CONVERT(VARCHAR,GETDATE(),112) AND ST_VD = 0
GROUP	BY V.CD_FILIAL,F.NM_FANT
UNION
SELECT	
		V.CD_FILIAL AS CODIGO,F.NM_FANT AS DESCRICAO,COUNT(0) AS QTDE,SUM(VLR_TOT_CUPOM) AS TOTAL
FROM	PDV_CUPOM_NAO_FISCAL V
INNER	JOIN PRC_FILIAL F ON V.CD_EMP = F.CD_EMP AND V.CD_FILIAL = F.CD_FILIAL
WHERE	V.DT_CUPOM = CONVERT(VARCHAR,GETDATE(),112) AND TP_CUPOM_NAO_FISCAL = 3
GROUP	BY V.CD_FILIAL,F.NM_FANT
)
		TMP
GROUP	BY CODIGO,DESCRICAO
ORDER	BY TOTAL DESC