Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Top 50% in a Group
Message
 
 
To
12/12/2008 08:37:33
Moacyr Zalcman
Independent Consultant
São Paulo, Brazil
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01366903
Message ID:
01366935
Views:
8
You need to get counts of products per each category and just get the half of it using Row_Number() function and something like

select ... Row_Number() OVER (ORDER BY Revenue DESC) as RowNumber where RowNumber < ...

See my changes inside your code.

>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, nProducts_Count) as
>	(
>	SELECT PRODID,SUM(TOTAL) as revenue,SUM(ri.qtd) asAmountSold, Count(*) as nProducts_Count 
                        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, Row_Number() OVER (partition by p.categoryID order by RP.revenue desc) as Rn,
>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 RN < cast(RP.nProducts_Count / 50 as int) order by categoryID,HR_Rank
>
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform