SQL Server database - get last record - sql

How do I we get last row in SQL Server if we don't have primary id and numeric column? For example we only have a name and a few other columns in the table

If you mean the highest name, alphabetically,
select max(columnName) from tableName
If you mean the last row added to the table, it's not possible unless you have a column with the date/time inserted, or some other value like an identity column.

Maybe if you have a column for insertedDate you could do something like this:
select top (1) * from tableName order by insertedDate DESC
If not, for future use if you don't want an ID column, maybe you can add a default constraint for inserted date to pick from the system date, so you wouldn't have to add anything when you insert.

With no definition of 'last row'... :
select top 1 *
from t
order by Name desc

Related

How to show specific value in a column - sql (redshift)

I have a situation where I want to show a value that corresponds to one field in a table but in all rows. What is better is to show you an example in a screenshot:
What I want is to have the value of 3807 in every row? How can I do that?
THank you in advance.
Try using MAX as an analytic function:
SELECT
site_id,
bt_max_speed,
MAX(bt_max_speed_on_site_coverage) OVER (PARTITION BY site_id) bt_max_speed_on_site_coverage
FROM yourTable
ORDER BY site_id;
If you simply want to update one column so it has same value for all rows you can just use UPDATE without any conditions
UPDATE <table_name> SET <column_name> = <value>
In your case it would look like this, but with replaced with actual name of your table
UPDATE <table_name> SET bt_max_speed_on_site_coverage = 3807

update rows one by one with max value + 1 in SQL

here is my situation,
I have 2 tables,
1st table has all records, and it has IDs
2nd table has new records and it doesnt have ID, yet.
I want to generate ID for 2nd table with max(id) + 1 from 1st table.
when i do this, it makes all rows same id number, but i want to make it unique increment number.
e.g
select max(id) from table1 then it gives '997040'
I want to make second table rows like;
id
997041
997042
997043
997044
i think i need to use cursor or whileloop, or both, but i could not create the actual query.
sorry about bad explanation, i am so confused now
Use ROWNUM to generate incrementing row numbers. E.g.:
SELECT someConstant + ROWNUM FROM source.
CREATE TABLE table_name
(
ID int IDENTITY(997041,1) PRIMARY KEY
)
I hope this sql query would work!!
Or refer http://www.w3schools.com/sql/sql_autoincrement.asp

How to get last inserted value of a Specific column in SQL?

Is there any way where we could us IDENT_CURRENT() to retrieve a specific value from a specific column
We will consider that you need to retrieve product_code from product table-
SELECT LAST (product_code) FROM product;
Or else you can try this
SELECT Ident_Current(product_code) AS Alias from product;
according to https://msdn.microsoft.com/en-us/library/ms175098.aspx
You could use
SELECT Ident_Current('column_name') AS Alias;
For example if you are trying to retrieve a product Id from your company_product table you could do:
SELECT IDENT_CURRENT('company_product.comp_prod_id') as CURRENT_PRODUCT_ID;
This would give you the last entered primary key if your primary key column name was comp_prod_id and it would set the alias CURRENT_PRODUCT_ID to your result
The correct format might be
SELECT IDENT_CURRENT ('tablename.columnname');
But I am not sure as I do not have anything to test it on at my disposal

Select last value in a specific column? (PostgreSQL)

Running into some issues when trying to retrieve the last value in a specific column, from a table and assign it into a variable.
Looking for the last int in a column "id" that is a primary key basically.
So I have a variable like "lastValue" in a select statement like :
select last(id) into lastValue from test_table
Not sure on an exact, or best way to accomplish this.
(on mobile, please forgive formatting)
A typical way to solve this is with order by and limit:
select id
from test_table
order by id desc
limit 1;
Of course, in this case, you could simply use:
select max(id)
from test_table;
But the first method allows you to choose whichever variables you want from the row with the maximum value.

SQL -How to add an auto incremental id in a auto generated temporary table

I have a query like below and it generate a temporary table automatically based on parameter. So, the number of column of this table can be vary. Now , i need to add an auto incremental id column into this table.How i do it?
SELECT #SourceFields INTO ##StoreSourceInfo FROM testdb.dbo.#SourceTable
Note: 1) Number of source field & name of table pass using the parameter #SourceFields & #SourceTable.
2) So, the number of column can be vary on ##StoreSourceInfo table.
Current Result:
select * from ##StoreSourceInfo shows only the available column.
Expected Result:
select * from ##StoreSourceInfo query will show an additional auto incremental id column & all rest of the column available in the temp table.
Hope you get me. Thanks in advance.
SELECT
IDENTITY(INT, 1, 1) AS id
INTO #Temptable
FROM User
You can use row_number function
Select ROW_NUMBER() over (order by T.field1) rownum
, T.field1, T.field2 into #temp1
from #Table T
Use the identity function. See the link for an example. http://msdn.microsoft.com/en-us/library/ms189838.aspx
You have to try with following query to get your excepted result to add a extra auto increment column :
SELECT
IDENTITY(INT, 1,1) AS Rank,
#SourceFields
INTO
##StoreSourceInfo
FROM
testdb.dbo.#SourceTable
Means apply IDENTITY function...