-
Aug 11th, 2011, 08:05 PM
#1
Thread Starter
Frenzied Member
query help
Given a table like this:
tblScores
ID Name Date Score
1 Adam 8/9/11 100
2 Bill 8/9/11 95
3 Charles 8/9/11 98
4 Adam 8/8/11 100
5 Bill 8/8/11 88
6 Charles 8/8/11 93
7 Adam 8/7/11 100
8 Bill 8/7/11 89
9 Charles 8/7/11 97
This is just an example of course.
I am trying to think of a query that will tell me the name of every person who has had a score of 100 for the past 3 days in a row (i.e. the most recent 3 days)
In this example
select name from tblScores where {most recent three records in this table have score = 100}
and in this example, the query would return "Adam"
Anybody?
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 11th, 2011, 08:19 PM
#2
Re: query help
Access? SQL Server? MySQL? helps to know what type of database we're dealing with here.
-tg
-
Aug 11th, 2011, 09:36 PM
#3
Re: query help
this link may be useful for u
-
Aug 11th, 2011, 10:52 PM
#4
Re: query help
Something like
Code:
SELECT TOP 3 Table1.DDate , Table1.ID, Table1.SName, Table1.Score
FROM Table1 WHERE Score >= 100 ORDER BY DDAte DESC;
-
Aug 12th, 2011, 09:12 AM
#5
Re: query help
Think it would need to be code or script as you are evaluating those >=100 over three days consecutively.
hmmm.. Unless.
If you are only looking at the last three days :
Code:
SELECT t.username, count(t.username) as TheTotal
FROM Table as T
WHERE t.CreatedDate>=format(now()-3,"dd mmm yyyy")
AND t.score>=100
GROUP BY t.UserName
HAVING TheTotal=3
That should list only those with 3 in the last three days...
But definately would need coding if you are spanning a larger date range and still looking for those who had three consecutive...
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Aug 12th, 2011, 05:01 PM
#6
Thread Starter
Frenzied Member
Re: query help
Ecniv,
That's look like exactly what I need.
Actually it isn't the last 3 days, its the last three days for which records exist.
I guess I would just do a select distinct top 3 CreatedDate .... order by CreatedDate Desc
and then take the date from the 3rd record and put that in this query dynamically.
I'm looking forward to trying it out.
Thanks.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 12th, 2011, 05:09 PM
#7
Thread Starter
Frenzied Member
Re: query help
it isn't working
I'm getting invalid column name 'TheTotal'
THis is SQL Server 2005
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 12th, 2011, 05:13 PM
#8
Thread Starter
Frenzied Member
Re: query help
OK
it works if I change your last line to
Having count(t.username) = 3
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 12th, 2011, 05:34 PM
#9
Thread Starter
Frenzied Member
Re: query help
sorry to pile on, if you happen to know this,
how can I add a column from another table joined on the username
i.e. change:
SELECT t.username, count(t.username) as TheTotal
FROM Table1 as T
to
SELECT t.username, count(t.username) as TheTotal, p.email
FROM Table1 as T left join Table2 as p on T.username = p.username
This won't work because I get that "p.email is an invalid column name becuase it is not part of an aggregate function or included in the GroupBy clause" error
the rest of the syntax is ok. if i take out "p.email" it executes the query, but of course with no new column
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Aug 15th, 2011, 03:15 PM
#10
Re: query help
p.email needs be added to a Group By clause.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Aug 16th, 2011, 08:17 AM
#11
Re: query help
Just a quick note of caution, this won't work if there are multiple emails to one user because it'll increase the number of records in the record set and mess up your counts.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Aug 16th, 2011, 02:14 PM
#12
Thread Starter
Frenzied Member
Re: query help
Actually I was simplifying the idea.
There is a second table, tblUsers
that contains various user information.
I was hoping in addition to getting those usernames who had the three records like i described above (which is accomplished well with Ecniv's code)
to add a number of columns from the other table to the record set.
So it would be like p.email, p.telephone, p.birthdate etc
In this table person is only listed once, so there is no concern over the same person being pulled more than once. The worry is that the birthdate or other info would identical.
Currently, I'm just doing it in code, getting the first RS back and then looping through it for the other data. But it is sloooooooow.
I guess another way might be to build a temporary table in a stored procedure and just stick the data together as records, then query that table. Sound right? IT seems like I did something like that a few years ago.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|