count total no of visits based on date codeigniter - php

А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

Related

PHP/SQL: Prevent Option values from repeating

So I am trying to create a query that shows the list of all the dates customers came in to order food. However, since there were multiple customers for a particular day and thats how the values have been stored in the table, whenever I turn the following query:-
SELECT DateOfEntry FROM KFC;
Although it works and I see all the details, I dont want the same date value to repeat. For example if Two customers A and B, came in on 11/11/17 to order food, I would get the result of the above mentioned query with two records of same date. I dont want the same dates to repeat. What do I do?
I know DISTINCT works but the problem is, I am creating a php page where admins can check the list of all the users who came in on a particular day. So I am using the select and option methods of HTML to select a date which then shows the list of the users who made an entry that day. With this code, only one record is shown
Try:
SELECT DISTINCT DateOfEntry FROM KFC;
Try :
SELECT DISTINCT DateOfEntry FROM KFC;
or this
SELECT DateOfEntry FROM KFC
GROUP BY DateOfEntry;

Average Rating PHP

I structured a PHP table with two columns: 1 for the hospital Id and 1 for the rating.
When a user rates the page, in the Id section goes a default id (depending on the page) and the rating goes in the rating section obv.
Now, how can I calculate the average rating only for a certain ID?
I've never studied Php so I need some help! Thanks everyone!!
You can use AVG on rating column and GROUP BY on hotel_id column.
Can refer below query.
SELECT hotel_id, avg(rating)
FROM hotel_rating
GROUP BY hotel_id;
PS: This is sample query you need to substitute your column names and table names in that

Select from database with count and count based conditions?

I have a table with data relating to a user, and two important columns:
refer_count, which is updated when a new entry is made in the table with the referred_by column set to that users user_id, and referred_by which is the user_id of the of the user that referred them.
I want to select the users from the table that have the highest number of referrals after a certain date.
For example:
If there are 3 users, one of which referred the other 2 (lets say users 2 and 3), however user 2 was referred on the 2/12/14, whereas user 3 was referred on the 3/1/15.
If the cutoff is 1/12/14, then user 1 is returned with refer_count set to 2, but if the cutoff is after 2/12/14, then user 1 is returned with refer_count set to 1.
I've been thinking of how to do this, but I can't think of a way that would work. Is there a way?
This is via MySQL.
EDIT: I think I may need to provide for information.
The date registered (register_date) is used as the refer date. I need the refer_count to be updated with the number of users referred after the cutoff, however I need to get the actual user. This is for a 'top referrers' table. I can't figure out why I'm having so much trouble thinking of a way to do this.
SELECT user_id FROM usertable WHERE (referal_date BETWEEN '2014-12-2' AND CURDATE())ORDER BY refer_count DESC;
That's the rough idea.
You should look into normalizing your tables if you're keeping that all in the same table, though. It'd be better to keep referals in a seperate table.
Get the row with the maximum in refer_count with a Date condition for your referal_date such that it's after the certainDate:
SELECT user_id FROM table WHERE refer_count = (SELECT MAX(refer_count) FROM table) AND referal_date>certainDate;
Note that WHERE is before SELECT so it will not get the highest count first, but will filter with the date condition then get the highest count.
Edit: Updated query based on edited question.

sql query to find event views by time period

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

Alternative solution for PHP/SQL max array size problem

I have a table bundled among 100 databases in MYSQL (i.e. 1st x rows of the table in database_1, 2nd x rows of the table in database_2, ... , last x rows of the table in database_100)
Each table has a row whenever a user visits a friend for a game.
The columns are iuin, logtime, beuin.
iuin is the user id of the visitor.
beuin is the user id of the friend who was visited.
logtime is when the visit was made.
I would like to find the # of distinct friends who were visited during a week.
There is roughly 300k distinct users who are visited per day.
However, when I extended my code to calculate for a week, I ran out of memory.
My code basically does an SQL query using SELECT DISTINCT beuin for a selected week for the table in each database. I store all the beuin in an array if it's not already stored (so I count distinct friends who were visited), and return the size of the array at the end.
FYI, I can't edit the database around such as joining all the tables in different databases into one table.
Is there any alternative ways i can do this?
Thanks
It's hard to say anything about your without the one. But I think you can solve this problem using mysql. My quick solution:
Create table - CREATE table if not exist users_ids(user_id INT NOT NULL DEAULT 0 PRIMARY KEY(UNIQUE)); in the first db
Truncate users_ids
Run 100 queries like INSERT IGNORE INTO db1.users_ids select distinct user_id from db1.table1;
Select count(*) from users_ids;

Categories