In my database I can easily see the last 50 people in my table with state=NY and postcode=12405 and I can look down the table to see if they have a dog.
SELECT state, postcode, havedog
FROM address
WHERE (state=NY) AND (postcode=12405)
ORDER BY surname DESC
LIMIT 0 , 5
Which returns
state postcode havedog
NY 12405 yes
NY 12405 yes
NY 12405 no
NY 12405 no
NY 12405 yes
I can also count how many people there are in my whole database that have a dog where state=NY and postcode=12405
SELECT state, postcode COUNT(havedog) AS DOG
FROM address
WHERE (state=NY AND postcode=12405 ) AND (havedog=yes)
But I can't work out how to limit the count so it only counts the last 5 records where state=NY and postcode=12405 and havedog=yes, is it possible to apply a limit to the Count function?
eg given the table above, the result of my count should be 3.
Use a subselect:
SELECT COUNT(*) FROM
(
SELECT state, postcode, havedog
FROM address
WHERE state = 'NY' AND postcode = 12405
ORDER BY surname DESC
LIMIT 5
) T1
WHERE havedog = 1
You can do this:
SELECT
state,
postcode
COUNT(havedog) AS DogCounts
FROM
(
SELECT state, postcode, havedog
FROM address
WHERE (state=NY) AND (postcode=12405)
ORDER BY surname DESC
LIMIT 50
) t
GROUP BY state, postcode
SELECT top 50 state, postcode, COUNT(havedog) OVER () AS DOG
FROM address
WHERE (state='NY' AND postcode=12405 ) AND (havedog='yes')
Related
I have a event_table as below.
city country event date
london UK Soccer 8/20/2010
toronto CA Basketball 8/12/2011
newyork US Basketball 8/21/2012
LA US Basketball 8/30/2013
.....
I need to get all fields with these conditions:
30 entries only
event = "Baseketball"
order by date
Distinct Country "US".
I tried below:
select distinct on country *
from event_table
where event="Basketball"
order by date Dec limit 20;
and below:
select *
from event_table
where event="Basketball"
order by date Dec limit 20
group by country;
But none working. Thanks for the help in advance.
Fro only one country it is enough to make
SELECT *
FROM event_table
WHERE event="Basketball"
AND country = 'US'
ORDER BY `date` DESC
LIMIT 30
If you want more counties use
SELECT
city, country, event, `date`
FROM
(SELECT *
,ROW_NUMBER() OVER(PARTITION BY county ORDER BY `date` DESC) rn
FROM event_table
WHERE event="Basketball") t1
WHERE rn <= 30
You can then select the countrie you like
I'm trying to display only zipcodes that multiple people have in my table and sort it by ascending order.
I have tried the following but it does not work.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY ASC;
SELECT zipcode AS "zipcodes", count(1) as ZIP_COUNT
FROM people
GROUP BY zipcode
ORDER BY ZIP_COUNT ASC;
If you only need zipcode that appears 2+ times append (before the order) :
HAVING COUNT(1) > 1
Or (if Mysql supports aliases in HAVING clause) :
HAVING ZIP_COUNT > 1
The main issue with your query is that you didn't specify any column in the order by sections.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY ASC;
So just add the zipcode to it and it will be good.
SELECT zipcode AS "zipcodes" FROM people GROUP BY zipcode ORDER BY zipcode ASC;
The following query selects zipcode(s) having multiple entries in table "people":
SELECT zipcode AS "zipcodes"
FROM people
GROUP BY zipcode
HAVING COUNT(*) > 1
ORDER BY zipcode ASC;
try this one
SELECT zipcode AS "zipcodes", count(zipcode) as noofzip
FROM people
GROUP BY zipcode
having count(zipcode)>1
order by zipcode
I'm new to mysql and I have two correctly working select statements with longest and shortest names. I can't really understand how can I join them in one statement. Can someone help, please.
Here are the queries:
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength
FROM country
ORDER BY CHAR_LENGTH(name) DESC, name
LIMIT 1;
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength
FROM country
ORDER BY CHAR_LENGTH(name), name
LIMIT 1;
The result I'm looking for is:
Country NameLength
----------------------------------------------------------
South Georgia and the South Sandwich Islands 44
Chad 4
Cuba 4
You use UNION:
SELECT Country, NameLength FROM (
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength FROM country LIMIT 1;
UNION
SELECT name AS Country, CHAR_LENGTH(name) AS NameLength FROM country LIMIT 1; )a
ORDER BY NameLength DESC, Country
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 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