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 : - "select distinct categ from categs"
Now I want to order categories first by initial then by most frequent
I tried : - "select distinct categ
-
from categs order by mid(categ,1,1) , count(categs) desc"
it didn't work so I separated queries like : - "select categ ,
-
(select count(categ) from categs order by count(categ))
-
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?
Ahmed, what you need is GROUP BY statement along with COUNT aggregate function.
This should get you started. - SELECT categ, COUNT(categ) AS countCateg
-
FROM categs
-
GROUP BY categ
-
ORDER by mid(categ,1,1)
8 3159
First, retry your sort order like this: - ORDER BY TEST1234.TEST1 DESC, TEST1234.TEST2 DESC;
Put the ordertype after each field.
Try using a "WHERE" clause instead of DISTINCT to filter your records. Its better practice.
Ahmed, what you need is GROUP BY statement along with COUNT aggregate function.
This should get you started. - SELECT categ, COUNT(categ) AS countCateg
-
FROM categs
-
GROUP BY categ
-
ORDER by mid(categ,1,1)
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.
this is syntax for order by and distinct - select distinct columnname from tablename
-
order by columnname asc/desc
-
group by columnname
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).
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 :-)
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).
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 ...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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....
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |