How to calculate average users visits per day - sql

I have a requests table which include request data for my my page requests. I have field including Id, UserAgent, Ip, EventDateUtc, etc. How to write SQL which give me average requests per day.

You can get a count grouped by UserAgent,EventDateUtc and then do a AVG on top of it.
SELECT AVG(reccount) avg_count,EventDateUtc
FROM
(
SELECT COUNT(*) reccount,UserAgent, CONVERT(DATE,EventDateUtc) EventDateUtc
FROM PageVisit
GROUP BY CONVERT(DATE,EventDateUtc),UserAgent
)T
GROUP BY EventDateUtc
If you want a total average for all days, don't do a GROUP BY in the outer query.
SELECT AVG(reccount) avg_count
FROM
(
SELECT COUNT(*) reccount,UserAgent, CONVERT(DATE,EventDateUtc) EventDateUtc
FROM PageVisit
GROUP BY CONVERT(DATE,EventDateUtc),UserAgent
)T

The inner select counts visits / day, the outer the average:
select avg(cnt)
from (select count(*) as cnt
from tablename
group by EventDateUtc)

Related

Users that played in X different dates - SQL Standard + BigQuery

I have the following schema of a data model (I only have the schema, not the tables) on BigQuery with SQL Standard.
I have created this query to select the Top 10 users that generated more revenue in the last three months on the Love game:
SELECT
users.user_id,
SUM(pay.amount) AS total_rev
FROM
`my-database.User` AS users
INNER JOIN
`my-database.IAP_events` AS pay
ON
users.User_id = pay.User_id
INNER JOIN
`my-database.Games` AS games
ON
users.Game_id = games.Game_id
WHERE
games.game_name = "Love"
GROUP BY
users.user_id
ORDER BY
total_rev ASC
LIMIT
10
But then, the exercise says to only consider users that played during 10 different days in the last 3 months. I understand I would use a subquery with a count in the dates but I am a little lost on how to do it...
Thanks a lot!
EDIT: You need to count distinct dates, not transactions, so in the qualify clause you'll need to state COUNT(DISTINCT date_) OVER ... instead of COUNT(transaction_id) OVER .... Fixed the code already.
As far as I understood, you need to count the distinct transaction_id inside IAP_Events on a 3 previous months window, check that the count is greater than 10, and then sum the amounts of all the users included in that constraint.
To do so, you can use BigQuery's analytic functions, aka window functions:
with window_counting as (
select
user_id,
amount
from
iap_events
where
date_ >= date_sub(current_date(), interval 3 month)
qualify
count(distinct date_) over (partition by user_id) > 10
),
final as (
select
user_id,
sum(amount)
from
window_counting
group by
1
order by
2 desc
limit 10
)
select * from final
You will just need to add the needed joins inside the first CTE in order to filter by game_name :)

Select student do most of the project in least amount of time

I have 2 tables, STUDENT(stu_id, name) and PROJECT(project_id, name), linked together by TASK(stu_id, project_id, duration), how can I find student with the number of projects is the largest while the duration of all that projects is the least? I've already tried this, but it's definitely wrong, since it find MAX COUNT PROJECT then join with MIN SUM DURATION, not MAX COUNT PROJECT AND MIN SUM DURATION
--WRONG
SELECT S.stu_id, S.stu_name
FROM STUDENT AS S
LEFT JOIN TASK AS T
ON S.stu_id = T.stu_id
GROUP BY S.stu_id, S.stu_name
HAVING COUNT(T.project_id) >= ALL(
SELECT COUNT(T1.project_id)
FROM TASK AS T1
GROUP BY T1.stu_id
)
AND SUM(T.duration) <= ALL(
SELECT SUM(T2.duration)
FROM TASK AS T2
GROUP BY T2.stu_id
)
how can I find student with the number of projects is the largest while the duration of all that projects is the least?
This sounds like aggregation and limiting:
select top (1) stu_id, count(*) as num_projects, sum(duration) as total_duration
from task
group by stu_id
order by count(*) desc, sum(duration) asc;

How to count the number of records in an sql database

I am using the below query to get distinct records from 4 specific columns in an sql DB.
SELECT DISTINCT customer,
product,
category,
sector
FROM data_table
I need to add the count of products in this query. Any ideas?
are you find something below
select count(*) from
(SELECT DISTINCT customer, product, category, sector
FROM data_table
) a
or do you need window function count() if your dbms support
SELECT DISTINCT customer, product, category, sector,
count(*) over() as cnt
FROM data_table

SQL Percentage Count Query By Date

I am able to calculate the percentage count on a particular date in a Microsoft Access 2007 SQL query using:
SELECT Date, Val, (Count(Val) / (SELECT Count(*) From Table HAVING Date=#7/31/2012#) as PercentVal
FROM Table
GROUP BY Date, Val
HAVING Date=#7/31/2012#
However, I would like to make this same calculation over every date using the count totals . For instance, the query:
SELECT Date, Val, Count(*) AS CountVal
FROM Table
GROUP BY Date, Val
finds the counts in every period. I would like to add an additional column with the percent counts. However, I can't seem to figure out how to calculate count percentage in every period without using the above block of text and setting up queries for each individual period.
You can subquery it like this:
SELECT A.ADate, A.Val, COUNT(A.Val) / B.DateCount
FROM Table1 AS A
INNER JOIN (
SELECT C.ADate, COUNT(*) AS DateCount
FROM Table1 C
GROUP BY C.ADate
) AS B ON A.ADate = B.ADate
GROUP BY A.ADate, A.Val, B.DateCount

Aggregate SQL Function to grab only one from each grouping

I have a table that I need to normalize with many fields In SQL-Server 2000.
It contains 2 fields which I'm using to come up with distinct combination as defined by the specs.
ID and Rate: there are multiple rows of same IDs and Rates
I first created a temp table by grouping the IDs and Rates combination.
SELECT ID, Count(*) AS IDCounts, SUM(RATE) As Total
INTO #Temp
GROUP BY ID
Now I use Distinct to find only the unique combinations. So i'll have multiple ID groups sharing same Total and IDCounts
SELECT DISTINCT Total, IDCounts
INTO #uniques
FROM #Temp
Now my question is how to join a single ID back to that distinct grouping of IDCounts and Total and put that into a new table? It doesn't matter which one of the IDs in the groups as long as I use one from the same grouping.
Keeping your temp tables (although this could all be done in a single query):
SELECT ID, Count(*) AS IDCounts, SUM(RATE) As Total
INTO #Temp
GROUP BY ID
SELECT Total, IDCounts, MIN(ID) AS SomeID
INTO #uniques
FROM #Temp
GROUP BY Total, IDCounts
Add "Min(ID) AS FirstID" to the select into #uniques.
Try something like this:
SELECT MAX(ID) AS Id, Count(*) AS IDCounts, SUM(RATE) As Total
FROM SOMETABLE
GROUP BY IDCounts, Total