I have table with columns like AutoID, Number, Name, City, State, Country.
What I wanted is the maximum number entered in the "Number" column with the combination of Name, City, State and Country.
Example:
Name City State Country
Smith NY NY USA
John NY NY USA
John NJ NY USA
Now smith should get "Number" 1, John 2, and again John(in NJ) 1 as he is the first from NJ.
I can simply put a where clause in query and get the max number + 1. But the problem is that when I have huge amount of data and the number of users increases, my query will be really slow. I am also inserting data in the same table so it will keep on piling.
I hope I have made my self clear.
Vipul Parekh
I am guessing that this is what you want:
Name City State Country Number
Smith NY NY USA 1
John NY NY USA 2
John NJ NY USA 1
This is provided by row_number():
select name, city,state, country,
row_number() over (partition by city, state, country order by (select null)) as Number
from table t;
Note that the sequencing of the Number within a group is arbitrary, because you don't provide an id or createdat column. There is no guarantee that it is in the same order as the table, because SQL tables are inherently unordered.
You can create a trigger on your table. whenever you insert a row it will count the number of rows and update the latest one with a number with the count + 1.
To get around slowness you can create a index on Name, City, State and Country.
Let me know if you need a sample code or some pointers.
You can Use ROW_NUMBER
SELECT * FROM
(
Select Name,
City,
State,
Country,
Row_Number() over (Partition by City, State, Country ORDER BY Name) AS Number
From table1
) AS T
WORKING FIDDLE
1)
WITH Optimize_CTE (id, cityid,stateid, countryid)
AS
-- Define the CTE query.
(
SELECT id, cityid,stateid, countryid
FROM Optimize
WHERE id IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT id,
convert(Varchar(255),cityid) as CityId,
convert(Varchar(255),stateid) as StateId,
convert(Varchar(255),countryid) as CountryId
FROM Optimize_CTE OPTION (MAXRECURSION 1)
DECLARE #RowsPerPage INT = 10, #PageNumber INT = 1
SELECT InvoiceID, CustomerID
FROM dbo.SalesInvoice
ORDER BY InvoiceID
OFFSET (#PageNumber-1)*#RowsPerPage ROWS
FETCH NEXT #RowsPerPage ROWS ONLY
GO
Related
I have this table:
first_name
last_name
age
country
John
Doe
31
USA
Robert
Luna
22
USA
David
Robinson
22
UK
John
Reinhardt
25
UK
Betty
Doe
28
UAE
How can I get only the names of the oldest per country?
When I do this query
SELECT first_name,last_name, MAX(age)
FROM Customers
GROUP BY country
I get this result:
first_name
last_name
MAX(age)
Betty
Doe
31
John
Reinhardt
22
John
Doe
31
But I want to get only first name and last name without the aggregate function.
If window functions are an option, you can use ROW_NUMBER for this task.
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY country ORDER BY age DESC) AS rn
FROM tab
)
SELECT first_name, last_name, age, country
FROM cte
WHERE rn = 1
Check the demo here.
It sounds like you want to get the oldest age per country first,
SELECT Country, MAX(age) AS MAX_AGE_IN_COUNTRY
FROM Customers
GROUP BY Country
With that, you want to match that back to the original table (aka a join) to see which names they match up to.
So, something like this perhaps:
SELECT Customers.*
FROM Customers
INNER JOIN
(
SELECT Country, MAX(age) AS MAX_AGE_IN_COUNTRY
FROM Customers
GROUP BY Country
) AS max_per_country_query
ON Customers.Country = max_per_country_query.Country
AND Customers.Age = max_per_country_query.MAX_AGE_IN_COUNTRY
If your database supports it, I prefer using the CTE style of handling these subqueries because it's easier to read and debug.
WITH cte_max_per_country AS (
SELECT Country, MAX(age) AS MAX_AGE_IN_COUNTRY
FROM Customers
GROUP BY Country
)
SELECT Customers.*
FROM Customers C
INNER JOIN cte_max_per_country
ON C.Country = cte_max_per_country.Country
AND C.Age = cte_max_per_country.MAX_AGE_IN_COUNTRY
I am trying to to do a count on all records that are similar.
Eg.
I have 2 records 1 is 1A Smith St and the other is 1 Smith St.
So this data needs to be returned/counted as 1.
Any assistance would be gratefully appreciated.
Thanks
Counting those where the (street, number) combination isn't unique.
SELECT COUNT(*) AS Total
FROM
(
SELECT Street, Number
FROM your_table
GROUP BY Street, Number
HAVING COUNT(*) > 1
) q
use
SELECT COUNT(1) AS NUMBER, Suffix, Street FROM TableName GROUP BY Suffix, Street
I need to identify rows where a certain value is repeated. Here is a sample table:
COUNTRY CITY
Italy Milan
Englad London
USA New York
Canada London
USA Atlanta
The query should return...
COUNTRY CITY
Englad London
Canada London
...because London is repeated. Thank you in advance for your help.
The easiest way is to use a subquery that counts the number of times each city appears (and filter to those values that appear more than once):
SELECT * FROM Cities
WHERE City in
(
SELECT City FROM Cities
GROUP BY City
HAVING COUNT(*) > 1
)
If your DBMS supports windowed aggregates.
SELECT COUNTRY,
CITY
FROM (SELECT COUNTRY,
CITY,
COUNT(*) OVER (PARTITION BY CITY) AS Cnt
FROM Cities) T
WHERE Cnt > 1
SQL Fiddle
select country, city
from aTable
where city in
(
select city
from aTable
group by city
HAVING count(1) > 1
)
Try it here: http://sqlfiddle.com/#!3/e9b1a/1
Or if the same city & country combo appears twice and you're only interested where the countries are different:
select distinct country, city
from aTable
where city in
(
select city
from aTable
group by city
HAVING count(distinct country) > 1
)
Try it here: http://sqlfiddle.com/#!3/2dfaa/2
This one works. Got it from my wife (she finally had time to look into this). Thought you might be interested.
SELECT * FROM Cities
WHERE City in ( select city
from (SELECT City,
count(distinct country)
FROM Cities
GROUP BY City
HAVING count(distinct country) > 1) a )
I'm trying to count the occurrences of a distinct set of cities and countries in a user table.
The table is set out similar to:
userid city country
------ --------- --------------
1 Cambridge United Kingdom
2 London United Kingdom
3 Cambridge United Kingdom
4 New York United States
What I need is a list of every city, country pair with the number of occurrences:
Cambridge, United Kingdom, 2
London, United Kingdom, 1
New York, United States, 1
Currently I run an SQL query to get the distinct pairs:
$array = SELECT DISTINCT city, country FROM usertable
then read it into an array in PHP, and loop through the array, running a query to count each occurrences for each row in the array:
SELECT count(*) FROM usertable
WHERE city = $array['city']
AND country = $array['country']
I'm assuming my scant grasp of SQL is missing something - what would be the correct way to do this, preferably without the intervention of PHP?
select city, country, count(*)
from usertable
group by city, country
What you need is a group by:
Select city, country, count(*) as counter
from usertable
group by city, country
SELECT cityandcountry, count(*) as occurrences FROM (
SELECT DISTINCT concat(city, country) FROM tablename
) as baseview;
if you want city and country preformated, or
SELECT cityandcountry, count(*) as occurrences FROM (
SELECT DISTINCT city, country FROM tablename
) as baseview;
if not.
I have a table with all U.S. zip codes. each row contains the city and state name for the zip code. I'm trying to get a list of cities that show up in multiple states. This wouldn't be a problem if there weren't X amount of zip codes in the same city...
So basically, I just want to the city in a state to count as 1 instead of it counting the city/state 7 times because there are 2+ zip codes in that city/state...
I'm not really sure how to do this. I know I need to use count but how do I tell the mysql to only count a given city/state combo as 1?
SELECT City, Count(City) As theCount
FROM (Select City, State From tblCityStateZips Group By City, State) As C
GROUP By City
HAVING COUNT Count(City) > 1
This would return all cities, with count, that were contained in more than one state.
Greenville 39
Greenwood 2
GreenBriar 3
etc.
First group on state and city, then group the result on city:
select City
from (
select State, City
from ZipCode
group by State, City
) x
group by City
having count(*) > 1
Will this do the trick
Select CityName, Count (Distinct State) as StateCount
From CityStateTable
Group by CityName
HAVING Count (Distinct State) > 1
Try using a select distinct
SELECT DISTINCT city, state FROM table GROUP BY city
You probably should have created a separate table for zip codes then to avoid the duplication.
You want to look into the GROUP BY Aggregate.