How to get maximum continuous iteration - sql

I have a table like below and I need to get maximum continuous iterations for the individual drink.
The output should be like this:

You can try the below - DEMO Here
with cte as (
select drink,count(*) as cnt
from
(
select *,row_number() over(order by queue) -
row_number() over(partition by drink order by queue) as grp
from t
)A group by drink,grp )
select drink, max(cnt) from cte group by drink

Related

Max and Min row numbers from single cte code block

;with cte1 as
( select id,
row_number() over (partition by pk,pp,sn order by id asc)
as rn
from mqms_production
) select * into #M from cte1 where rn=1
With the above, I get all the rows with rn=1 but I also want to copy to another table all the rows with max rn for a given partition pk,pp, sn.
Is it possibleto do it without having to write the cte block again with
(partition by pk,pp,sn order by id DESC)
thanks!
You can just add another window function based expression there with reverse sort order and get the top rows on both of them
with cte1 as (
select id,
row_number() over (partition by pk,pp,sn order by id asc) as rn1,
row_number() over (partition by pk,pp,sn order by id desc) as rn2
from mqms_production
)
select * from cte1
where rn1 = 1 or rn2 = 1;

Display the top two most popular activities across all reservations

The Table is
SUPERVISION (ResNo, ActivityID, SupervisorID, Day, Time)
I have done something like this but it is wrong
SELECT COUNT(S.Res, S.ActivityID) AS PopularActivities
FROM Supervision S
WHERE rownum = 2;
ORDER BY COUNT(*) DESC;
or
SELECT S.ResNo, S.ActivityID
FROM Supervision S
WHERE (rank() over (order by count(*) DESC) as RNK
from Supervision S) AND rnk = 2;
You need to put it in a subquery:
SELECT *
FROM (
SELECT
ActivityID, COUNT(*) AS Cnt
FROM Supervision
GROUP BY ActivityID
ORDER BY Cnt DESC
) t
WHERE rownum <= 2
Alternatively, you can use RANK to achieve the same result:
SELECT *
FROM (
SELECT
ActivityID,
RANK() OVER(PARTITION BY ActivityID ORDER BY COUNT(*) DESC) AS rnk
FROM Supervision
GROUP BY ActivityID
) t
WHERE rnk <= 2
Try this
SELECT *
FROM (
SELECT
ActivityID, COUNT(*) AS top
FROM Supervision
GROUP BY ActivityID
ORDER BY top DESC
) t
limit 0,2
For me, what I did is this:
ORDER BY TIME DESC
You want the two most popular activities across all reservations.
This means that you want to count the reservations of each activity type, and take the two with the highest count.
Another way to say it is, you want to group the reservations by activity, count them, order the result decrescently, and take the first two
It looks like you are using Microsoft TSQL syntax, so it should look like this:
SELECT TOP 2 ActivityID, COUNT(*)
FROM SUPERVISION
GROUP BY ActivityID
ORDER BY COUNT(*) DESC

ROW_NUMBER and Grouping of data

I have a data that looks like this:
What I want is to have a row number that is group by GroupCode,Group Description,SubGroup and subgroup class and I want to retain the ordering by account code that will look like this:
What's the proper way of seting a row number at the same time grouping them?
You are looking for dense_rank():
select dense_rank() over (order by GroupCode, GroupDescription, SubGroup)
. . .
However, this doesn't guarantee the ordering by accountCode. That will require more work. First, determine the minimum account code for each grouping, then use dense_rank() on that:
select t.*, dense_rank() over (order by minac)
from (select t.*,
min(accountCode) over (partition by GroupCode, GroupDescription, SubGroup) as minac
from t
) t
You're looking for the DENSE_RANK window function:
SELECT
rn = DENSE_RANK() OVER(ORDER BY GroupCode, GroupDescription, SubgroupClass),
*
FROM tbl
ORDER BY rn, AccountCode
I Guess you need this
;WITH cte
AS (SELECT groupcode,
groupdescription,
subgroup,
subgroupclass,
Min(accountcode) AS accountcode
FROM your_table
GROUP BY groupcode,
groupdescription,
subgroup,
subgroupclass),
ordr
AS (SELECT Row_number()OVER(ORDER BY accountcode) AS RN,
*
FROM cte)
SELECT C.rn,
A.*
FROM your_table A
INNER JOIN cte C
ON A.groupcode = C.groupcode
AND A.groupdescription = C.groupdescription
AND A.subgroup = C.subgroup
AND A.subgroupclass = C.subgroupclass

To get the only 5th record from a table in ORACLE

I have a table named Stud in which I have a column to store the total. Now I need to find the 5th largest total in total column. How to perform this operation?
Try this:
SELECT total FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY total DESC) as RN, total FROM Stud
) T
WHERE RN=5
Select total
From
(SELECT total,
row_number() over(order by total desc) as rn
From totalTable
)Z
Where rn=5
Also can be done using ROWNUM pseudocolumn
Select total
From
(SELECT total
FROM totalTable
ORDER BY total desc
)Z
Where ROWNUM=5

Grab top 5 rows and combine the rest SQL Server

I have a column for group name and a column for amount spent.
I need to sum the amounts group them based on the group name and then grab the highest five. After that, I need to combine the the rest into it's own group w/ a total of their amount spent. This is what i have right now
SELECT groupName, SUM(amount) AS theAmountSpent
FROM purchases
GROUP BY groupName
ORDER BY theAmountSpent DESC
This groups and orders them, but i dont know how to then grab the remaining groups to combine them. Any help would be appreciated.
Alternate CTE-approach using row_number() (SQL Server 2005+):
WITH cte AS (
SELECT ROW_NUMBER() OVER (ORDER BY (SUM(amount)) DESC) AS num,
groupName, SUM(amount) AS theAmountSpent
FROM purchases
GROUP BY groupName
)
SELECT groupName, theAmountSpent FROM cte WHERE num BETWEEN 1 AND 5 --top 5
UNION ALL
SELECT 'Sum rest', SUM(theAmountSpent) FROM cte WHERE num > 5 -- sum of rest
If I'm understanding you correctly, this should do it:
SELECT top 5 groupName, SUM(amount) AS theAmountSpent
into #tempSpent FROM purchases
GROUP BY groupName
ORDER BY theAmountSpent DESC
Select * from #tempSpent -- get the top 5
--get sum for the rest
SELECT SUM(amount) AS theAmountSpent
FROM purchases
where groupName not in (select groupName from #tempSpent)
Drop table #tempSpent
Another idea from Larsts code:
WITH cte
AS
(
SELECT case
when ROW_NUMBER() OVER (ORDER BY (SUM(amount)) DESC) <=5
then ROW_NUMBER() OVER (ORDER BY (SUM(amount)) DESC)
else 6 end AS num
, groupName
, SUM(amount) AS theAmountSpent
FROM purchases
GROUP BY groupName
)
SELECT num
, max(groupName)
, sum(theAmountSpent )
FROM cte
group by num