Solved

sql query.

Posted on 2008-10-08
12
175 Views
Last Modified: 2012-05-05
i have 2 tables...  client and games.....

in client i have many columns, of which 2 are client id and user id

in games i have many columns , of which one is client id

now i need to get to know how many games are generated by the user within 30 days.......... from that userid.



0
Comment
Question by:sakvk
  • 6
  • 4
  • 2
12 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22672828
Try something like this substituting my made up column names with real ones.
SELECT Count(g.*) As NumGamesPlayed
FROM client c INNER JOIN games g
ON c.clientID = g.clientID
WHERE c.userID = 'u1234'
AND g.datePlayed >= DateAdd(dd, -30, DateDiff(dd, 0, getdate()))

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22672846
select client.userid, count(*)
from client inner join games on client.clientid = games.cliented
group by client.userid
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 22672855
Ignore my suggestion... it was incomplete, and mwvisa1's looks good!
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:sakvk
ID: 22675561
hi mwvisa1,

when u say
g.datePlayed >= DateAdd(dd, -30, DateDiff(dd, 0, getdate())), whts dd? in that... why do we need to do DateAdd and then Datediff....

Can I just do a DateDiff(timestamp,g.dateplayed) < '45'
 
please let me know...

the other part of the query seems good.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22675909
>>now i need to get to know how many games are generated by the user within 30 days.......... from that userid.

g.datePlayed >= DateAdd(dd, -30, DateDiff(dd, 0, getdate()))

dd = day interval
-30 = goes back 30 days from getdate() (i.e. today)
DateDiff = used to ensure that we go back to 30 days ago at midnight, so that all games for that day are included regardless of time.  If you just do DateAdd it will add x number of days with current timestamp which in a >= statement will exclude records for that day with timestamps earlier than current time the query is run.  

Hopefully that makes sense.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22675921
g.datePlayed ==> column name in your games or whichever table is used to determine when a game was generated/played/etc.  This is field you use to determine what is 30 days ago, so whatever the name is change that here.  
0
 

Author Comment

by:sakvk
ID: 22677854
so can i say dd is CURTIMESTAMP()

DateAdd(CURRENT_TIMESTAMP, -30, DateDiff(CURTIMESTAMP(), 0, getdate()))

as all the time is in date and time format
i think.. its DATE_ADD or ADDDATE......

if i am abl to get this , then the whole query works......
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22678124
I am sorry, that was bad of me too assume.  What SQL platform are you on?  CURRENT_TIMESTAMP() is MySQL I believe, am I correct on that?  

If so, please use this as reference for functions I am about to use:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
(specifically) http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add

DATE_ADD(NOW(), INTERVAL -30 DAY)

NOW() - INTERVAL 30 DAY

For SQL server, the DateAdd(dd, -30, DateDiff(dd, 0, getdate())) was already implemented code, no need to change anything unless you want to adjust the 30 to go more/less days in past.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22678156
Putting it together:
SELECT Count(g.*) As NumGamesPlayed
FROM client c INNER JOIN games g
ON c.clientID = g.clientID
WHERE c.userID = 'u1234'
AND g.datePlayed >= DATE_ADD(DATE(NOW()), INTERVAL -30 DAY); 

Open in new window

0
 

Author Comment

by:sakvk
ID: 22680114
yes boss this worked.. thanku
0
 

Author Closing Comment

by:sakvk
ID: 31504410
very happy with the solution....
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22681062
You are welcome.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question