473,386 Members | 1,706 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,386 software developers and data experts.

SQL: DISTINCT and Count()

Hello every one,

I have a table of categories that have two columns product and categ , I used to have a simple query to select category :
Expand|Select|Wrap|Line Numbers
  1. "select distinct categ from categs"
Now I want to order categories first by initial then by most frequent
I tried :
Expand|Select|Wrap|Line Numbers
  1. "select distinct categ 
  2. from categs order by mid(categ,1,1) , count(categs) desc"
it didn't work so I separated queries like :
Expand|Select|Wrap|Line Numbers
  1. "select categ , 
  2. (select count(categ) from categs order by count(categ)) 
  3. from categs order by mid(categ,1,1)"
this query did the order but still repeating categories.
iam not sure where to put DISTINCT , I tried the above query as a part of another one that does distinct but it didn't work , any ideas?
Jan 17 '12 #1

✓ answered by Mariostg

Ahmed, what you need is GROUP BY statement along with COUNT aggregate function.

This should get you started.
Expand|Select|Wrap|Line Numbers
  1. SELECT categ, COUNT(categ) AS countCateg
  2. FROM categs 
  3. GROUP BY categ
  4. ORDER by mid(categ,1,1)

8 3159
C CSR
144 100+
First, retry your sort order like this:
Expand|Select|Wrap|Line Numbers
  1. ORDER BY TEST1234.TEST1 DESC, TEST1234.TEST2 DESC;
Put the ordertype after each field.
Jan 17 '12 #2
C CSR
144 100+
Try using a "WHERE" clause instead of DISTINCT to filter your records. Its better practice.
Jan 17 '12 #3
Mariostg
332 100+
Ahmed, what you need is GROUP BY statement along with COUNT aggregate function.

This should get you started.
Expand|Select|Wrap|Line Numbers
  1. SELECT categ, COUNT(categ) AS countCateg
  2. FROM categs 
  3. GROUP BY categ
  4. ORDER by mid(categ,1,1)
Jan 17 '12 #4
ahamed, here u had used in group by clause because where it is aggregate function is used that only. group by clause is what are columns are selected the table using select clause that all the columns are used in group by clause except the aggregate function columns.
Jan 17 '12 #5
this is syntax for order by and distinct
Expand|Select|Wrap|Line Numbers
  1. select distinct columnname from tablename 
  2. order by columnname asc/desc
  3. group by columnname
Jan 17 '12 #6
NeoPa
32,556 Expert Mod 16PB
mmabdulkadir:
this is syntax for order by and distinct
I'm afraid that aggregation (GROUP BY) and the DISTINCT predicate don't go together very well (for pretty obvious reasons if you think about it). If you want a count, then you certainly don't want DISTINCT.

Mario's solution illustrates exactly what is required (LEFT([X],1) may get the initial more straightforwardly - but that's nitpicking).
Jan 17 '12 #7
Mariostg
332 100+
what is required (LEFT([X],1) may get the initial more straightforwardly - but that's nitpicking).
I was going to leave the ORDER BY statement, but I am sure the OP will figure that one out :-)
Jan 17 '12 #8
NeoPa
32,556 Expert Mod 16PB
I'm glad you didn't, as it illustrates that you can sort by an item that is not included in the SELECT clause. Many don't realise that I find (because it's not so obvious when developing from the Access design view).
Jan 17 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Anne Heddal | last post by:
In the first line I'm trying to count the the unqiue values of the ID column, but I'm getting syntax error, any idea how to format the distinct count? SELECT Count(test.ID) AS IDCOUNT FROM...
2
by: Chris | last post by:
Hello all, I'm having some trouble setting up a query. Background: The table TBLSCREEN stores data about screenings of patients/subjects for eligibility to participate in a health study. A...
2
by: pierrelap | last post by:
Hello, I need to code a query that: 1-counts the number of time two companies have been in a deal together 2-in the five years that preceded the deal Lead Participant DealDate AAA BBB ...
5
by: JimmyKoolPantz | last post by:
for some reason I can't seem to figure this out. Situation: I'm using vb.net to create a query that will populate a dataset with zipcode and count that have unique first 3 digits. I want to...
1
by: Bill | last post by:
I'm trying to write a query that will select a distinct count of more than one field. I have records that display user productivity. Each of the records have a time associated with it and I want to...
0
by: NeoGeo | last post by:
I have a problem with a SQL SELECT query. As far as my research goes i figured out that UNIQUE is used when you have one column that you whant unique and DISTINCT is used when you have more than one...
6
by: leegold58 | last post by:
Hi, What is the SQL to count the number of null values in a specific column in a table? I knw this must be simple but I've been Googling this for a long time and have not the SQL to do this....
7
by: AccessHunter | last post by:
Hi, Please help with this problem. I have a report that displays a list of Case/Judge Records. using the following fields, Case Nbr, Case Seq Nbr, Judge Name, Attorney name 723187, 1...
1
by: KMEscherich | last post by:
Using Access 2003 Hi there, am wondering if someone can please help me with retrieving DISTINCT COUNT of a type field. I can get a total, which in this example would = 9, but I also need to know...
0
by: Deven Oza | last post by:
Hi All, I am changing measures property "sum" to "distinct count" but when I am looking at the browser to see the data, its not changing at all, the data value is same as I was getting before....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.