SQL: filter by date - sql

I have a table SIGNUPS, where I register all signups to a specific event. Now, I would like to get all people who signed up to an event, with an extra column STATUS telling if the user is actually accepted (STATUS = "OK") or if it is in a waiting list (STATUS="WL"). I tried something like this
SELECT *, IDUSER IN (SELECT IDUSER FROM SIGNUPS ORDER BY DATE ASC LIMIT 10)
as STATUS from SIGNUPS WHERE IDEVENT = 1
This should return STATUS 1 for the first 10 users who signed up, and 0 for all other ones. Unluckily, I get a Mysql error telling me that LIMIT in subqueries is not yet supported.
Could you please suggest another way to get the same information?
Thanks

Something like the following will get what you need - although I haven't tested it against some sample tables. The subqueries find the date above which the last ten signups occur, which is then used to comapre to the date of the current row.
select
s.*,
s.DATE > d.min_date_10 AS STATUS
from SIGNUPS s
join (
select MIN(DATE) AS min_date_10 from (
select DATE from SIGNUPS order by DATE asc LIMIT 10
) a
) d
WHERE IDEVENT = 1

Related

SQL Query, how to order by date in specific case

I have a table 'exam_table' containing : User_ID, Exam_date, Exam_status.
Exam_status = ['Success' or 'Fail']
The question is :
Based on the above data, propose an SQL
query to finds the 5 candidates with the most failures. In case
of equality, we wish to obtain first the students whose date of first exam is the most distant in time.
I found the 5 candidates with the most failures but I still don't know how to sort them according to exam_date in case of equality.
Do you have any suggestions? Thank you in advance for helping !
Your order by is a clause which has ordering criteria separated by ,. So you can easily add another criteria, like below:
SELECT User_ID, count(exam_status) as nb_Failures
FROM exam_table
GROUP BY User_ID
ORDER BY nb_Failures, min(exam_date)
LIMIT 5;
UPDATED:
corrected by the date of the first exam:
SELECT
user_id,
MIN (exam_date) AS first_exam_date,
SUM (
CASE exam_status
WHEN 'Failed' THEN 1
ELSE 0
END
) AS nb_failures
FROM exam_table
GROUP BY user_id
ORDER BY nb_failures DESC, first_exam_date ASC
LIMIT 5;
or like this:
SELECT
user_id,
MIN (exam_date) AS first_exam_date,
COUNT(exam_status) AS nb_failures
FROM exam_table
WHERE exam_status = 'Failed'
GROUP BY user_id
ORDER BY nb_failures DESC, first_exam_date ASC
LIMIT 5;
PS: aggregate functions must also be applied to the date
PPS: but the first and second queries have different results. In the first, the date of the first exam is selected, in principle, it does not matter if it is successful or not. The second selects only the date of the first failed exam.

How Can I Retrieve The Earliest Date and Status Per Each Distinct ID

I have been trying to write a query to perfect this instance but cant seem to do the trick because I am still receiving duplicated. Hoping I can get help how to fix this issue.
SELECT DISTINCT
1.Client
1.ID
1.Thing
1.Status
MIN(1.StatusDate) as 'statdate'
FROM
SAMPLE 1
WHERE
[]
GROUP BY
1.Client
1.ID
1.Thing
1.status
My output is as follows
Client Id Thing Status Statdate
CompanyA 123 Thing1 Approved 12/9/2019
CompanyA 123 Thing1 Denied 12/6/2019
So although the query is doing what I asked and showing the mininmum status date per status, I want only the first status date. I have about 30k rows to filter through so whatever does not run overload the query and have it not run. Any help would be appreciated
Use window functions:
SELECT s.*
FROM (SELECT s.*,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY statdate) as seqnum
FROM SAMPLE s
WHERE []
) s
WHERE seqnum = 1;
This returns the first row for each id.
Use whichever of these you feel more comfortable with/understand:
SELECT
*
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY statusdate) as rn
FROM sample
WHERE ...
) x
WHERE rn = 1
The way that one works is to number all rows sequentially in order of StatusDate, restarting the numbering from 1 every time ID changes. If you thus collect all the number 1's togetyher you have your set of "first records"
Or can coordinate a MIN:
SELECT
*
FROM
sample s
INNER JOIN
(SELECT ID, MIN(statusDate) as minDate FROM sample WHERE ... GROUP BY ID) mins
ON s.ID = mins.ID and s.StatusDate = mins.MinDate
WHERE
...
This one prepares a list of all the ID and the min date, then joins it back to the main table. You thus get all the data back that was lost during the grouping operation; you cannot simultaneously "keep data" and "throw away data" during a group; if you group by more than just ID, you get more groups (as you have found). If you only group by ID you lose the other columns. There isn't any way to say "GROUP BY id, AND take the MIN date, AND also take all the other data from the same row as the min date" without doing a "group by id, take min date, then join this data set back to the main dataset to get the other data for that min date". If you try and do it all in a single grouping you'll fail because you either have to group by more columns, or use aggregating functions for the other data in the SELECT, which mixes your data up; when groups are done, the concept of "other data from the same row" is gone
Be aware that this can return duplicate rows if two records have identical min dates. The ROW_NUMBER form doesn't return duplicated records but if two records have the same minimum StatusDate then which one you'll get is random. To force a specific one, ORDER BY more stuff so you can be sure which will end up with 1

Sql query to get bounce rate based on session id and datetime

We have table with 3 columns- Url of Page Visited, User Session ID and Datetime.
Based on this information we have generate result with 2 columns - Date (unique) and Bounce Rate.
It is very clear that we need to look for single occurrences of session id, if there are 2 entries for same session id it means the user hitted the another page and didn't bounced but one entry means it bounced.
I can not write a sql query for this. I tried grouping data by session id and date but couldn't get the result in required format.
Can anyone do this?
If you want the number of sessions with only one page per day, you can use aggregation:
select dte,
avg( (num_pages = 1)::int ) as bounce_rate
from (select sessionid, min(datetime)::date as dte, count(*) as num_pages
from t
group by sessionid
) t
group by dte;

Find most recent date of purchase in user day table

I'm trying to put together a query that will fetch the date, purchase amount, and number of transactions of the last time each user made a purchase. I am pulling from a user day table that contains a row for each time a user does anything in the app, purchase or not. Basically all I am trying to get is the most recent date in which the number of transactions field was greater than zero. The below query returns all days of purchase made by a particular user when all I'm looking for is the last purchase so just the 1st row shown in the attached screenshot is what I am trying to get.
screen shot of query and result set
select tuid, max(event_day),
purchases_day_rev as last_dop_rev,
purchases_day_num as last_dop_quantity,
purchases_day_rev/nullif(purchases_day_num,0) as last_dop_spend_pp
from
(select tuid, event_day,purchases_day_rev,purchases_day_num
from
app.user_day
where purchases_day_num > 0
and tuid='122d665e-1d71-4319-bb0d-05c7f37a28b0'
group by 1,2,3,4) a
group by 1,3,4,5
I'm not going to comment on the logic of your query... if all you want is the first row of your result set, you can try:
<your query here> ORDER BY 2 DESC LIMIT 1 ;
Where ORDER BY 2 DESC orders the result set on max(event_day) and LIMIT 1 extracts only the first row.
I don't know all of the ins and outs of your data, but I don't understand why you are grouping within the subquery without any aggregate function (sum, average, min, max, etc). With that said, I would try something like this:
select tuid
,event_day
,purchases_day_rev as last_dop_rev
,purchases_day_num as last_dop_quantity
,purchases_day_rev/nullif(purchases_day_num,0) as last_day_spend_pp
from app.user_day a
inner join
(
select tuid
,max(event_day) as MAX_DAY
from app.user_day
where purchases_day_num > 0
and tuid='122d665e-1d71-4319-bb0d-05c7f37a28b0'
group by 1
) b
on a.tuid = b.tuid
and a.event_day = b.max_day;

mysql counts in a group by

Say I have a voting table, where users can vote on values up, down, or flat.
Say a user gets a point each time the corrcet projection is made.
At the end of each week I want to display some statistics.
Something like:
SELECT user_id, sum( user_points ) as sum_points FROM voting_results
WHERE voting_date > ('2009-09-18' - INTERVAL 1 WEEK)
GROUP BY user_id
ORDER BY sum_points DESC
Fine. This will get me a nice list where the "best guessing" user comes up first.
Here's my question:
How do I - in the same query - go about obtaining how many times each user has voted during the given timeperiod?
Put another way: I want a count - per row - that need to contain the number of rows found with the user_id within the above mentioned query.
Any suggestions?
Thanks.
Just add COUNT(*):
SELECT user_id,
SUM(user_points) as sum_points,
COUNT(*) AS num_votes
FROM voting_results
WHERE voting_date > ('2009-09-18' - INTERVAL 1 WEEK)
GROUP BY
user_id
ORDER BY
sum_points DESC