Filter out records that exceed certain count - sql

was hoping someone could advise on the following problem.
I have a table similar to below and am trying to filter out all IDs that have the same subject and date (not just dedupe but remove complete) from my output.
ID
Date
Subject
Type
abc
1/22/22
Welcome>
Email
def
1/23/22
Offer!
Call
xyz
1/22/22
Welcome>
Email
So in the example above we would want to remove both rows with subject, "Welcome>"
I am able to write a query that shows me the count of IDs per subject and date, but stuck finding a way to remove these from my final output.
select distinct
activitydate,
name,
subject,
count (id)
from activity
where activity.activitydate between X and Y'
and type IN ('Email')
group by 1,2,3
having count (id) > 30
order by count (id) desc
Please let me know if any further details are needed and thank you in advance!

-- Filter out all IDs that have the same subject and date
SELECT
Date,
Subject,
COUNT(*)
FROM
activity
GROUP BY
Date,
Subject
HAVING
COUNT(*) < 2

If you want to remove the rows where the COUNT by ID and SUBJECT is > 1 then you can do some thing like this.
-- untested
SELECT * FROM activity
WHERE (ID,SUBJECT) NOT IN
(SELECT ID,SUBJECT FROM activity
GROUP BY ID, SUBJECT HAVING COUNT(*) > 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.

MS Access Query to get multiple counts from the same field

I'm querying imported data that has a date/time field that I can't format as date in the table.
Sample:
Ticket Name Date
INC000101 User1 9/5/2016 10:00:34AM
INC000102 User2 9/5/2016 12:02:00PM
INC000103 User1 9/7/2016 3:34:00PM
INC000104 User2 10/1/2016 9:30:23AM
INC000105 User1 10/5/2016 10:20:00AM
INC000106 USer2 10/6/2016 4:56:00PM
I'm trying to get a count of how many tickets each user has per month. Because the Date field comes from the database as a text field, I can't seem to make that format as date/time so I use "left" to filter by month. This is what I've used to get a return on a single User item for the month of October.
SELECT COUNT(*)
FROM 2016YTD
WHERE [Name]='User1' AND left(Date,3) = '10/';
I would like to add counts for User2 through UserX per month so that I can get a count row or column for each the quantity of tickets for each user each month in one report. Everything I've tried won't save the query due to syntax errors in one form or another. I've tried variations of the following query help post as well without success.
SELECT a.distributor_id,
(SELECT COUNT(*) FROM myTable WHERE level='personal' and distributor_id = a.distributor_id) as PersonalCount,
(SELECT COUNT(*) FROM myTable WHERE level='exec' and distributor_id = a.distributor_id) as ExecCount,
(SELECT COUNT(*) FROM myTable WHERE distributor_id = a.distributor_id) as TotalCount
FROM myTable a ;
I'm sure the answer is staring at me, just not sure what at the moment.
Thanks for reading
John
This is only an answer to your first question about how to deal with dates that are stored in text fields.
To get the count for all users for every month you can do:
SELECT [Name], Format([Date],'mmmm') AS Month, COUNT(*) as Count
FROM 2016YTD
GROUP BY [Name], Format([Date],'mmmm')
A text field containing a date that is always in the same format can be treated as a date with Format() so Format([Date],'mmmm') returns the full month name for each date.
you should just need conditional aggregation. I haven't looked at access in a while but probably just something like this:
SELECT
a.distributor_id
,Format([Date],'mmmm') as Month
,SUM(IIF(level='personal',1,0)) as PersonalCount
,SUM(IIF(level='exec',1,0)) as ExecCount
,COUNT(*) as TotalCount
FROM
myTable a
GROUP BY
a.distributor_id
,Format([Date],'mmmm')

How to get duplicate values in all rows filtering by one column

Here is what my table looks like.
Person Date Entry
Person1 05-20-14 142
Person2 05-20-14 443
Person1 05-21-14 248
Person1 05-21-14 142
I need two things.
First the number of times a Person made an entry for the first time.
I tried doing it with these queries. But the problem is I need this information per day.
That is if I query for 05/21, I need to see output
"Person1 1"
142 wont be included because it already exists.
In my query, I am filtering by date already, so I am not sure how to go out and search in the rest of the dates values. Here is what I have.
SELECT PERSON, Count(distinct Entry)
from [table]
where date >= 05/21/2014
and date < 05/22/2014
group by person
order by person.
This gives me
Person1 2
Both 248 and 142 are considered here. How do I look for 142 was an entry already made in previous dates. I am not very good at nested queries.
Thanks for looking.
Will this solve your problem or give you an idea how inner query should be?
SELECT PERSON, Count(distinct Entry)
from [table]
where date >= 05/21/2014
and date < 05/22/2014
and Entry not in (select distinct entry from [table] where date <> 05/21/2014)
group by person
order by person.
in the above query i have just added an inner query to get the distinct entry from other dates
select distinct entry from [table] where date <> 05/21/2014
and i have added the where condition that the current result should not consider those entries by
and Entry not in (select distinct entry from [table] where date <> 05/21/2014)
hope this helps you.
For the first query, it sounds like you need something like this:
SELECT Person
FROM sample
GROUP BY date, person
HAVING date = '05-21-2014'
See http://sqlfiddle.com/#!3/4653d/1
This might also help:
SELECT Person, date
FROM sample
GROUP BY date, person
ORDER BY date
Hopefully that helps, let me know if I am misunderstanding something..

SQL Picking Top Date out of the Date Field

So I have a table with a date column. I want to be able to group these dates by Item field of some type. For example I might have a column called Item and within the Item field there may be 500 entries. Item 12345 might have 5 entires, each with a price. I want to be able to pull out all of the Items, grouped by the newest date.
031126-2M1 8/10/2011 12:00:00 AM 7.8678
031126-2M1 7/22/2011 12:00:00 AM 9.5620
031126-2M1 7/15/2011 12:00:00 AM 8.8090
In this example, I want to show the item with the closest date, 7/15/2011 so I can use that Price of 8.8090. The list would then show the other items, some may have one entry, others might have many, but I want to show all of them with the closets date. Need Help!
Thanks
A MS SQL Server version...
WITH
sorted_data AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY item_date DESC) AS row_id,
*
FROM
item_data
WHERE
item_date <= getDate()
)
SELECT * FROM sorted_data WHERE row_id = 1
selct * from table
where This = that
Group by something having This.
order by date desc.
having "THIS" should be in the query itself.
hope this helps..
Cheers

SQL: filter by date

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