简体   繁体   中英

SQL Server 2008 R2 Express Row numbers

Backstory: I'm using SQL Server because my data set is ~ 150 columns wide and I don't feel like typing all of that into MySql Workbench table setup. So I'm using the MS SQL Server data import tool.

I'm trying to set the table up so that I can downsample the data in R . I'm planning on pulling all the positives (~100k) and then pulling a random sample of ~400k from the non-positives. To do this I need a row numbering column to index positives and non-positives.

In MySQL I'd just add a row numbering column when setting up the table, but I don't know how to do that in SQL Server's data import tool. I've already built the table and would prefer to add it (or know if SQL Server already has assigned row numbers) but am willing to rebuild the table as it's not useable without row numbers.

EDIT: I need to know how to do this in SSMS or directly in the import tool.

Any suggestions?

You can use the ROW_NUMBER() function. Either use it in your queries, or you could write an update statement that, based on ROW_NUMBER(), updates the rows in your table.

If you don't care about ordering when assigning row numbers, you could also just add a new integer auto incrementing column to the table. In order to do that, add a new IDENTITY column to the table:

ALTER TABLE dbo.YourTable
    ADD ID INT IDENTITY(1,1)

Add a column to existing table and uniquely number them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM