i've been using group concat and concat separately in order to concatenate rows or column. However, i want to concatenate now both column data as well as the row such as:
From these given table
id disease domain
-- ------- --------
1 Typhoid Davao City
2 Pox Davao City
3 Dengue Manila City
I want the sql to output this:
disease domain
------- --------------
Typhoid: 1, Pox: 1 Davao City
Dengue: 1 Manila City
The task was concatenate or group all the diseases reported in a city, then count how many reports for each disease for each city. I know it sounds useless, but it's only given to me as an assignment, and I'm lost.
SELECT GROUP_CONCAT(CONCAT_WS(' ',disease,total))
, domain
FROM
( SELECT disease
, domain
, COUNT(*) total
FROM my_table
GROUP
BY domain
, disease
) x
GROUP
BY domain;
Related
two table EMPLOYEE and Department
EMPLOYEE's fields are ID,Name, Salary ,DEPT_ID(foreign key to department table)
DEPARTMENT'S fields are id,NAME,LOCATION
VALUES OF EMPLOYEE TABLE WILL Be
Values OF DEPARTMENT TABLE WILL BE
Output from these table should be
DEPARTMENT_Name should be alpabetically within their count If are there same Count DEPARTMENT_Name should appear in alpabetically and count will be desc order
EMPLOYEE TABLE Values
id name salary dept_id
1 Candice 4685 1
2 Julia 2559 2
3 Bob 4405 4
4 Scarlet 2305 1
5 Ileana 1151 4
Department TABLE Values
id name location
1 Executive Sydney
2 Production Sydney
3 Resources Cape Town
4 Technical Texas
5 Management Paris
OUTPUT DATA SHOULD BE
DEPARTMENT_Name Count_OF_EMPLOYEE_SAME_DEPARTMENT
Executive 2,
Technical 2,
PRODUCTION 1,
MANAGEMENT 0,
RESOURCES 0
For what you want to show all departments even if there are no employees is a LEFT JOIN. So, start with the department table (alias "d" in the query) and LEFT JOIN to the employee table (alias "e"). using shorter alias names that make sense with context makes readability easier.
Now, you have the common "count()" which just returns a count for however many records are encountered, even if multiple in the secondary (employee) table based on common ID. In addition to count(), I also did a sum of the employee salary just for purposes that you can get multiple aggregate values in the same query.. Use it or don't, just wanted to present as an option for you.
Now the order. You want that based on the highest count first, so the COUNT(*) DESC (descending order) is the first sorting. Secondary is the department name to keep alphabetized if within the same count.
select
d.`name` Department_Name,
d.Location,
count(*) NumberOfEmployees,
sum( coalesce( e.salary, 0 )) as DeptTotalSalary
from
Department d
left join employee e
on d.dept_id = e.id
group by
d.`name`
order by
count(*) desc,
d.`name`
i.e. a table called students (Assume there is only one student with that name, e.g. there isnt two students called John Smith) with
studentName, studentScore, subject
assume table has this
John Smith 40 maths
bob grey 20 english
anne hank 23 english
John Smith 30 english
anne grey 10 maths
I am trying to find the most highest scored student by calculating the maximum of averages of all students
this here will select the highest average but not the student name with that average:
SELECT MAX(avgStudentScore)
FROM (SELECT AVG(studentScore) AS avgStudentScore FROM students GROUP BY studentName) t
thx
If all you need is the name, you can do something like this:
select studentName, avg(studentScore) as avgStudentScore
from students
group by studentName
order by avgStudentScore desc
limit 1
This will return only the first row of the query. Since it's ordered by avgStudentScore, it will return the student with the top average.
The above solution is the simplest and fastests, but it's not the only one. If you want to do it "the hard way" (with subqueries), what you need to do is:
Calculate the average for each student
Get the highest average
Filter the student with the highest average
So... let's do it the hard way ;)
select a.studentName
from
(
select studentName, avg(studentScore) as avgStudentScore
from students
group by studentName
) as a
where
a.avgStudentScore = (
select max(avgStudentScore)
from (
select avg(studentScore) as avgStudentScore
from students
group by studentName
) as a
)
Notice that with this approach the final result might not be unique (i.e. there may be one ore more students with the same average and that average is the highest).
Use this query:
SELECT studentName, max( mark ) as maxMark FROM `student` GROUP BY studentName
I have a list of names with country and city.
| COUNTRY | CITY | NAME | ID|
I need to find a count of each name grouped by country and city. I also need percentage of each name WITHIN each city/country. For example:
RESULTS
SELECT count(ID), country, city, name
from table NAME
GROUP BY country, city, name
How do I calculate percentage of each name WITHIN each city/country?
It looks like your query doesn't compute the count of each name grouped by city and state but rather the count of each id grouped by name, city and country. Assuming this is what you wanted, then the percents might be calculated like this:
SELECT count(N.ID) as NameCount
, count(N.ID) * 100.0 / (SELECT COUNT(*) FROM NAME WHERE Country = N.Country AND City = N.City) as NamePercent
, N.Name
, N.Country
, N.City
FROM NAME N
GROUP BY N.country, N.city, N.Name
Here's a test fiddle I've created for it: http://sqlfiddle.com/#!2/875026/1/0
For the second question - from the comments,
How to calculate AVE of name occurance across different cities grouped by a country. For example: count of Mikes in NY-2, Chicago-4, LA-5. So ave for Mike in US is 3.6
You might do something like this:
SELECT Name
, Country
, AVG(NameCount) NameAvgAcrossCountry
FROM
(SELECT count(N.ID) as NameCount
, count(N.ID) * 100.0 / (SELECT COUNT(*) FROM NAME WHERE Country = N.Country AND City = N.City) as NamePercent
, N.Name
, N.Country
, N.City
FROM NAME N
GROUP BY N.country, N.city, N.Name) NamesQuery
GROUP BY Name, Country
I have a table of flights, which have an origin and destination city, represented as a foreign id.
A very simplified example of this table looks like:
id | origin | destination
023 1 3
044 3 2
332 2 1
509 1 3
493 1 4
I need to get the first time that a city shows up as an origin or a destination; a list of all the flights that contain a city that hasn't been flown to or from yet.
What I would like to get for the above example would be:
023: 1, 3
044: 2
493: 4
Flights 332 and 509 aren't in the output because they only visit cities that have already been visited.
Here's what I've tried:
(SELECT distinct(origin), distinct(destination) FROM flights ORDER BY id)
Doesn't work because you can't select more than one distinct column
SELECT (distinct(origin) FROM flights ORDER BY id) UNION (distinct (destination) FROM flights ORDER BY id)
Doesn't work because of syntax errors, but mainly because it doesn't take into account that a city should be unique in the origin and destination columns.
If there's not a quick way to do this in SQL I'm also happy to just iterate through and keep track of cities that have been visited (this app has literally one user, and he doesn't care about a few milliseconds of computation because he's over 80), but I'd love to know just so that I can learn more about SQL!
This does it:
SELECT id, GROUP_CONCAT(city ORDER BY city) cities
FROM (
SELECT city, min(id) id
FROM (
SELECT origin city, MIN(id) id
FROM flights
GROUP BY city
UNION
SELECT destination city, MIN(id) id
FROM flights
GROUP BY city) u
GROUP BY city) x
GROUP BY id
DEMO
I have a table which stores IDs and the city where the store is located.
I want to list all the stores starting with the stores that are in the city where there are the most stores.
TABLE
ID CITY
1 NYC
2 BOS
3 BOS
4 NYC
5 NYC
The output I want is the following since I have the most stores in NYC, I want all the NYC location to be listed first.
1 NYC
4 NYC
5 NYC
2 BOS
3 BOS
SELECT count(City), City
FROM table
GROUP BY City
ORDER BY count(City);
OR
SELECT count(City) as count, City
FROM table
GROUP BY City
ORDER BY count;
Ahh, sorry, I was misinterpreting your question. I believe Peter Langs answer was the correct one.
This one calculates the count in a separate query, joins it and orders by that count (SQL-Fiddle):
SELECT c.id, c.city
FROM cities c
JOIN ( SELECT city, COUNT(*) AS cnt
FROM cities
GROUP BY city
) c2 ON ( c2.city = c.city )
ORDER BY c2.cnt DESC;
This solution is not a very optimal one so if your table is very large it will take some time to execute but it does what you are asking.
select c.city, c.id,
(select count(*) as cnt from city c2
where c2.city = c.city) as order_col
from city c
order by order_col desc
That is, for each city that you come across you are counting the number of times that that city occurs in the database.
Disclaimer: This gives what you are asking for but I would not recommend it for production environments where the number of rows will grow too large.
SELECT `FirstAddressLine4`, count(*) AS `Count`
FROM `leads`
WHERE `Status`='Yes'
AND `broker_id`='0'
GROUPBY `FirstAddressLine4`
ORDERBY `Count` DESC
LIMIT 0, 8