MYSQL query for a table - mysql

I have a table called visitors which contains IP and country columns.
Now I want to query the table such that I get unique countries from the table and display the count of rows of that country.
Table:
IP Country
1.1.1.1. xyz
1.2.3.4 xyz
2.2.3.6 abc
3.61.3.69 axy
Now I want the result as :
Country No_Visitors
xyz 2
abc 1
axy 1
I know how to do it by using 2 queries, get the unique country first and then again query the table for the country name. But how can I do it with single query.

use AGGREGATE FUNCTION COUNT() to get the total number of instances for each country.
SELECT Country, COUNT(IP)
FROM tableName
GROUP BY Country
SQLFiddle Demo

Related

Sql SELECT query just shows 1 row

So im trying to get a table that shows 3 activities which have the id's of 6,7,8 and they must be from a specific country. It should show their names and the number of times they appear in the person_activity_interval but the table i get is only 1 row with the name of 1 activity and the count of all the activities that are listed. If i remove other 2 activities it gets me the correct name and count but i need a table with all 3. Do i need to use something like join or union?
SELECT name,count(activity_id)
FROM activity,person_activity_interval
WHERE oib IN (SELECT oib
FROM person
WHERE living_id IN (SELECT id
FROM living
WHERE country= "Poland"))
AND ((activity_id=8 AND id =8) OR (activity_id=7 AND id =7) OR (activity_id=6 AND id =6));

Count and sum up all duplicate records in MySQL

I have table with, following structure.
id name
1 john
2 ana
3 john
4 ana
5 peter
6 ana
7 Abrar
8 Raju
Duplicate entries in the table are as follows
john(2) duplicate
ana(3) duplicate
The names which are duplicates are john and ana.
My question is how would I count the records in total which are duplicate here it is '5' records
Note : I also followed the similar question in community but it explains how we can add the number of duplicates exists for that particular name in the table and adds up the third column in table representing the duplicates records with same name but in my case I wanted to know the number of all duplicates exist in the table (here the result of the query is just number "5") irrespective of the names.
Just take a count subquery on the query you already have in mind (or perhaps have already written):
SELECT SUM(cnt) AS total_duplicates
FROM
(
SELECT COUNT(*) AS cnt
FROM yourTable
GROUP BY name
HAVING COUNT(*) > 1
) t;
Demo

Query: COUNT in Access To Only Count Unique Values

I have a table like so:
Customer Purchase Date Product
Frank 7/28/2015 Hammer
Bob 7/29/2015 Shovel
Bob 7/29/2015 Pickaxe
Bill 7/30/2015 Pliers
The Purchase Date field records a new entry for every purchase. So, if in one visit a customer purchases four items, my database creates four entries each with the same date.
I'm trying to write a query that displays the numbers of visits for each customer. Output like so:
Frank 1
Bob 1
Bill 1
But when I use the COUNT function on the date in my query, it returns:
Frank 1
Bob 2
Bill 1
I want my query to only count unique dates, but the COUNT function doesn't work. Everywhere I read, it also says that the SQL COUNT (Distinct) doesn't work in Access. Access help says that if I set the Query Properties to Unique Values "Yes", it should only return unique values, but it doesn't work. I tried Unique Record "Yes" also, but that didn't work either.
Please help! Thanks!
Try this:
select Cust, count(cust) as CustomerCount
from (Select Distinct Table1.Customer as cust, Table1.PurchaseDate
from Table1)
group by cust

how to have a Count when using Groupby?

I have got a column name country in which there are 3 entries which are shown below. In each countries i have set of people working in it in a different column. I want a count query which can count how people are working in each country in a single query.
country
________
India
America
China
try this:
SELECT country,COUNT(*)
FROM table
GROUP BY country;

Tricky multiple where condition MySQL Query

I have a table customer, a table with different criterias (These criterias are then used to make ratings). Another table has the values and its keys in it.
table company
==============
int company_id
varchar name
bool status
table criteria
==============
int criteria_id
varchar name
bool status
table company_criteria
==============
int company_id
int criteria_id
int criteria_value
varchar comments
Now i display all the criterias in the form of select boxes which will have themselves a value against each criteria (already in the DB). Now i want the user to be able to search for different companies who have those specific criteria and the stored value.
e.g: table customer has a record with id 1
table criteria has records
1--->Reputation, 2--> Salary
table company_criteria has following records:
company_id | criteria_id | criteria_value |
============================================
1 1 10
1 2 20
Now the user sees two select boxes (remember there are two records in the criteria table) - with different options. He selects 10 from first select box and 20 from the second select box. How would i write the Query - I tried the following
SELECT DISTINCT `co`.*
FROM (`company` co)
JOIN `company_criteria` cc ON `co`.`company_id` = `cc`.`company_id`
WHERE (`cc`.`criteria_id`=1 AND `cc`.`criteria_value`>=10) AND (`cc`.`criteria_id`=2 AND `cc`.`criteria_value`>=20)
It just doesn't work - gives me no results - always. Appreciate any help - thanks.
Probably you want to put and OR instead of the AND here
SELECT DISTINCT `co`.*
FROM (`company` co)
JOIN `company_criteria` cc ON `co`.`company_id` = `cc`.`company_id`
WHERE (`cc`.`criteria_id`=1 AND `cc`.`criteria_value`>=10) OR (`cc`.`criteria_id`=2 AND `cc`.`criteria_value`>=20)
between the 2 where conditions....they both cant be true at the same time I think