SQL: Numbering rows in a temporary column?
-
SQL Filter: is there any way when making a query to create a temporary column that assigns each row in the result set an ordinal number? This is for when you not only want to count the number of items in a result set (ie, there are 50 items), you also want them numbered 1-50 or whatever. I'm using Access as a client for SQL Server, FWIW, but the more general the SQL answer, the better.
-
Answer:
I dont believe there is a standard SQL way. Oracle uses a pseudo-column called 'rownum'. Its a bit trickier in SQL Server but there http://support.microsoft.com/default.aspx?scid=KB;EN-US;q186133.
wildblueyonder at Ask.Metafilter.Com Visit the source
Other answers
The need to see them numbered sounds like more of a front-end concern. Creating a temporary column in the database is a stretch for something like this. Remember that your result set is the product of tables + joins + selection criteria, so it's not even a "table" per se, it's the product of some tables + your query. Creating a column doesn't make sense. Which table would it go in? You'd need to create a temporary "query results" table, but this is what front ends are for. I think ordering your output rows should be reasonably doable in your front end app, though I know nothing about Access.
scarabic
You can do it in PostgreSQL (and Oracle) with http://microolap.com/dba/pgsql/isql/help/pghelp/sql-createsequence.html In MS SQL you can mimic the behaviour of sequences by producing your own procedure to produce numbers.. http://jamesthornton.com/software/coldfusion/nextval.html This is not a full solution by any means, but should be hackable. As I have zero knowledge of MS's scary system, I cannot help with this, but I figure you can hack it into returning a collection of values, and use it in a JOIN with your main query. Best of luck!
wackybrit
After following vacapinta's link, I'd say that provides a good way to do it, doesn't seem tricky to me, although very MS SQL specific.. so give that a shot first.
wackybrit
If you just want to count the number of results for a query, what's the matter with http://www.techonthenet.com/sql/count.htm%20 ? Obviously you can do sequences and such, but this seems like the most direct approach.
majick
(Oh. I misread the question. Shutting up now.)
majick
Remember that your result set is the product of tables + joins + selection criteria, so it's not even a "table" per se, it's the product of some tables + your query. With all due respect, this is not a productive way of thinking in SQL. With structures like views and nested queries, a result set is a table in many ways. I was also going to add that the auto-join mentioned in the sql server link applies to Oracle and other SQL databases and is a powerful way of getting things done. For example, counting duplicate rows in a table is done the same way: Select count(*) from nametable a where userid>(select min(userid) from nametable b where a.first_name=b.first_name); For userid, you can subsitute any unique, ordered column. This example counts how many duplicate first names there are in the table.
vacapinta
Finally a question I feel comfortable answering... Here is a quick query I wrote up that accomplishes what you want, you can put it in a SQL Server stored procedure to use it . (sample codes creates and orders a numbered list of users who are employees ordered by name) SET NOCOUNT ON CREATE TABLE #temp ( Counter INTEGER IDENTITY (1,1) NOT NULL, UserID INTEGER ) INSERT INTO #temp (UserID) SELECT UserID FROM Users WHERE Users.UserID IN (SELECT UserID FROM Employees) ORDER BY Users.LastName, Users.FirstName SELECT #temp.Counter, Users.* FROM #temp INNER JOIN Users ON Users.UserID=#temp.UserID ORDER BY #temp.Counter DROP TABLE #temp Of course, change the tables to accomplish what you want, but I think this is a fairly good template.
patrickje
Related Q & A:
- How To Sort a GridView Column When a Header Is Clicked?Best solution by Stack Overflow
- How to write temporary files from temporary path to a temporary directory?Best solution by msdn.microsoft.com
- How to change column data's as a separate column wise format in a SQL Server?Best solution by stackoverflow.com
- How do I group different rows in a field?Best solution by Stack Overflow
- Can you get a temporary permit for both a motorcycle and car at the same time?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.