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

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

Related

SQL Server database - get last record

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

SQL Server - Select list and update a field in each row

I hate to ask something that I know has been answered, but after 45 minutes of searching I yield to everyone here.
I have a table where I need to select a list of items based on a value, and then take one field and add a year to it and update another column with that value.
Example:
select * from table where description='value_i_need'
UPDATE table SET endDate=DATEADD(year, 1, beginDate) -- For each item in the select
EDIT: The select statement will return a ton of results. I need to update each row uniquely.
I don't know how to combine the select or loop through the results.
My SQL knowledge is limited, and I wish I could have figured this out without having to ask. I appreciate any help
Edit 2:
I have one table that I need to go through and update the end date on each item that matches a description. So I have 1000 "Computers" for example, that I need to update a field in each row with that description based on another field in the same row. All coming from one table
Try like this
UPDATE
Table T1
SET
T1.endDate=DATEADD(year, 1, beginDate)
WHERE T1.description='value_i_need'
Unless I missed something, you should be able to simply add your criteria to your update statement as follows:
UPDATE [table]
SET endDate=DATEADD(year, 1, beginDate)
WHERE description='value_i_need'
UPDATE T
SET T.END_DATE = DATEADD(YEAR,1,T.BEGINDATE)
FROM TABLE T
WHERE T.DESCRIPTION = 'value_i_need
Do you mean:
UPDATE [TABLE] SET ENDDATE=DATEADD(YEAR,1,BEGINDATE) WHERE DESCRIPTION='VALUE_I_NEED'

select statement - update the value of one field with a fixed value

I have a select statement that returns a few fields from a table.
I want to update only the results of that select, giving a fixed value in one field.
I though of that, but it doesn't work:
UPDATE
(SELECT * from table.... where...)
SET field1=1
You didn't need a SELECT, just use a WHERE clause directly with the UPDATE to do this only for the rows that statify the condition in the WHERE clause:
UPDATE t
SET field1 = 1
FROM table AS t
WHERE ...
If your are using t-sql
UPDATE
SET field = fixed value
from tablename
where filed....

Delete one column on the criteria of another SQL Server 2008

One attribute in a table became corrupted after a certain point in a table of mine. I want to delete every pat_coun attribute if it has an ID that begins with 11 (number, not text). So I don't want to get rid of any of the records in the database, just clear out the attribute pat_coun if it's ID begins with 11
DELETE pat_coun from myTable
WHERE id %11
Just want to make sure this is right before I go deleting stuff. Thanks.
To clear out an attribute, do NOT use the DELETE function! That deletes a row from your table!
You need to use UPDATE instead:
UPDATE myTable
SET pat_coun = NULL
WHERE id LIKE '11%'
If you want to delete a record (a row) you can use
DELETE FROM myTable
WHERE condition
If you just want to "clear" a particular column you should use
UPDATE myTable
SET pat_coun = 0 // or NULL or whatever you please
WHERE condition
For condition IMHO you should convert your number to string and check like this
WHERE CONVERT(VARCHAR(20), pat_coun) LIKE '11%'
try this
update myTable
set pat_coun = null
where id like '11%'

How do you use the results from a query to automatically update a table

Basically, I have a calculated field I am creating using a select query. I want to use the results of this calculation to update a column in an existing table. How do I do that in the same query? How do I use the results of a calculated field to automatically update my table?
Use the select as the value in an update statement.
UPDATE <table> SET <column> = (SELECT ...)