473,386 Members | 1,766 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.

TSQL: I want to use a SELECT statement with COUNT(*) AS 'name' and ORDER BY 'name'

I am very new to Transact-SQL programming and don't have a programming
background and was hoping that someone could point me in the right
direction. I have a SELECT statement SELECT FIXID, COUNT(*) AS IOIs
and want to ORDER BY 'IOI's'. I have been combing through the BOL, but
I don't even know what topic/heading this would fall under.

USE INDII
SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY FIXID

I know that it is a simple question, but perhaps someone could assist
me.

Thanks,

Jul 23 '05 #1
18 9728
Try ORDER BY COUNT(*) instead of ORDER BY FIXID.

-Tom.

Jul 23 '05 #2
This statement as written may not give you what your looking for.
Basically its going to return the number of rows in your table. that's
it. What are you trying to count?

Jul 23 '05 #3
Tom,

I modified my query as per my understanding of your suggestion e.g.

USE INDII
SELECT COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
ORDER BY COUNT(*)

The above example works, it returns a total of all IOIs. That would be
fine except that I am trying to see a total number of IOIs for each
'FIXID' sorted by largest number of IOI's then FIXID.

Jul 23 '05 #4
Your original message said:

SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY FIXID

In your last message you removed the GROUP BY clause. I'm not sure why
both you and Dan assumed that I wanted that removed. Leave your
statement as is, but replace the ORDER BY:

SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY COUNT(*)

-Tom.

Jul 23 '05 #5
Tom,

Your suggestion worked.

Thanks.

Jul 23 '05 #6
On 3 Mar 2005 07:15:19 -0800, wa*********@yahoo.com wrote:
I am very new to Transact-SQL programming and don't have a programming
background and was hoping that someone could point me in the right
direction. I have a SELECT statement SELECT FIXID, COUNT(*) AS IOIs
and want to ORDER BY 'IOI's'. I have been combing through the BOL, but
I don't even know what topic/heading this would fall under.

USE INDII
SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY FIXID

I know that it is a simple question, but perhaps someone could assist
me.


Hi war_wheelan,

Though the alias you assign to a column in the SELECT clause can't be
used in most of the other parts of a query, the ORDER BY clause is an
exception: here, you can refer to an alias. So you could just use

SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY IOIs

Note that ORDER BY COUNT(*) (as suggested by Thomas) works just as well,
but is less portable. ANSI standard SQL doesn't allow expressions in the
ORDER BY clause. T-SQL does allow this, and I use it often to get
orderings that are otherwise hard to achieve, but I always choose the
ANSI-standard version if that works as well.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #7

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:eu********************************@4ax.com...
On 3 Mar 2005 07:15:19 -0800, wa*********@yahoo.com wrote:
I am very new to Transact-SQL programming and don't have a programming
background and was hoping that someone could point me in the right
direction. I have a SELECT statement SELECT FIXID, COUNT(*) AS IOIs
and want to ORDER BY 'IOI's'. I have been combing through the BOL, but
I don't even know what topic/heading this would fall under.

USE INDII
SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY FIXID

I know that it is a simple question, but perhaps someone could assist
me.


Hi war_wheelan,

Though the alias you assign to a column in the SELECT clause can't be
used in most of the other parts of a query, the ORDER BY clause is an
exception: here, you can refer to an alias. So you could just use

SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY IOIs

Note that ORDER BY COUNT(*) (as suggested by Thomas) works just as well,
but is less portable. ANSI standard SQL doesn't allow expressions in the
ORDER BY clause. T-SQL does allow this, and I use it often to get
orderings that are otherwise hard to achieve, but I always choose the
ANSI-standard version if that works as well.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

I am not sure of its ANSI-ness or portability,
but I have specified the column index of a result set in some of my order by
's

SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY 2

Jul 23 '05 #8

Hugo Kornelis wrote:
[...] ANSI standard SQL doesn't allow expressions in the
ORDER BY clause. T-SQL does allow this, and I use it often to get
orderings that are otherwise hard to achieve, but I always choose the
ANSI-standard version if that works as well.


Actually, ORDER BY expression has been in the ANSI/ISO SQL standard for
6 years, since SQL-99.
Regards,
Jarl

Jul 23 '05 #9
On Fri, 04 Mar 2005 02:47:36 GMT, David Rawheiser wrote:

Hi David,

(snip)
I am not sure of its ANSI-ness or portability,
Adhering to ANSI standards will always improva portablility.
but I have specified the column index of a result set in some of my order by
's

SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY 2


ANSI permits you to use either the name or the ordinal position of a
column in the result set in the ORDER BY clause. Using a column name is
not permitted, unless that column appears in the SELECT list without
alias (since in this case, the name of the column in the result set will
be equal to that column's name). Using an expression is never permitted.

Note that technically, the ORDER BY is not part of the SELECT statement,
but a cursor operation. In theory, the SELECT statement returns a set
(which is unordered by definition); the ORDER BY reads that set (and
nothing else!) in temp storage, sorts it, then outputs the rows in the
sorted order. Any DBMS that strictly follows this theoretical order can
never sort on anything that's not in the result set. But real products
may (and will) change the order for optimization. SQL Server doesn't
only optimize, it also allows ordering on expressions and on columns
that are not included in the SELECT clause.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #10
On 4 Mar 2005 00:22:17 -0800, ja**@mimer.com wrote:

Hugo Kornelis wrote:
[...] ANSI standard SQL doesn't allow expressions in the
ORDER BY clause. T-SQL does allow this, and I use it often to get
orderings that are otherwise hard to achieve, but I always choose the
ANSI-standard version if that works as well.


Actually, ORDER BY expression has been in the ANSI/ISO SQL standard for
6 years, since SQL-99.


Hi Jarl,

As far as I know, SQL-99 is an ISO standard only, not (yet?) adopted by
the ANSI committee.

I'm sure that Joe Celko will correct me if I'm wrong.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #11

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:nh********************************@4ax.com...
On Fri, 04 Mar 2005 02:47:36 GMT, David Rawheiser wrote:

Hi David,

(snip)
I am not sure of its ANSI-ness or portability,


Adhering to ANSI standards will always improva portablility.
but I have specified the column index of a result set in some of my order
by
's

SELECT FIXID, COUNT(*) AS IOIs
FROM [dbo].[IOI_2005_03_03]
GROUP BY FIXID
ORDER BY 2


ANSI permits you to use either the name or the ordinal position of a
column in the result set in the ORDER BY clause. Using a column name is
not permitted, unless that column appears in the SELECT list without
alias (since in this case, the name of the column in the result set will
be equal to that column's name). Using an expression is never permitted.

Note that technically, the ORDER BY is not part of the SELECT statement,
but a cursor operation. In theory, the SELECT statement returns a set
(which is unordered by definition); the ORDER BY reads that set (and
nothing else!) in temp storage, sorts it, then outputs the rows in the
sorted order. Any DBMS that strictly follows this theoretical order can
never sort on anything that's not in the result set. But real products
may (and will) change the order for optimization. SQL Server doesn't
only optimize, it also allows ordering on expressions and on columns
that are not included in the SELECT clause.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Thanks ...
So in summary
if I want to be portable on a query i want ordered by an expression,
use the column index in the order by clause


Jul 23 '05 #12

Hugo Kornelis wrote:

As far as I know, SQL-99 is an ISO standard only, not (yet?) adopted by the ANSI committee.

I'm sure that Joe Celko will correct me if I'm wrong.

Best, Hugo


Hugo,

This is quite confusing.

If you go to the ANSI web store, http://webstore.ansi.org/ansidocstore,
and search for SQL, you'll find that the SQL-99 documents are named
something like INCITS/ISO/IEC 9075-5-1999 etc, all with a note
"formerly ANSI/ISO/IEC 9075-5-...". As you can see the ANSI part of the
label has been replaced with INCITS. (Whatever that means.)

And in the list these documents are labelled with the ANSI logo. I
don't know what that means... But I do know that ANSI is a member of
the ISO SQL Standards Committee.
Regards,
Jarl

Jul 23 '05 #13
On Fri, 04 Mar 2005 12:45:24 GMT, David Rawheiser wrote:
Thanks ...
So in summary
if I want to be portable on a query i want ordered by an expression,
use the column index in the order by clause


Hi David,

Either the column index, or the alias assigned to it in the SELECT
statement (keeping in mind that a column name by itself in the SELECT
will default to it's own name if no alias is provided).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #14
On 4 Mar 2005 05:34:07 -0800, ja**@mimer.com wrote:
This is quite confusing.

(snip)

Hi Jarl,

Now, I'm confused too.

What I posted is based on what I've read over the years in this and
other groups. I have no inside knowledge about this subject.

Let's hope that Joe Celko sees this exchange and posts a reply to
enlighten us.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #15
AFAIK, ANSI SQL-92 does not allow expressions in the ORDER BY clause,
only names or ordinal positions of the columns in the resultset.

ANSI SQL-99 does allow expressions in the ORDER BY clause. Also, it
deprecated the use of ordinal positions.

So the most ANSI compliant way of writing the query would be the one
posted by Hugo:

SELECT FIXID, COUNT(*) AS IOIs
FROM IOI_2005_03_03
GROUP BY FIXID
ORDER BY IOIs

HTH,
Gert-Jan

ja**@mimer.com wrote:

Hugo Kornelis wrote:

As far as I know, SQL-99 is an ISO standard only, not (yet?) adopted

by
the ANSI committee.

I'm sure that Joe Celko will correct me if I'm wrong.

Best, Hugo


Hugo,

This is quite confusing.

If you go to the ANSI web store, http://webstore.ansi.org/ansidocstore,
and search for SQL, you'll find that the SQL-99 documents are named
something like INCITS/ISO/IEC 9075-5-1999 etc, all with a note
"formerly ANSI/ISO/IEC 9075-5-...". As you can see the ANSI part of the
label has been replaced with INCITS. (Whatever that means.)

And in the list these documents are labelled with the ANSI logo. I
don't know what that means... But I do know that ANSI is a member of
the ISO SQL Standards Committee.

Regards,
Jarl

Jul 23 '05 #16
Hugo Kornelis (hugo@pe_NO_rFact.in_SPAM_fo) writes:
Let's hope that Joe Celko sees this exchange and posts a reply to
enlighten us.


Celko was on the SQL-92 committee, and does not seem to have high
thoughts about SQL-99, so I would not count on him.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #17
On Sat, 5 Mar 2005 17:16:21 +0000 (UTC), Erland Sommarskog wrote:
Hugo Kornelis (hugo@pe_NO_rFact.in_SPAM_fo) writes:
Let's hope that Joe Celko sees this exchange and posts a reply to
enlighten us.


Celko was on the SQL-92 committee, and does not seem to have high
thoughts about SQL-99


Probably because embracing SQL-99 would force him to rewrite all his
canned replies <bg>
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #18
>> As you can see the ANSI part of the label has been replaced with
INCITS. (Whatever that means.) <<

First it was NCITS, which is pronounced "Insights" -- I think that was
National Committee for Information Technology Standards, then it was
"International" when they added the "I". Standards are a weird world.
I am just back from vacation and want to go to bed, but i will look it
up tomorow. I was on the "ANSI X3H2 Datatabase Standards Committee"
which started as part of the CODASYL committee, etc.

Jul 23 '05 #19

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

Similar topics

3
by: Steve | last post by:
Hi; I would like to read a list of tables from a temp table and then do a sql statement on each table name retrieved in a loop, ie: -- snip cursor loop where cursor contains a list of...
11
by: randi_clausen | last post by:
Using SQL against a DB2 table the 'with' key word is used to dynamically create a temporary table with an SQL statement that is retained for the duration of that SQL statement. What is the...
8
by: windandwaves | last post by:
Hi Folk I want to create something like AMAZON: those who liked page A also liked page B (I am going to apply the concept to a few different selections, but to keep it simple I will talk about...
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
1
by: kbromer | last post by:
I'm in the process of trying to teach myself T-SQL out of a WROX book, and am having a problem wrapping my head around this example, I understand the concept of Primary Keys through using MS Access,...
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="...
0
by: dharper | last post by:
Hi! I'm new to sql 2005 and need to run an automated report that is a fairly simple fixed width ragged right report. Problem is I need a header in the report that has static text, plus the rowcount...
4
by: eeb4u | last post by:
I am connecting to MS SQL 2000 from Red Hat EL4 using freetds and currently running queries to get counts on tables etc. When running SELECT queries I notice that the data returns and I have to...
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: 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
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
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
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.