Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Top 50% in a Group
Message
De
12/12/2008 08:37:33
Moacyr Zalcman
Independent Consultant
São Paulo, Brésil
 
 
À
Tous
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Top 50% in a Group
Divers
Thread ID:
01366903
Message ID:
01366903
Vues:
43
Hi

I have the following query that returns the top 10 products in each category basead on the revenue of the product. But I'd like to have the top 50% of the number of the products in each category. Any Ideas?
SET @lddataini='20081121 00:00:00'
SET @lddatafim='20081127 23:59:59';

with ReceitaporProduto(ProdID,Revenue,AmountSold) as
	(
	SELECT PRODID,SUM(TOTAL) as revenue,SUM(ri.qtd) asAmountSold from romaneioitens ri inner join romaneio r on r.id=ri.romaneioID where r.caddate BETWEEN @lddataini and @lddatafim GROUP BY prodid 
	)

select HRRanks.* from
(
select prodId,categoryID,unestoque,Revenue,AmountSold,
rank() over (partition by p.categoryID order by RP.revenue desc) as HR_Rank
 from ReceitaporProduto RP
inner join produto p on p.id=RP.prodid
inner join estoquefisico e on e.id=p.id
where e.ativo=1
) HRRanks
where HR_Rank<=10 order by categoryID,HR_Rank
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform