Solved

Show a counter starting at 1 in an SQL 2005 View

Posted on 2014-10-16
11
151 Views
Last Modified: 2014-10-17
My final line of data in my SQL 2005 view is a line counter. Can I add a line counter in a SQL 2005 view? I am not sure if that can be done or it I need to create a new table each time and import the data.
0
Comment
Question by:allenkent
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40385460
I don't know what exactly you are doing on your view, you can add another column doesn't matter
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40385461
Hugh betcha.
CREATE TABLE tmp (numbers int) 
GO

INSERT INTO tmp (numbers) 
VALUES (3), (12), (120),(13), (12), (999) ,(42), (549), (11) 
GO

CREATE VIEW myview AS
SELECT ROW_NUMBER() OVER (ORDER BY numbers)  as counter, numbers
FROM tmp
GO

SELECT * FROM myview
GO

Open in new window

0
 

Author Comment

by:allenkent
ID: 40385558
To answer what I have and what I am doing:
I have a query now that has 832 lines. This view is the data I want but a piece of data is a counter. I need this for an extract to import into someone else system. This is a sample of what I have (and need)(the last part the sequence):

454545 5454512 FS454556       09242014        000000000   00000000001R
454545 8946546 FS454556       09252014        000000000   00000000002R
454545 1557885 FR213317       09252014        000000000   00000000003R
454545 2789942 ET548816       09262014        000000000   00000000004R
454545 5499833 LK712455       09272014        000000000   00000000005R

I ran the above script and got. Nice but I need 850 or so lines of data:

counter      numbers
1      3
2      11
3      12
4      12
5      13
6      42
7      120
8      549
9      999
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 40385567
Show us the query that returns the above set.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40385787
No points please:

Can I add a line counter in a SQL 2005 view?

YES, use ROW_NUMBER() OVER()

ROW_NUMBER() & OVER() have been available since (and including) SQL Server 2005

see:
http://msdn.microsoft.com/en-us/library/ms186734(v=sql.90).aspx
http://msdn.microsoft.com/en-au/library/ms189461(v=sql.90).aspx

nb, row_number returns BIGINT

(that's up to 9,223,372,036,854,775,807 & is way more than 850! )
0
 
LVL 39

Expert Comment

by:lcohan
ID: 40387128
Or by adding a simply count as the row number to show the "relative record number" - something like in pseudo code below:

SELECT id, (SELECT COUNT(*) FROM table_name t2 WHERE t2.id <= t.id) AS rownumber
FROM table_name t
ORDER BY id
0
 

Author Comment

by:allenkent
ID: 40387372
I am getting errors on both samples. Can you help with code specific? Here is my basic query:

SELECT INVOICE_NUMBER,TRANSACTION_DATE
FROM dbo.INVOICE_HISTORY
WHERE     (TRANSACTION_DATE > CONVERT(DATETIME, '2014-10-01 00:00:00', 102))
0
 
LVL 39

Expert Comment

by:lcohan
ID: 40387414
Please try this:

 
SELECT t.INVOICE_NUMBER,t.TRANSACTION_DATE,
             (SELECT COUNT(*) FROM INVOICE_HISTORY t2 with (NOLOCK) WHERE t2.INVOICE_NUMBER <= t.INVOICE_NUMBER) AS rownumber
  FROM dbo.INVOICE_HISTORY t
  WHERE     (TRANSACTION_DATE > CONVERT(DATETIME, '2014-10-01 00:00:00', 102))
 ORDER BY t.INVOICE_NUMBER
0
 

Author Comment

by:allenkent
ID: 40387428
Works nice but it is not starting from 1. The rownumber your are listing the row number of the database. Example:  Invoice 66010 happens to be row number 64810. This is the number displaying. I want to get count from 1 to x on my SQL view.
0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 40387440
Right - sorry...this should work and add the order by if you need your record set ordered:


SELECT INVOICE_NUMBER,TRANSACTION_DATE,
            ROW_NUMBER() OVER (ORDER BY INVOICE_NUMBER)  as rownumber
 FROM dbo.INVOICE_HISTORY
 WHERE     (TRANSACTION_DATE > CONVERT(DATETIME, '2014-10-01 00:00:00', 102))
0
 

Author Closing Comment

by:allenkent
ID: 40387449
That is beautiful! Perfect.

SELECT INVOICE_NUMBER,TRANSACTION_DATE,
             ROW_NUMBER() OVER (ORDER BY INVOICE_NUMBER)  as rownumber
  FROM dbo.INVOICE_HISTORY
  WHERE     (TRANSACTION_DATE > CONVERT(DATETIME, '2014-10-01 00:00:00', 102))
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question