SQL COUNT only display results once - sql

My simple query is as follows in SQL Server Management Studio 2012:-
SELECT Last_Name, City,
(SELECT COUNT (City) FROM Customers IX WHERE IX.City = EX.City) as counting
FROM Customers EX
GROUP BY City, Last_Name
ORDER BY City
Outputs this:-
Last_Name City Counting
Joe London 3
Smith London 3
Carter London 3
Stones New York 3
Jones New York 3
White New York 3
But I would like it to not repeat the counts for the same city and output like this:-
Last_Name City Counting
Joe London 3
Smith London
Carter London
Stones New York 3
Jones New York
White New York
How would I achieve this please?

Seems like a very odd request, and easily done on the application side. But it is not that hard in SQL:
SELECT Last_Name, City,
(case when row_number() over (partition by Last_Name, City order by (select NULL)) = 1
then (SELECT COUNT(City) FROM Customers IX WHERE IX.City = EX.City) as counting
end) as Counting
FROM Customers EX
GROUP BY City, Last_Name
ORDER BY City;
This will put the value on one row for each city. It is not determinate which one, but would probably be the first in practice. You can guarantee this by using a stable sort:
SELECT Last_Name, City,
(case when row_number() over (partition by Last_Name, City order by Last_Name) = 1
then (SELECT COUNT(City) FROM Customers IX WHERE IX.City = EX.City) as counting
end) as Counting
FROM Customers EX
GROUP BY City, Last_Name
ORDER BY City, Last_Name

Related

which command should i use on SQL for having?

I want your help.
I have 2 columns with data, customer and city.
I want to show customers a mix if this customer has more different cities, and I want to show the customer if it has only 1 city.
for example, I have this data:
customer city
ana London
Ella London
Sarah Paris
Haidi Greece
Chloe France
ana London
Ella france
I want to show it like this:
Ana London
Ella Mix
Sarah Paris
Haidi Greece
Chloe France
How could I do this? Which command should I use?
Here you go.
Select
Customer,
case when count(DISTINCT city) > 1 then 'MIX' Else max(City) End as City
from MyTable
Group by Customer
You could use a command like before:
Select
Customer,
case when count(city) > 1 then 'MIX' Else City End as City
from Mytable
Group by Customer, City
but Ana is present twice in the table, and you have to use the Max to solve
Select
Customer,
case when count(city) > 1 then 'MIX' Else MAX(City) End as City
from Mytable
Group by Customer
If you wanted NULL instead of 'Mix', you could use:
select customer, nullif(min(city), max(city)) as city
from t
group by customer;
You can actually extend this to get 'Mix':
select customer, coalesce(nullif(min(city), max(city)), 'Mix') as city
from t
group by customer;
But NULL makes sense to me.
Here's what I think you're looking for
select customer,
case when count(*)>1
then 'mix'
else max(city) end as city
from t
group by customer;

I'd like some help to write sql code to return a list of customer data items ranked by frequency (high to low)

The table I am querying has several thousand rows and numerous fields - I'd like the code to return the top 10 values for a handful of the fields, namely: Forename, Surname and City - I'd also like to see a count of the values returned.
For example
Ranking
Forename
FName Frequency
Surname
SName Frequency
City
City Frequency
1
Liam
830,091
Smith
2,353,709
New York
2,679,785
2
Mary
708,390
Johnson
1,562,990
Los Angeles
413,359
3
Noah
639,592
Williams
792,306
Chicago
393,511
4
Patricia
568,410
Brown
743,346
Houston
367,496
5
William
557,049
Jones
633,933
Phoenix
336,929
6
Linda
497,138
Miller
503,523
Philadelphia
304,638
7
James
490,665
Davis
503,115
San Antonio
255,142
8
Barbara
418,312
Garcia
468,683
San Diego
238,521
9
Logan
399,947
Rodriguez
461,816
Dallas
232,718
10
Elizabeth
399,737
Wilson
436,843
San Jose
213,483
The returned list should be interpreted thus:
The most frequently occurring forename in the table is Liam - with 830,091 instances,
The 5th most frequently occurring forename is William - with 557,049 instances,
The 8th most frequently occurring city is San Diego - with 238,521 instances
...and so on
(N.b. the table does not show there are 2.7m Liams in New York - just that there are 830,091 Liams in the entire table - and that there are 2,679,785 New York addresses in the entire table)
The following produces what I need - but just for the first field (Forename) - I'd like to be able to do the same for three fields
SELECT Forename, COUNT(Forename) AS FName_Frequency
FROM Customer_Table
GROUP BY Forename
ORDER BY FName_Frequency DESC
limit 10
Thanks in anticipation
I would just put this in separate rows:
select 'forename', forename, count(*) as freq
from customer_table
group by forename
order by freq desc
fetch first 10 rows only
union all
select 'surname', surname, count(*) as freq
from customer_table
group by surname
order by freq desc
fetch first 10 rows only
union all
select 'city', city, count(*) as freq
from customer_table
group by city
order by freq desc
fetch first 10 rows only;
Note that this uses Standard SQL syntax, because you have not tagged with the question with the database you are using. You can also put this in separate columns, using:
select max(case when which = 'forename' then col end),
max(case when which = 'forename' then freq end),
max(case when which = 'surname' then col end),
max(case when which = 'surname' then freq end),
max(case when which = 'city' then col end),
max(case when which = 'city' then freq end)
from ((select 'forename' as which, forename as col, count(*) as freq,
row_number() over (order by count(*) desc) as seqnum
from customer_table
group by forename
) union all
(select 'surname' as which, surname, count(*) as freq
row_number() over (order by count(*) desc) as seqnum
from customer_table
group by surname
) union all
(select 'city', city, count(*) as freq,
row_number() over (order by count(*) desc) as seqnum
from customer_table
group by city
)
) x
group by seqnum;

SQL ordering cities ascending and persons descending

I have been stuck in complicated problem. I do not know the version of this SQL, it is school edition. But it is not relevant info now anyway.
I want order cities ascending and numbers descending. With descending numbers I mean when there is same city couple times it orders then biggest number first.
I also need row numbers, I have tried SELECT ROW_NUMBER() OVER(ORDER BY COUNT(FIRST_NAME)) row with no succes.
I have two tables called CUSTOMERS and EMPLOYEES. Both of them having FIRST_NAME, LAST_NAME, CITY.
Now I have this kind of code:
SELECT
CITY, COUNT(FIRST_NAME),
CASE WHEN COUNT(FIRST_NAME) >= 0 THEN 'CUSTOMERS'
END
FROM CUSTOMERS
GROUP BY CITY
UNION
SELECT
CITY, COUNT(FIRST_NAME),
CASE WHEN COUNT(FIRST_NAME) >= 0 THEN 'EMPLOYEES'
END
FROM EMPLOYEES
GROUP BY CITY
This SQL code gives me list like this:
CITY
NEW YORK 2 CUSTOMERS
MIAMI 1 CUSTOMERS
MIAMI 4 EMPLOYEES
LOS ANGELES 1 CUSTOMERS
CHIGACO 1 CUSTOMERS
HOUSTON 1 CUSTOMERS
DALLAS 2 CUSTOMERS
SAN JOSE 2 CUSTOMERS
SEATTLE 2 CUSTOMERS
SEATTLE 5 EMPLOYEES
BOSTON 1 CUSTOMERS
BOSTON 3 EMPLOYEES
I want it look like this:
ROW CITY
1 NEW YORK 2 CUSTOMERS
2 MIAMI 4 EMPLOYEES
3 MIAMI 1 CUSTOMERS
4 LOS ANGELES 1 CUSTOMERS
5 CHIGACO 1 CUSTOMERS
6 HOUSTON 1 CUSTOMERS
7 DALLAS 2 CUSTOMERS
8 SAN JOSE 2 CUSTOMERS
9 SEATTLE 5 EMPLOYEES
10 SEATTLE 2 CUSTOMERS
11 BOSTON 3 EMPLOYEES
12 BOSTON 1 CUSTOMERS
You can use window functions in the ORDER BY:
SELECT c.*
FROM ((SELECT CITY, COUNT(*) as cnt, 'CUSTOMERS' as WHICH
FROM CUSTOMERS
GROUP BY CITY
) UNION ALL
(SELECT CITY, COUNT(*), 'EMPLOYEES'
FROM EMPLOYEES
GROUP BY CITY
)
) c
ORDER BY MAX(cnt) OVER (PARTITION BY city) DESC,
city,
cnt DESC;

Pseudo to SQL? All Records where FirstName & LastName Is duplicated but City is different?

I need to find all records that the FIRSTNAME is a duplicate and the LASTNAME is a duplicate but the city is different for the records where the names are duplicate.
So my data looks like this:
FirstName LastName CustomerFileLocation City
----------------------------------------------------------------------
Joe Smith c:\file1\File1.txt Dallas
Joe Jones c:\File2\File1.txt New York
Joe Smith c:\File3\File1.txt New Mexico City
Harry Smith c:\File4\File1.txt Boca Raton
Joe Smith c:\File3\File1.txt Dallas
Michael Smith c:\File1\File1.txt Dallas
I want the query to return
Joe Smith c:\file1\File1.txt Dallas
Joe Smith c:\File3\File1.txt New Mexico City
I wrote the following to find the matching FirstName and LastName. But I am not sure how to say "and City doesn't match"
SELECT
dbo.TblFileCache.FirstName, dbo.TblFileCache.LastName,
dbo.TblFileCache.ClaimFilePath, dbo.TblFileCache.Skip
FROM
dbo.TblFileCache
INNER JOIN
(SELECT
FirstName, LastName, COUNT(*) AS CountOf
FROM
dbo.TblFileCache AS tblFileCache_1
GROUP BY
FirstName, LastName
HAVING
(COUNT(*) > 1)) AS dt ON dbo.TblFileCache.FirstName = dt.FirstName
AND dbo.TblFileCache.LastName = dt.LastName
WHERE
(dbo.TblFileCache.Skip = 0)
ORDER BY
dbo.TblFileCache.FirstName, dbo.TblFileCache.LastName
To get all the rows in your original data where one set of user names has multiple cities, you can use window functions:
select t.*
from (select t.*,
min(city) over (partition by FirstName, LastName) as mincity,
max(city) over (partition by FirstName, LastName) as maxcity
from dbo.TblFileCache t
) t
where mincity <> maxcity;
If you want one row per city, you can do an aggregation on top of this:
select FirstName, LastName, min(CustomerFileLocation) as CustomerFileLocation, city
from (select t.*,
min(city) over (partition by FirstName, LastName) as mincity,
max(city) over (partition by FirstName, LastName) as maxcity
from dbo.TblFileCache t
) t
where mincity <> maxcity
group by FirstName, LastName, City;

SELECT clause with same data using DISTINCT

Example in my employee table i having these data
//EMPLOYEE
E# NAME CITY
--------------------------
1 JOHN ENGLAND
2 SITI ENGLAND
3 JESS GERMANY
4 HOLY BOSTON
When i run this statement:
SELECT DISTINCT CITY, COUNT(E#) as "Total Employee" FROM EMPLOYEE Where CITY=;
what should i insert the value for
//CITY=?
in order to get the result like this
CITY TOTAL EMPLOYEE
----------------------------
ENGLAND 2
GERMANY 1
BOSTON 1
isn't impossible to use group by or having clause?
ANSWER FIXED ! THANKS
Well, you're not excluding any cities, so you don't need a WHERE clause at all! But you DO need a GROUP BY to do a count. You can just do:
SELECT CITY, COUNT(E#) as "Total Employee"
FROM EMPLOYEE
GROUP BY CITY
However if you only want to include those three cities (even if more are added to the underlying data), you can do:
SELECT CITY, COUNT(E#) as "Total Employee"
FROM EMPLOYEE
WHERE CITY IN ('ENGLAND', 'GERMANY', 'BOSTON')
GROUP BY CITY