November 22, 2022 at 6:08 am
November 22, 2022 at 7:20 am
An outer join to some form of a calendar table or sequence generator along with a count of days vs students appearing in the table should do the trick.
If you want a coded answer, use one of many methods to provide readily consumable data, please. The article at the first link in my signature line below identifies one method. At the very least, make the data copyable instead of using a graphic. Thanks for helping us help you. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
November 22, 2022 at 7:07 pm
I think all that is needed for this is a GROUP BY and HAVING count(*) = 3. If you need it to be dynamic - then you need to determine the total number of submission dates and use that in the HAVING clause.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
November 22, 2022 at 7:12 pm
You do need an outer join with the dates students are expected to have turn-ins in order to accommodate a very naughty class that has conspired with each other where no one turns in an assignment.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 22, 2022 at 7:28 pm
Jeff,
As requested, I am adding the sql for creating the data.
Apologies for not doing it.
declare @student table
(
StudentId int
,SubmissionDate date
)
insert into @student(StudentId,SubmissionDate) values(1,'10/01/2018')
insert into @student(StudentId,SubmissionDate) values(2,'10/01/2018')
insert into @student(StudentId,SubmissionDate) values(3,'10/01/2018')
insert into @student(StudentId,SubmissionDate) values(4,'10/01/2018')
insert into @student(StudentId,SubmissionDate) values(1,'10/02/2018')
insert into @student(StudentId,SubmissionDate) values(2,'10/02/2018')
insert into @student(StudentId,SubmissionDate) values(3,'10/02/2018')
insert into @student(StudentId,SubmissionDate) values(1,'10/03/2018')
insert into @student(StudentId,SubmissionDate) values(2,'10/03/2018')
insert into @student(StudentId,SubmissionDate) values(4,'10/03/2018')
select * from @student
November 22, 2022 at 8:17 pm
You do need an outer join with the dates students are expected to have turn-ins in order to accommodate a very naughty class that has conspired with each other where no one turns in an assignment.
You don't need that to answer *this* question. I agree that you would need that to identify which students did not submit the assignments due and which dates were missed.
But to just identify the students that did submit on every day - all you need is the number of days to match. Any student that has a distinct count for each day equal to the number of days being checked, submitted the assignment on each day.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
November 23, 2022 at 12:48 am
But to just identify the students that did submit on every day - all you need is the number of days to match. Any student that has a distinct count for each day equal to the number of days being checked, submitted the assignment on each day.
EDIT... I pulled my answer here... Jeffrey Williams is correct... you don't need a sequence of dates to solve the question that was asked.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 23, 2022 at 12:58 am
Yeah, but in the actual question the OP asks for those students that turned in the assignment for all dates.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
November 23, 2022 at 1:35 am
Yeah, but in the actual question the OP asks for those students that turned in the assignment for all dates.
Sorry... I was in the process of changing my reply when you replied and I don't have the original anymore but you do.
Anyway, I agree that to answer only the precise question that was asked, the solution code would not require a series of dates.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 23, 2022 at 1:42 am
Ok, Jeffrey... the Op posted some good test data. I'll give you the honors for providing the solution.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 23, 2022 at 2:20 am
jignesh209 ,
Jeffrey must've gotten busy. Apologies for the delay. Here's the code that will answer the question that you asked.
SELECT StudentId
FROM #Student
GROUP BY StudentID
HAVING COUNT(*) = 3
;
Here's the result set from that code using the given test data...
Are you sure that's all you'd want to know from this data?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 23, 2022 at 6:47 am
-- swePeso solution
WITH cteData(StudentId, delta, rnk)
AS (
SELECT StudentId,
DATEDIFF(DAY, '19000101', SubmissionDate),
ROW_NUMBER() OVER (PARTITION BY StudentId ORDER BY SubmissionDate) AS rnk
FROM @student
)
SELECT DISTINCT StudentId
FROM cteData
GROUP BY StudentId,
delta - rnk
HAVING COUNT(*) >= 3;
N 56°04'39.16"
E 12°55'05.25"
November 23, 2022 at 2:19 pm
-- swePeso solution
WITH cteData(StudentId, delta, rnk)
AS (
SELECT StudentId,
DATEDIFF(DAY, '19000101', SubmissionDate),
ROW_NUMBER() OVER (PARTITION BY StudentId ORDER BY SubmissionDate) AS rnk
FROM @student
)
SELECT DISTINCT StudentId
FROM cteData
GROUP BY StudentId,
delta - rnk
HAVING COUNT(*) >= 3;
Now there's a different kind of embedded date table based on Date Serial Numbers. Nicely done Peter.
You're probably going to get chastised for not needing such a thing, as well. 😀 😀 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
November 23, 2022 at 2:51 pm
Just in case anyone wonders what I'm going on about, the original question is a "data analysis" question.
Yep. It can be solved without using a series of dates (and Peter's solution makes a "hidden" sequencing of dates). But just answering the question only solves one problem and, when it comes to "data analysis", I've found the you need to leave the door open for when whomever the code is created for says "Great... now can you show me the students that don't have consecutive completions and can you show me any days that are missing"? And how about when the next email the Op gets that says "Great... the table I'm working with actually has thousands of rows across multiple a lot more than just 3 dates..." ?
There are also other things to consider. The original post has submittal dates with dates AND times indicating something other than the DATE datatype. While the posted data has no times, should you actually trust that or help protect the users from what happens if and when times are suddenly involved because of currently unknown requirements. That's happened to a whole lot of people even after they asked the question of "Will times ever be included?" and the response was "No, never". Peter's code does handle that issue nicely because of the the sequence based on dates that he uses. The code I posted out of frustration about the insistence that no date sequence is need will fail if times are involved.
So... yeah... it can be done without a date or other sequence... why would anyone cripple the possibilities that way? And, this isn't Stack Overflow where such possibilities are suppressed.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 23, 2022 at 2:53 pm
p.s. This would make a great DA whiteboard interview question... if the candidate only comes up with the solution to the exact problem, you might want to consider someone else. 😉
The question I always ask myself in regards to the person asking the question in real life for such analysis questions is the same question I asked in the "simple" coded response I made... "Are you sure that's all you'd want to know from this data?"
The answer is usually "No... can you also do the following..."?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 18 total)
You must be logged in to reply to this topic. Login to reply