How can I implement rank over division operation in cakephp query builder for 2 column? - cakephp

I am trying to get rank over from order by 2 column on division result. Example I have total get_count and total members. I am trying to rank it by team_members/team_get_count ASC order.
->select([
'team_members' => $query->func()->count('TeamsUsers.user_id'),
'team_get_count' => $this->find()->func()->coalesce([$query->func()->sum('SummaryCoins.get_count'), 0],[null, 'integer']),
'ranking'=> RANK() OVER(ORDER BY (coalesce(SUM(SummaryCoins.get_count),0)/coalesce(COUNT(TeamsUsers.user_id),1)) DESC), <--- Trying to implement this line using cakephp.
])

Related

I want to format 2nd column in the query to return numerical values with 2 decimals places. The problem is all the rows return: {100.00,100.00,100.00}

This is the snippet of code. It runs well, the issue is it returns 100.00 for each value and not actual decimal figures like say 68.73 for example
select
array(select products.product_category_name from brooklyndata.olist_products_dataset order by orders_and_revenue.revenue desc limit 3) as top_three_product_categories_by_revenue
, array(select round(cast(sum(orders_and_revenue.revenue) over (partition by products.product_category_name) as decimal)/round(cast(sum(orders_and_revenue.revenue) over () as decimal)) * 100,2) from brooklyndata.olist_products_dataset order by orders_and_revenue.revenue desc limit 3 )
, orders_and_revenue.revenue
from brooklyndata.olist_products_dataset as products

Trying to use dense rank ,Getting error in ranks using dense rank

I am trying to get the unique rank for every unique value in column a:
-------------------
column a column b
-------------------
1234. 1
6353. 5
3636. 4
7266. 6
2772. 3
2663. 2
2663. 2
I want something like this. But getting higher number of values in column b it’s counting extra. Please help me how to ignore any discrepancies while doing this.
As of now I am using this:
Dense_rank() over ( order by column a ) as abc
You probably need ROW_NUMBER() with PARTITION BY:
SELECT
ColumnA,
ROW_NUMBER() OVER (PARTITION BY ColumnA ORDER BY (SELECT NULL)) AS ColumnB
FROM (VALUES
('1234'),
('6353'),
('3636'),
('7266'),
('2772'),
('2663'),
('2663')
) v (ColumnA)

ranking within MDX

I have this MDX Query:
WITH
MEMBER [COUNT_RANK] AS
RANK(([TKT].[SP].CURRENTMEMBER,
[TKT].[SA].CURRENTMEMBER) ,
[TKT].[SP].CURRENTMEMBER
*[TKT].[SA].[SA]
)
SELECT {
[COUNT_RANK],
[Measures].[TKT Count],
[Measures].[Est Hours]
} ON 0,
ORDER ({[TKT].[SP].[SP]} * {[TKT].[SA].[SA]}, [Measures].[TKT Count], DESC)
ON 1
FROM Ops
the issue I have is that While the COUNT_RANK works and provides a 1-to-n value of ranking per SP for each SA, I need the order of the rank based on TKT Count desc. Meaning for rank = 1, then that SP*SA must have the highest number of TKTs.
Right now the result is random TKT Counts for the RANKING. how do I make the RANK go based on TKT Count DESC?
This is for SQL Server 2016 SSAS. Thanks.
From the MSDN Docs, you should create an ordered set before you declare the RANK() member, and use RANK function on the ordered set.
Here's their example:
WITH
SET OrderedCities AS Order
([Geography].[City].[City].members
, [Measures].[Reseller Sales Amount], BDESC
)
MEMBER [Measures].[City Rank] AS Rank
([Geography].[City].CurrentMember, OrderedCities)
SELECT {[Measures].[City Rank],[Measures].[Reseller Sales Amount]} ON 0
,Order
([Geography].[City].[City].MEMBERS
,[City Rank], ASC)
ON 1
FROM [Adventure Works]

SQL Case Then: remember and evaluate based on last group result

Example: I have the following case then statement (see here SQL: Order by column, then by substring mix asc and desc ) my warehouse and the locations are in 1 column as follows and the case alternates the rows going from ASC (odd) to DESC (even):
select *
from #temp
order
by substring(id,1,2),
case
when substring(id,1,2)%2=0 then row_number() over (partition by substring(id,1,2) order by SUBSTRING(id,4,3) desc)
else row_number() over (partition by substring(id,1,2) order by SUBSTRING(id,4,3) asc)
end
01-001-A-01
01-002-A-02
01-003-A-03
01-004-A-01
01-005-A-03
02-001-A-01
02-002-A-02
02-003-A-03
02-004-A-01
02-005-A-03
03-001-A-01
03-002-A-02
03-003-A-03
03-004-A-01
03-005-A-03
Now I would like to add the following: I pick an order from row 1 but nothing to pick from row 2 so I want to go to row 3, now I don't want to walk back the aisle to the beginning of row 03-01 because I'm close to 03-05, so I would like that my results should always be alternating between ASC and desc, so after 01-005-A-03 if I have 0 results with 02 then I want 03-005-A-03 meaning that in this case I would like row 3 DESC and row 4 ASC (so always do the opposite than in the previous group?
This is how it should be if no result begins in 02-XXX-X-XX
01-001-A-01
01-002-A-02
01-003-A-03
01-004-A-01
01-005-A-03
03-005-A-01
03-004-A-02
03-003-A-03
03-002-A-01
03-001-A-03
change the ORDER BY clause to
order by
substring(id,1,2),
case
when dense_rank() over (order by substring(id,1,2)) % 2 = 0
then row_number() over (partition by substring(id,1,2) order by SUBSTRING(id,4,3) desc)
else row_number() over (partition by substring(id,1,2) order by SUBSTRING(id,4,3) asc)
end
instead of finding modulo of substring(id,1,2) % 2 , use dense_rank() to get the continuous numbering and then find the modulo of it
Note : the original query would failed if your first segment is not pure numeric

How to reset DENSE_RANK

I have the following table
Id,Cat1,Cat2,ColA,ColB,
1,1,1,,
2,1,1,,
3,1,2,,
4,1,3,,
5,2,11,,
6,2,12,,
7,2,12,,
I need to add a unique sequential number per grouping column of Cat2 starting at 1 BUT it has to reset back to 1 when Cat1 changes
My output should be
Row, Id,Cat1,Cat2,ColA,ColB,
1,1,1,1,,
1,2,1,1,,
2,3,1,2,,
3,4,1,3,,
1,5,2,11,,
2,6,2,12,,
2,7,2,12,,
DENSE_RANK() works to give a unique value e.g.
Row= DENSE_RANK() OVER ( ORDER BY Cat2)
but I need it to reset itself based of another column Cat1
Use the PARTITION BY clause on Cat1:
Row = DENSE_RANK() OVER ( PARTITION BY Cat1 ORDER BY Cat2)
The PARTITION BY clause will partition your DENSE_RANK() first based on your Cat1 before ranking them densely, which is exactly what you need.

Resources