I want to insert sequential numbers into a single column.
The columns have been set to NULL
Is there a command I can now use to populate just that column with sequential numbers
Thanks
you can use row_number analytic function to generate sequence number, that can be used to update the values.
WITH cte
AS
( SELECT ROW_NUMBER() OVER ( ORDER BY (SELECT null)) as rn , column1
FROM table1
)
UPDATE cte
SET column1 = rn
Based on your comment:
that almost did it but it would be nice if we can get it to sequence
properly for instance like 0000001, 0000002,0000003 etc up to 7
characters
I think this is what you are trying to do:
SELECT RIGHT('0000000' + CONVERT(VARCHAR, ROW_NUMBER() OVER ( ORDER BY COLUMN_NAME)), 7) AS ROW_NUM
FROM TABLE_NAME
Update after comment:
Basically this is what I am trying to do. I will be running a query to
grab those 7 character ID from that ALTATH column that will be used as
Reference ID so the column should have those output or type in
it..hope that's clear...thanks
I would personally use an Identity column and format the output when selecting records like this:
SELECT RIGHT('0000000' + CONVERT(VARCHAR, ID_COLUMN), 7) AS ROW_NUM
FROM TABLE_NAME
If you use the analytical functions the ROW_NUM column will not be the same everytime. If you use Identity SQL Server will take care of assigning the numbers in sequence for you. That is the DBMS's job.
Related
Lets say I have a table with a varchar colum and and integer column. The varchar columns are filled and the integer columns are all emtpy:
I would like to execute a signle SQL update statement which populates the integer column with an ascending number like this:
This update statement should not be DBMS-specific, it should run on all popular SQL DBMSs. Is there a way to do this?
I edited the contents of the varchar column as the previous version of my question implied that they were numbered and distinct. They are not - they are completely random.
I would use subquery :
UPDATE t
SET my_integer_column = (SELECT COUNT(*) FROM t t1 WHERE t1.My_Varchar_Column <= t.My_Varchar_Column);
If My_Varchar_Column is unique you could use ROW_NUMBER:
UPDATE table_name
SET my_integer_column =
(SELECT rn
FROM (SELECT t.*, ROW_NUMBER() OVER(ORDER BY My_Varchar_Column) AS rn
FROM table_name t) sub
WHERE table_name.My_Varchar_Column = sub.My_Varchar_Column);
db<>fiddle demo
It will work on modern RDBMS like PostgreSQL/MySQL 8.0/Maria DB/SQLite 3.25/Oracle/SQL Server.
This is a very simple question but I can't seem to find documentation on it. How one would query rows by index (ie select the 10th through 20th row in a table)?
I know there's a row_numbers function but it doesn't seem to do what I want.
Do not specify any partition so your row number will be an integer between 1 and your number of record.
SELECT row_num FROM (
SELECT row_number() over () as row_num
FROM your_table
)
WHERE row_num between 100000 and 100010
I seem to have found a roundabout and clunky way of doing this in Athena, so any better answers are welcome. This approach requires you have some numeric column in your table already, in this case named some_numeric_column:
SELECT some_numeric_column, row_num FROM (
SELECT some_numeric_column,
row_number() over (order by some_numeric_column) as row_num
FROM your_table
)
WHERE row_num between 100000 and 100010
To explain, you first select some numeric column in your data, then create a column (called row_num) of row numbers which is based on the order of your selected numeric column. Then you wrap that all in a select call because Athena doesn't support creating and then conditioning on the row_num column within a single call. If you don't wrap it in a second SELECT call Athena will spit out some errors about not finding a column named row_num.
I need a computed column formula that gives me this yyMMdd##.
I have an identity column (DataID) and a date column (DataDate).
This what I have so far.
(((right(CONVERT([varchar](4),datepart(year,[DataDate]),0),(2))+
right(CONVERT([varchar](4),datepart(month,[DataDate]),0),(2)))+
right(CONVERT([varchar](4),datepart(day,[DataDate]),0),(2)))+
right('00'+CONVERT([varchar](2),[DataID],0),(2)))
And this gives me:
12111201
12111202
12111303
12111304
12111405
12111406
12111407
12111508
What I want is:
12111201
12111202
12111301
12111302
12111401
12111402
12111403
12111501
I'm assuming you want to have a sequence starting at 1 for each date - right? If not: please explain what you really want / need.
You won't be able to do this with a IDENTITY column and a computed column specification. An IDENTITY column returns constantly increasing numbers.
What you could do is not store those values on disk - but instead use CTE and the ROW_NUMBER() OVER (PARTITION BY....) construct to create those numbers on the fly - whenever you need to select them. Or have a job that sets those values based on such a CTE on a regular basis (e.g. once every hour or so).
That CTE might look something like this - again, assuming that DataDate is indeed of type DATE (and not DATETIME or something like that) :
;WITH CTE AS
(
SELECT
DataID, DataDate,
RowNum = ROW_NUMBER() OVER (PARTITION BY DataDate ORDER BY DataID)
FROM
dbo.YourTable
)
SELECT
DataID, DataDate, RowNum
FROM
CTE
Got a table with 1200 rows. Added a new column which will contain incremental values -
Candidate1
Candidate2
Candidate3
.
.
.
Candidate1200
What is the best way to insert these values , non manual way. I am using SQL Server 2008
Assuming there's an IDENTITY column (I called it id for this example):
UPDATE YOUR_TABLE
SET new_column = (SELECT 'Candidate'+ CAST(ROW_NUMBER() OVER(ORDER BY yt.id) AS VARCHAR(4))
FROM YOUR_TABLE yt
WHERE yt.id = YOUR_TABLE.id)
You might be able to use ROW_NUMBER
http://msdn.microsoft.com/en-us/library/ms186734.aspx
Perhaps perform a query that gets the row number and use that query to perform an update on the source table where you concatenate the Row Number with 'Candidate'.
If I do something like
SELECT * FROM mytable ORDER BY mycolumn ASC;
I get a result table in a specific order.
Is there a way in SQL to efficiently find out, given a PK, what position in that result table would contain the record with my PK?
You can count the number of records where the value that you are sorting on has a lower value than the record that you know the key value of:
select count(*)
from mytable
where mycolumn < (select mycolumn from mytable where key = 42)
On databases that support it, you could use ROW_NUMBER() for this purpose:
SELECT RowNr
FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY mycolumn) AS RowNr,
mycolumn
FROM mytable
) sub
WHERE sub.mycolumn = 42
The example assumes you're looking for primary key 42 :)
The subquery is necessary because something like:
SELECT
ROW_NUMBER() OVER (ORDER BY mycolumn) AS RowNr
FROM mytable
WHERE sub.mycolumn = 42
Will always return 1; ROW_NUMBER() works after the WHERE, so to speak.
SQL doesn't work that way. It's set-based, which means that "position in that result table" is meaningless to the database.
You can keep track of position when you map the ResultSet into a collection of objects or when you iterate over it.
Unfortunately you cannot get "the position of a row in a table".
The best you can get, using ORDER BY and a variant of the ROW_NUMBER construct (depends on the database engine in use), is the position of a row in the resultset of the query executed.
This position does not map back to any position in the table, though, unless the ORDER BY is on a set of clustered index columns, but even then that position might be invalidated the next second.
What I would like to know is what you intended to use this "position" for.
This answer applies to MySQL
==> lower than 8.0
SET #row_number = 0;
SELECT
(#row_number:=#row_number + 1) AS num,
myColumn.first,
myColumn.second
FROM
myTable
ORDER BY myColumn.first, myColumn.second
source: http://www.mysqltutorial.org/mysql-row_number/
==> greater than 8.0
Please see MySQL ROW_NUMBER() function manual as I did not test. But it seems this function is prefered.
There's no way you can tell that without selecting an entire subset of records. If your PK is of integer type, you can
select count(*) from mytable
where id <= 10 -- Record with ID 10
order by mycolumn asc