473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Count unique values by group

All,

I have this table:

ClientName City
Ram Mumbai
Ram Cochin
Ram Mumbai
Ram Bangalore
Lakhan Mumbai
Lakhan Bangalore
Lakhan Mumbai

I want to give the output as:
Name Count of Unique Cities
Ram 3
Lakhan 2

Please help!!!!!

I tried the following:

SELECT DISTINCT tTemp.ClientName, Count(tTemp.City) AS Expr1
FROM tTemp
GROUP BY tTemp.ClientName;
It gave me duplicate values too of the city wher eit was repeasted..
pelase help!

thanks a lot!

May 2 '06 #1
3 19166
> ClientName City
Ram Mumbai
Ram Cochin
Ram Mumbai
Ram Bangalore
Lakhan Mumbai
Lakhan Bangalore
Lakhan Mumbai

I want to give the output as:
Name Count of Unique Cities
Ram 3
Lakhan 2

I tried the following:

SELECT DISTINCT tTemp.ClientName, Count(tTemp.City) AS Expr1
FROM tTemp
GROUP BY tTemp.ClientName;
It gave me duplicate values too of the city wher eit was repeasted..


SELECT
MIN(ClientName) AS MyClient,
COUNT(City) AS NumCity
FROM (SELECT DISTINCT ClientName, City FROM tbl03)
GROUP BY ClientName

--
PBsoft di Gabriele Bertolucci
www.pbsoft.it
skype: pbsoftsolution
May 2 '06 #2
Hello Mihir,

Try this - just change the table name to your table

SELECT clientname, Count(t1.city) AS CountOfcity
FROM [select clientname, city from tblclients group by clientname,
city]. AS t1
group by clientname Order By ClientName desc
Rich

*** Sent via Developersdex http://www.developersdex.com ***
May 2 '06 #3
Rich,

Thanks a lot!~!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Mihir

May 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael Krzepkowski | last post by:
All, I have a view that returns the following values: Item Vendor 70807 1234 70807 5678 If I am looking for items that have more...
2
by: SJM | last post by:
I have a report that displays records of real estate properties. It is possible for each property to appear a number of times for various reasons. Each record however is unique. What I would like...
3
by: Abhi | last post by:
Hi! I am wondering if this query is possible somehow: I have a table with many fields that all can have a value from 1 to 5. if I wanna see the count of each value from one field, then this...
2
by: john | last post by:
I have a table with 5 fields. In a query I would like to make a count for every unique combination of field 2,3, and 4. I fiddled with the count and dcount option, but can't get it to work for more...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
newnewbie
by: newnewbie | last post by:
Desperately need help in creating a query to count unique values in a table. I am a Business analyst with limited knowledge of Access….My boss got me ODBC connection to the underlying tables for our...
5
by: Dean | last post by:
Hi, I have a table with non-unique identifiers. I need to take all the values with the same ID's and combine them into one field with a semicolon as a seperator. These values may exceed 255...
1
by: dez5000 | last post by:
I'm trying to get a report by location that would list the number of visits to the location for the month but also count the number of unique visitors to that location. I have a table of data with...
3
by: alienz747 | last post by:
Hi, I have a query which is supposed to count distinct fields but it doesn't work properly. It does not count the unique values of the PayrollNumber. It counts all values. Any ideas why? Thanks....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.