The following query works great to count and get the total sum of visitors to my page.
SELECT id, COUNT(id) AS count
FROM visits
WHERE status <> 'test'
GROUP BY id
UNION ALL
SELECT 'SUM' id, COUNT(id)
FROM visits
WHERE status <> 'test'
But inside the visits table I have a column called IP where I store the IP of the visitors. I want to change the above query so that visits with the same IP are not counted (I only want to count unique IPs). How can I do it?
I have tried HAVING COUNT(IP) > 1 but it doesn't work.
Thanks!!!
Related
I have table with users actions. One of them occurs every time when user opens certain page.
Table structure:
id, user_id, action_type, created_at...
I need to select from this table all actions per day/week... but without repeating of similar in one day. For example: user has visited 10 pages but 5 of them was the same. The result of selection should contain only unique pages per day.
Is it possible to do with only MySQL logic? Or better I should update repeated action if it occurs the same day?
One approach uses select distinct:
select distinct user_id, action_type, date(created_at) created_date
from mytable
If needed, you can also count how many times each action_type was met on a user_id and day basis with aggregation:
select user_id, action_type, date(created_at) created_date, count(*) cnt
from mytable
group by user_id, action_type, date(created_at)
I suggest the following SQL code :
SELECT DISTINCT URL
FROM table_name
GROUP BY date;
I assume that your table name is table_name, you have the URLs (pages) in the column named URL and you you track the date in the column named date;
I have table like this
enter image description here
I need to get the data only whose age > 10, along with that i need to get the total number of records present in the table. ie. in this example it is 4 records. what i need is in single query i need to get the total number of records present in table and columns which i query.
Query will be somewhat like
SELECT ID, NAME, count(TOTAL NUMBER OF RECORDS IN TABLE) as Count from MYTABLE WHERE AGE > 10
Any idea about this ?
You can use a subquery in the FROM clause:
SELECT ID, NAME, c.cnt as Count
FROM MYTABLE CROSS JOIN
(SELECT COUNT(*) as cnt FROM MYTABLE) c
WHERE AGE > 10 ;
Both databases support window functions, but they are not really helpful here, because the count is not filtered in the same way as the outer query. If you do want the filter for both, then in the most recent versions you can do:
SELECT ID, NAME, COUNT(*) OVER () as cnt
FROM MYTABLE
WHERE AGE > 10 ;
You can try below - using scalar subquery
SELECT ID, NAME, age,(select count(*) from mytable WHERE AGE > 10) as Count
from MYTABLE
WHERE AGE > 10
I have a database structure of:
ip | yid | date
Where ip represents a user's IP and yid is a specific page ID. I'm trying to group by the ip and yid column, which I have done below. But then I need to group those results by the yid. So it would be grouping it into:
yid | count
Where count is the number of times that the page was called limiting 1 call per ip address.
Right now I have:
SELECT `ip`, `yid`, `time`, MAX(`time`), count(*)
FROM mp_downloads
GROUP BY CONCAT(`ip`, `yid`), `yid`
ORDER BY count(*) DESC
But it's not grouping the page ids after it groups by the distinct yid & ip combo.
Try This using nested query:
select temp.*, count(*) from
(SELECT `ip`, `yid`, `time`, MAX(`time`)
FROM mp_downloads
GROUP BY CONCAT(`ip`, `yid`))temp group by temp.yid;
The normative pattern for getting a count of ip addresses that have accessed a (yid) would be:
SELECT d.yid
, COUNT(DISTINCT d.ip) AS ip_count
, MAX(d.date) AS latest_time
FROM mp_downloads d
GROUP BY d.yid
For best performance, you'd want a suitable index, e.g.
... ON mp_downloads (yid, ip, date)
There's no need to group by an expression that concatenates two columns. There's no need for a derived table (inline view) or a subquery. If you don't need the latest_time, you can omit that expression.
I have a voting application that writes values to a mysql db table. It is a preference/weighted voting system so people choose a first option, second option, and third option. These all go into separate fields in the table. I'm looking for a way to write a query that will assign numerical values to the responses (3 for a first response, 2 for a second, 1 for a first) and then display the value with the summed score. I've been able to do this for total number of votes
select count(name) as votes,name
from (select 1st_option as name from votes
union all
select 2nd_option from votes
union all
select 3rd_option from votes) as tbl
group by name
having count(name) > 0
order by 1 desc;
but haven't quite figured out how to assign values to response in each column and then pull them together. Any help is much appreciated. Thanks!
You could do something like this:
select sum(score) as votes,name
from (select 1st_option as name, 3 as score from votes
union all
select 2nd_option as name, 2 as score from votes
union all
select 3rd_option as name, 1 as score from votes) as tbl
group by name;
I have a column that takes user names. How can I count the number of instances of a users name. For example I have 10 rows and in column username i want to count all names that show up multiple times. I would like to build a list of the top contributors to my database. So if username alex shows up 5 times and jeff shows up 3 and april shows up 2 times i will count this and from that I can build my list.
Try GROUP BY:
SELECT username, COUNT(*) AS user_count
FROM yourtable
GROUP BY username
ORDER BY user_count DESC
Try something like
SELECT USER_NAME, COUNT(USER_NAME) FROM YOUR_TABLE GROUP BY USER_NAME;
If you want to get a count of all the usernames then you just do the following SQL:
Select Count(*) from tablename
If you want to get just the count of unique usernames
Select Count(*) from tablename Group by username