Below shows my original MY SQL table.
SELECT name, COUNT(city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
I need following result.
SAM has 4 records. But there are city of London in two records.
I need How many different cities in SAM's Records.
I did lots of queries, but I couldn't create my SQL query.
Add DISTINCT to the count parameter:
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
Related
I know I can't have fields on Select not appearing on Group By. But may I go the other way? For example:
Select B
From Table
Group By A,B
It's possible.
Example
select MAX(salary) max_sal
from Employee
group by Department_id
I have two tables with identical schema. I want to get a count of all the people with a given surname in both tables, and have found I can do it like this:
SELECT surname, count(*) AS cnt
FROM
(
SELECT surname
FROM people.NorthKorea
UNION ALL
SELECT surname
FROM peopleGlobal.NorthKorea
) AS t
GROUP BY surname
ORDER BY cnt DESC
This is fine for small tables, but I have tables with up to 250 million rows, so was wondering if there may be a more efficient way of doing this? Such as INSERTING the result of the COUNT from one table into a table, and then updating / inserting (REPLACE?) the result of the COUNT on the second table.
N.B. I actually want to store the result of the COUNT on both tables in another table.
An index on the surname column should help a lot. I would try with this query, if there are a lot more rows than surnames I expect it to run faster:
SELECT surname, SUM(cnt)
FROM
(
SELECT surname, COUNT(*) as cnt
FROM people.NorthKorea
GROUP BY surname
UNION ALL
SELECT surname, COUNT(*) as cnt
FROM peopleGlobal.NorthKorea
GROUP BY surname
)
GROUP BY surname
ORDER BY cnt DESC
I have a table where I am trying to build a distinct list of all the cities with more than two occurrences in the table. I am trying the current query am am told "function count does not exist"? What am I doing wrong?
SELECT COUNT (city)
FROM `table1`
GROUP BY city
HAVING COUNT (city) >=2
Your query is correct you have given a space between COUNT and (City) it must be COUNT(City). that will work fine. Your query should be like this:
SELECT City, COUNT(city) Counts
FROM `table1`
GROUP BY City
HAVING COUNT(city) >=2;
See this SQLFiddle
USE ALIAS
SELECT COUNT(city) as SOME_TEXT FROM table1 GROUP BY city HAVING SOME_TEXT >=2
Is it possible to repeat the names of a database table's columns when executing a query whenever the GROUP BY info change? My query is group by a column.
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
GROUP BY city
ORDER BY name.fname;
I think you want this:
SELECT name.fname as First_Name,
name.sales as Sales,
name.city as City
FROM name
ORDER BY city, fname;
I have a relationship table such that it has
employeeID | storeID
What would be the query to find out which employees worked at more than one store?
SELECT employeeID WHERE ???
And possibly also list each different stores just once per employee...
Use group by and having, as in:
select employeeID, count(*) from table group by employeeID having count(distinct storeID) > 1
This will give you the employees working at more than one store. Use this as a subquery to list the stores for each such employee.
you can try -
select distinct employeeID,StoreID from table1
where storeID in
(
select storeID from table1 group by storeID having count(distinct employeeID) >1
)
cor storing count and showing store ID also in one query you can use following query..
select a.employeeID,a.storeID,b.cnt
from table1 a,
(select employeeID,count(*) cnt
from table1
group by employeeID
having count(distinct storeID) >1) b
where a.employeID=b.employeeid