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;
Related
My database has a table named customer with data in the following columns: id, first_name, last_name, and city.
Like this:
Now I need a table with the number of customers in each Distinct city. what should the SQL query for it?
You can use COUNT aggregate function -
SELECT city, COUNT(ID)
FROM customer
GROUP BY city;
In my mySQL project I need to get a result of 3 columns: name, items, items_with_condition. The data are stored in a single table (myTable).
It's easy get the result of name, items.
SELECT name, COUNT(DISTINCT pid) AS items
FROM myTable
GROUP BY name
Also easy to get the result for name, items_with_condition.
SELECT name, COUNT(DISTINCT pid) AS items_with_condition
FROM myTable
WHERE someColumn='something'
GROUP BY name
But now I am unable to combine these two results into one table with name, items, items_with_condition columns using only SQL. Could you please help me? Thanks a lot!
You can use conditional aggregation:
SELECT
name
, COUNT(DISTINCT pid) AS items
, COUNT(DISTINCT CASE
WHEN someColumn='something' THEN pid
END) AS items_with_condition
FROM myTable
GROUP BY name
So I have a table with the following columns: id, Fname, Lname, city and desc
What I am trying to accomplish is to create a function that counts how many employees are working in each city.
Normally I would just use something like
select city, count(*) as NumofEmp from employee group by city
but at the moment I have to create a function for this. How could I accomplish this?
select city, count(name) as NumofEmp from employee group by city
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;
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