Display the top two most popular activities across all reservations - sql

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

Related

Just another SQL case (GROUP BY)

I'm stuck on an SQL problem that I don't know how to solve.
Let's say I have a table like this (concerning estimations on house prices):
estimationID | estimationDate | userID | cityID
1 | '2020-01-01' | 123456 | 987654
2 | '2020-12-01' | 135790 | 975310
...
With estimationDate being the date when the estimation was made, userID the ID of the user who made the estimation and cityID the ID of the city where the estimation was made.
I need to get the maximum number of estimations made by one user (I don't care which one, I don't need an ID) for each city.
Something like
SELECT cityID,*maximum number of estimations made by one user from this city* FROM estimationsTable GROUP BY cityID
Any idea?
Step by step:
Get the number of estimations per user and city.
Get the maximum of these numbers per city.
The query:
select cityid, max(cnt)
from
(
select cityid, userid, count(*) as cnt
from estimationstable
group by cityid, userid
) counted
group by cityid
order by cityid;
try like below
with cte as (
select userid,cityid,count(*) as cnt
from table_name group by userid,cityid
)
, cte2 as (
select *,
row_number() over(partition by cityid order by cnt desc) rn
from cte
) select * from cte2 where rn=1
sol 1:
SELECT id, MAX(maximum_number_of_estimations)
FROM (SELECT id,COUNT(*) AS maximum_number_of_estimations
FROM TABLE x)group by id as final_query
sol2:
use order by Count DESC with group by`
something like this should work
the idea is you count all the occurrences in the inner query with the group by on your id and another query to get the max of it OR you use ORDER BY [Field] DESC
with GROUP BY which will automatically put the highest ones on the top
In BigQuery, I think you can do this without a subquery:
select distinct cityid,
(array_agg(userid order by count(*) desc, userid))[ordinal(1)] as userid,
max(count(*)) over (order by count(*) desc) as cnt
from estimationstable
group by cityid, userid

Filter out null values resulting from window function lag() in SQL query

Example query:
SELECT *,
lag(sum(sales), 1) OVER(PARTITION BY department
ORDER BY date ASC) AS end_date_sales
FROM revenue
GROUP BY department, date;
I want to show only the rows where end_date is not NULL.
Is there a clause used specifically for these cases? WHERE or HAVING does not allow aggregate or window function cases.
One method uses a subquery:
SELECT r.*
FROM (SELECT r. *,
LAG(sum(sales), 1) OVER (ORDER BY date ASC) AS end_date
FROM revenue r
) r
WHERE end_date IS NOT NULL;
That said, I don't think the query is correct as you have written it. I would assume that you want something like this:
SELECT r.*
FROM (SELECT r. *,
LEAD(end_date, 1) OVER (PARTITION BY ? ORDER BY date ASC) AS end_date
FROM revenue r
) r
WHERE end_date IS NOT NULL;
Where ? is a column such as the customer id.
Try this
select * from (select distinct *,SUM(sales) OVER (PARTITION BY dept) from test)t
where t.date in(select max(date) from test group by dept)
order by date,dept;
And one more simpler way without sub query
SELECT distinct dept,MAX(date) OVER (PARTITION BY dept),
SUM(sales) OVER (PARTITION BY dept)
FROM test;

How to find most frequent value in SQL column and return that value?

I was trying to do something like this:
select nume_produs
from incasari
group by id
having count(nume_produs) = max(count(nume_produs));
but it doesn't work
Do a GROUP BY. Order by count descending. Fetch the first row (highest count) only.
select nume_produs, count(*) as cnt
from incasari
group by nume_produs
order by cnt desc
fetch first 1 row with ties
For the most common value in the column:
select num_produs
from (select nume_produs, count(*) as cnt,
row_number() over (order by count(*)) as seqnum
from incasari
group by nume_produs
) i
where seqnum = 1;
If you want multiple values in the event of duplicates, use rank() instead of row_number().
If you want the most common value per id, then add partition by:
select num_produs
from (select nume_produs, count(*) as cnt,
row_number() over (partition by id order by count(*)) as seqnum
from incasari
group by nume_produs
) i
where seqnum = 1;
SELECT `nume_produs`,
COUNT(`nume_produs`) AS `value_occurrence`
FROM `incasari`
GROUP BY `nume_produs`
ORDER BY `value_occurrence` DESC
LIMIT 1;
Increase 1 if you want to see the N most common values of the column.

SQL return all fields from max of count

I have tried but just cannot get the results so I'm asking for help here. I have a table of logins for users on computers. I want to count the number of times a user has logged into a computer and then get the max so that max user is considered the primary user of the computer.
Depending on what dbms you are using, you can use a window function ROW_NUMBER() to rank the counts per comp. Then pull the highest ranking.
SELECT
Comp,
[User],
Cnt
FROM
(SELECT
Comp,
[User],
COUNT(1) AS Cnt,
ROW_NUMBER() OVER (PARTITION BY Comp ORDER BY Count(1) DESC) AS Rnk
FROM UserHist
GROUP BY Comp, [User]) BaseTable
WHERE Rnk = 1
You can RANK the counts and then select the largest. This uses a CTE to get the data with the count and the rank.
WITH CTECOMP AS
(
SELECT COMP, [user], COUNT([USER]) [USERCOUNT],
RANK() OVER (PARTITION BY COMP ORDER BY COUNT([USER]) DESC ) [MYRANK]
FROM #TableData
GROUP BY comp, [user]
)
SELECT *
FROM CTECOMP
WHERE MYRANK = 1
select Comp, [User], count([User]) as MaxCnt
from TableHere
group by Comp, [User]

How to write a derived query in Netezza SQL?

I need to query the data for inviteid based. For each inviteid I need to have the top 5 IDs and ID Descriptions.
I see that the query I wrote is taking all the time in the world to fetch. I didn't notice an error or anything wrong with it.
The code is:
SELECT count(distinct ID),
IDdesc,
inviteid,
A
FROM (
SELECT
ID,
IDdesc,
inviteid,
RANK() OVER(order by invtypeid asc ) A
FROM Fact_s
--WHERE dateid ='26012013'
GROUP BY invteid,IDdesc,ID
ORDER BY invteid,IDdesc,ID
) B
WHERE A <=5
GROUP BY A, IDDESC, inviteid
ORDER BY A
I'm not sure I understood you requirement completely, but as far as I can tell the group by in the derived table is not necessary (just as the order by as Mark mentioned) because you are using a window function.
And you probably want row_number() instead of rank() in there.
Including the result of rank() in the outer query seems dubious as well.
So this leads to the following statement:
SELECT count(distinct ID),
IDdesc,
inviteid
FROM (
SELECT ID,
IDdesc,
inviteid,
row_number() OVER (order by invtypeid asc ) as rn
FROM Fact_s
) B
WHERE rn <= 5
GROUP BY IDDESC, inviteid;