473,465 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Count Item function

41 New Member
Hi,

I need to count the number of rows in my Item table. The following statement gives me the number i.e. 200.

Select Count(*) As Counter
From Item
Group By Id

How can I number each individual row so that the row will have a number next to it i.e.

Select Count(*) As Counter,[Count Statement] as Number of the Row
From Item
Group By Id

Thanks
Jan 12 '07 #1
6 2971
almaz
168 Recognized Expert New Member
It's not clear what are you trying to achieve, because your first query should return a number of rows equal to the number of different Id values in your table, not a single value. If following query is not what you want, please post here sample data and expected result.
Expand|Select|Wrap|Line Numbers
  1. Select Count(*) As Counter, Id as Number of the Row
  2. From Item
  3. Group By Id
  4.  
Jan 12 '07 #2
Lawrence 007
41 New Member
Almaz,

Your query gave me the following error:
Incorrect syntax near the keyword 'of'

Some saple data would be :

ID ItemLookupcode Price
----------- ------------------------- ---------------------
2683 PC6950A 11.6600
1280 B17082205 21.1700
1348 B1SD38913 12.9800
1349 B1SD91013 22.8300
2571 P02600 .1772

If you do the Count Statement it gives you the following:

Select Count(*) As NumberItems from Item

NumberItems
-----------
5

What I need if Possible would be the following :

NumberItems ID ItemLookupcode Price
------------------- ----------- ------------------------- ---------------------
1 2683 PC6950A 11.6600
2 1280 B17082205 21.1700
3 1348 B1SD38913 12.9800
4 1349 B1SD91013 22.8300
5 2571 P02600 .1772


Thanks Almaz
Jan 12 '07 #3
almaz
168 Recognized Expert New Member
So you are looking for a way to enumerate rows. You can only do it over some sorting. It means that you must define the order in which rows are to be enumerated. For your case ID field can define the order:
Expand|Select|Wrap|Line Numbers
  1. select count(*) As NumberItems, Item.ID, Item.ItemLookupcode, Item.Price
  2. from Item Prev inner join Item on Prev.ID<=Item.ID
  3. group by Item.ID, Item.ItemLookupcode, Item.Price
In SQL Server 2005 there is a much simpler way:
Expand|Select|Wrap|Line Numbers
  1. select row_number() over (order by ID) as NumberItems, *
  2. from Item
Jan 12 '07 #4
iburyak
1,017 Recognized Expert Top Contributor
Maybe you can try this:



select id_col = identity(int,1,1), * into #tmp from Item order by ID

select * from #tmp


You can omit order.
Jan 12 '07 #5
Lawrence 007
41 New Member
That is what I needed. Thanks Almaz
Jan 12 '07 #6
Lawrence 007
41 New Member
iburyak,

Thanks, that worked as well.

Thanks
Jan 12 '07 #7

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

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
2
by: Ken | last post by:
The fact that you can not reassign a variable in XSL is an endless source of frustration, causing you to jump through all sorts of non-intuitive hoops. In this case, however, the lack of...
3
by: jason | last post by:
Hello. I've got this simple collection populate code I downloaded from the net (sorry can't find source now) I'm trying to test, but I can't seem to get it to work. Any help would be greatly...
2
by: Praveen Balanagendra via .NET 247 | last post by:
here is the source code private void AddRow() { TableCell tc = new TableCell(); tc.Controls.Add(new LiteralControl("NewRow")); DataGridItem di = new...
3
by: Hyphessobricon | last post by:
Hallo, Indeed, a count of a query with a group by function gives more records than there are and so for-next structures don't function. How is this to be mended. Anyone? Everyone in fact....
2
by: GoCoogs | last post by:
I'm trying to count how many items are in a dynamic collection. This is the code I have so far. *** Begin Code *** Public Class Rule Private _rulevars As RuleVarsCollection Private _rulename...
1
by: AZKing | last post by:
Hi all, I am trying to create a query that will count the number of items in a certain field. For example, the number of cars listed as "Ford" in the USA. I can do it for just one item, but there...
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="...
26
by: Ping | last post by:
Hi, I'm wondering if it is useful to extend the count() method of a list to accept a callable object? What it does should be quite intuitive: count the number of items that the callable returns...
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,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.