sql query to find event views by time period - php

i have the following query to return a list of events created by a user and the events total hits
SELECT view_id, user_id, event_id, date_viewed, COUNT( event_id ) AS views
FROM `tbl_event_views`
WHERE user_id =1
GROUP BY event_id
LIMIT 0 , 30
which is fine, but what i want to acheive is the number of views by days,weeks,months,years based on the date the listing was created to the current date.
could someone point me in the correct direction please?
really not sure where to start with this.
Many Thanks
Luke

You need to add a column for date created for the views. There is a timestamp for sql but theres nothing to remember when an item was created unless you add it to the database. Add that column and put the timestamp into it, then you just see how many views fall into your date range with DateCreated IN (StartDate, EndDate) or something similar

Related

count total no of visits based on date codeigniter

Аctually I am working on the CI medical project. I have column name visit date. I want to display the total no of visits by each patient on that specific date, for example 5-4-2019 total visits by that patient were 3. Similarly 4-3-2020 total visits were 5.
How can I create another column that counts total visits based on this column visit date?
Since you have provided no DDL or DML, the best we can do is guess at what your schema might be and what your code might look like. So, here's a guess:
select -- <your existing columns>
visits_on_this_date = count(*) over (partition by patient_id, visit_date)
from -- the rest of your query here
Something simple like that?
select patient_id,visit_date,count(*) as total_visits
from table
group by patient_id,visit_date

MySQL select unused/stale id based on timestamp period

I have an attendance table with simple .user and .timestamp and want to identify any .user without a timestamp since a specific time (eg 1 year ago).
I have tried several combinations of between and is not between etc., but can't seem to get the correct output (my logic and SQLs is obviously different :( )
The want to remove 'stale' users (haven't attended for a year). Once I have the data from the attendance table I will use that to delete records from other tables containing their details.
To Delete:
DELETE FROM userattendance WHERE user_id IN (SELECT user_id FROM users WHERE mytimestamp < (UNIX_TIMESTAMP() - 31557600))
It's pretty much saying DELETE data from the user attendance table where mytimestamp field is less than the current unix timestamp minus a year for any user.

PHP MySQL calculate how many rows have the same data

Ok, well I know I can just say WHERE example='vrvrv' but that's not the case. Ok I have a rating system and whenever a page is rated the page id and the date it was rated is sent to a table in my database. Now on the homepage I want to determine top rated that day, I can easily compare dates but I'm not quite sure how to count how many rows have the same value in the project_id column. It would be amazing if someone could help me out, thanks!
You can use GROUP and COUNT to get duplicates:
SELECT count(`project_id`) AS `duplicates` FROM `table` GROUP BY `project_id` HAVING `duplicates` > 1

Getting last and next db-record by date in MySQL-Table?

I've got a certain question related to a blog, which I am developing in OOP (PHP) right now. All blogposts are stored in a MySQL-table.
In the app you can read a specific post by giving the id of the post via GET-parameter. So like http://example.com/?id=2. Under the blogpost I want to show to navigation links like "previous" and "next", to see the next and previous blogpost ordered by date relative to the post the user is reading now. So what I need is the id of the next and the previous record in the mysql-table by date.
How to solve this? Is there any way to solve this in SQL, or do I have to get all records with php and then do some checks to determine if this is the last or next one?
Just a note: I don't want to fetch the last and next posts by id, but by date to get the id of them.
Any help would be appreciated.
Thanks.
To get the newest record older than a certain date:
SELECT id
FROM yourtable
WHERE date < '2010-08-15 14:07:12'
ORDER BY date DESC
LIMIT 1
Or the oldest record newer than a certain date:
SELECT id
FROM yourtable
WHERE date > '2010-08-15 14:07:12'
ORDER BY date
LIMIT 1
Make sure that the date column is indexed.
This works fine if date is unique, but if you have two records with exactly the same date and use next repeatedly this could skip over one of the records. To solve this you could use a tie-breaker column such that (date, tie-breaker) is always unique. You could for example use the primary key as a tie-breaker.
See my answer to this question to see how to do this:
Forward Back Records in MySQL with the same DATA in the primary
the next id:
SELECT TOP 1 id FROM blogposts WHERE blogdate > $given_date_of_actual_blogpost$ ORDER BY blogdate
the previous id:
SELECT TOP 1 id FROM blogposts WHERE blogdate < $given_date_of_actual_blogpost$ ORDER BY blogdate DESC

Select Mulitple Records based on One record's column value

I have a table which contains related records (multiple revisions of the same record). Each record has a string field that resembles a date (date, time, and microtime). I want to select all records that are older than a specific date. If a record has a related record newer than the specific date, I do not want to select any of those related records. Any ideas for that select statement? Eventually, it will be a REMOVE statement.
Edit: Some Sample Rows
id shared_id date type other_data...
1 2 2010-01-01 01:02:03.1234567 original ...
2 3 2010-01-15 11:12:03.1234733 original ...
3 2 2010-02-01 03:04:04.5465654 amendment ...
If my cut-off date was "2010-01-31", I would want to select id #2 only because id #1 has an amendment newer than the cut-off date.
I found this link helping me generate the select statement.
SELECT DISTINCT T.shared_id,T.date,T.id
FROM table T WHERE T.date = (
SELECT MAX( date ) FROM table WHERE shared_id = T.shared_id )
AND T.date < 'my_cut_off_date_string'
This seems to work for me. Thanks for everyone's help.
Maybe you can try the DATEDIFF() function, check this out:
Link 1
Or this one: Link 2
Or maybe you can try the classic query (SELECT FROM table WHERE data <= anotherdata), but in this case you need to convert both data in timestamp format
DELETE from tablename where relatedField = 'relatedValue' AND dateField <= dateToDeleteFrom
Something along those lines should do what you need it to do, if I understand your scenario. If you provide a sample data set I can adjust the statement to more accurately represent your need, but I think this is a good starting point.
HTH,
Jc

Categories