Forum Moderators: open
I have a simplified datebase table below that Im provided with.
How can I extract the duration of membership of the rap stars below?
fields:
username ¦ event ¦ time
Snoop , join , 1121535147
Snoop , leave , 1131535147
Dre , join , 1121535126
Dre , leave , 1131735191
jayz , join , 1141735191
Here is a query that I have to get the difference between the min and max. Works darn well, but one problem.
It didn't take into account those who are still enrolled. e.g. jayz
So I get a bunch of zeros.
SELECT `username` , (
MAX( time ) - MIN( time )
) AS diff
FROM `log`
WHERE `event` LIKE 'join'
OR `event` LIKE 'leave'
GROUP BY `username`
I have tried adding a ' AND diff!=0 ' to the WHERE clause but it throws up an error ' Unknown column 'diff' in 'where clause' '
SELECT `username` , (
MAX( time ) - MIN( time )
) AS diff
FROM `log`
WHERE (`event` LIKE 'join'
OR `event` LIKE 'leave')
AND diff!=0
GROUP BY `username`
Anyway around this?
Thanks in advance!
If that doesnt work (sorry, can't test it), try:
SELECT `username` , (
MAX( time ) - MIN( time )
) AS diff, COUNT(username)
FROM `log`
WHERE `event` LIKE 'join'
OR `event` LIKE 'leave'
GROUP BY `username`
WHERE COUNT(username)=2