Questions tagged [computed-column]

Computed columns are columns that represent a calculation or operation performed on other columns contained in the given table. Known as generated or virtual columns in some products.

Filter by
Sorted by
Tagged with
29
votes
3answers
7k views

When are computed columns computed?

When are the values for computed columns determined? When the value is retrieved? When the value is changed? Some other time? I'm guessing this is a novice question since I'm not finding anything in ...
29
votes
2answers
2k views

Is there a way to prevent Scalar UDFs in computed columns from inhibiting parallelism?

Much has been written about the perils of Scalar UDFs in SQL Server. A casual search will return oodles of results. There are some places where a Scalar UDF is the only option, though. As an ...
29
votes
4answers
3k views

Does SQL Server Only Perform Calculations In A SELECT List Once?

Take the following example: SELECT <CalculationA> As ColA, <CalculationB> As ColB, <CalculationA> + <CalculationB> As ColC FROM TableA Would CalculationA and ...
24
votes
3answers
3k views

Index on Persisted Computed column needs key lookup to get columns in the computed expression

I have a persisted computed column on a table which is simply made up concatenated columns, e.g. CREATE TABLE dbo.T ( ID INT IDENTITY(1, 1) NOT NULL CONSTRAINT PK_T_ID PRIMARY KEY, A ...
18
votes
5answers
3k views

Unable to create a Filtered Index on a Computed Column

In a previous question of mine, Is it a good idea to disable lock escalation while adding new calculated columns to a table?, I am creating a computed column: ALTER TABLE dbo.tblBGiftVoucherItem ADD ...
16
votes
4answers
10k views

PostgreSQL: Generated Columns

Does PostgreSQL support generated columns? Also know as virtual columns. I am not talking about IDENTITY columns. I can’t find any information on this remarkable feature but I know that it is ...
16
votes
2answers
480 views

Is it legal for SQL Server to fill PERSISTED columns with data that does not match the definition?

I'm following up on this question about strange values in a PERSISTED computed column. The answer there makes a few guesses about how this behavior came to be. I'm asking the following: Is this not ...
15
votes
2answers
7k views

Why is a NOT NULL computed column considered nullable in a view?

I have a table: CREATE TABLE [dbo].[Realty]( [Id] [int] IDENTITY(1,1) NOT NULL, [RankingBonus] [int] NOT NULL, [Ranking] AS ([Id]+[RankingBonus]) PERSISTED NOT NULL .... ) And a ...
15
votes
2answers
474 views

Index on a persisted computed column not seekable

I have table, called Address, that has a persisted computed column called Hashkey. The column is deterministic but not precise. It has a unique index on it that is not seekable. If I run this query, ...
14
votes
2answers
796 views

Computed Column Index Not Used

I want to have a fast lookup based on if two columns are equal. I tried to use a computed column with an index, but SQL Server doesn't seem to use it. If I just use a statically populated bit column ...
10
votes
3answers
2k views

Creating Non-Clustered Index on Non-Persisted Computed Column SQL Server

I am struggling to find any documentation on how SQL Server actually stores a non-persisted computed column. Take the following example: --SCHEMA CREATE TABLE dbo.Invoice ( InvoiceID INT ...
10
votes
5answers
2k views

Alternative to Self Join

I have asked a question here: https://stackoverflow.com/questions/43807566/how-to-divide-two-values-from-the-same-column-but-at-different-rows about dividing values from the same table, at the same ...
10
votes
1answer
955 views

Changing primary key from IDENTITY to being persisted Computed column using COALESCE

In an attempt to decouple an application from our monolithic database, we've tried to change INT IDENTITY columns of various tables to be a PERSISTED computed column that uses COALESCE. Basically, we ...
8
votes
3answers
3k views

Avoiding a divide by zero error in a computed column

How do you avoid divide by zero error in the below table? CREATE TABLE [dbo].[TblDivision] ( [Numerator] int NOT NULL, [Denominator] int NOT NULL, [Result] AS (Numerator/ Denominator) ) ...
8
votes
2answers
5k views

Computed column cannot be persisted because the column is non-deterministic

I know this is not the first time this type of question has been asked. But why in the following scenario is the persisted computed column being created "non-deterministic". The answer should always ...
7
votes
2answers
211 views

Deriving a computed column based on several nullable columns

I'm a bit of an SQL novice. I've been trying to create an auto generated column called TAG from 5 other columns, some of which can retain NULL marks. The syntax used to create the auto generated ...
7
votes
1answer
393 views

Why does adding a computed column prevent predicate pushdown?

I have a weird situation that I don't quite understand. I have a table like this kinda: CREATE TABLE dbo.cc_demo ( id INT IDENTITY PRIMARY KEY, up_action INT, ...
6
votes
3answers
4k views

Bad Practice to Store Calculated Data in Database? [closed]

Is it bad practice to store calculated data in each row, or is it better to calculate at the application layer with every read from the database. Storing in the database avoids the need to calculate ...
6
votes
1answer
123 views

Table with persisted computed CLR originated data marked with has_unchecked_assembly_data = 1, but DBCC CHECKTABLE does not unset this same flag

We have an OrderLines table with columns: Quantity int not null QtyCancelled int not null QtyBackorder int not null QtyPicking int not null QtyPacking int not null QtyShiped int not null with ...
5
votes
5answers
3k views

Creating a Persisted Computed Column with a function

I am working with the programmers on a database solution. They want to add a computed column to mimic the old keys for the older queries, procedures, and systems and index it. The new keys will be ...
5
votes
4answers
795 views

Implicit conversion warning despite persisted computed column vs. performance hit from UDF in persisted computed column definition

I have a need for a computed column to resolve an implicit conversion problem. I have a column that is declared as a VARCHAR that should only ever store integers, but is occasionally populated in ...
5
votes
4answers
549 views

Interpolate dates along a line

I have a road_condition table in Oracle 12c: create table road_condition ( cond_id number(5,0), road_id number(5,0), cond_date date, condition number(5,0) ); insert into ...
5
votes
2answers
5k views

Referencing another table from a computed column

I am working on a project where many of the entities in the data model do not have a name column, but instead the "name" needs to be constructed from a concatenation of multiple columns. Initially I ...
4
votes
2answers
379 views

Is there any need to persist a (precise) computed search column that I've indexed?

I have a number of columns that store data that may contain some extra characters for formatting/grouping. Think white space and dashes in data like phone numbers and license plate numbers. When ...
3
votes
2answers
272 views

Why is the result of a totally deterministic and all-encompassing “case” in select query in view nullable?

I have a table, with Id as the primary key. create table Anything ( Id bigint not null primary key identity(1, 1) ) When I look at this table in Object Explorer, I see this image of course: As ...
3
votes
3answers
318 views

create a visible uniquifier column for a nonunique clustered index

I have a table of events, clustered index is the creation date/time. Normally there should never be two events happening in the same millisecond, but freak occurences happen, so the column is not ...
3
votes
2answers
9k views

Computed/Generated columns in MySQL 5

How do I create a table with a formula column in MySQL version 5? For example: I have a table named Product, which has 5 columns: id int, amount int, sold_out int, faulty int, remain_amount is (...
3
votes
1answer
242 views

Add computed column with “IN” syntax

I have a query in SQL Server 2008R2 that I want to run against a computed column that in turn I am going to index - in order to speed things up. The column is of type int right now. I cannot get the ...
3
votes
1answer
540 views

Type conversion in expression may affect “CardinalityEstimate” - on a computed column?

I had a look at this question: Type conversion in expression may affect “CardinalityEstimate” in query plan choice? But that was related to collation and not with computed columns. I have the ...
3
votes
2answers
159 views

Can I create a computed column that requires input to select?

I have a scenario in a very old and very large application, where I have a table representing a type of resource: CREATE TABLE resource (resource_id INT, name NVARCHAR(4000)) This table is selected ...
3
votes
1answer
1k views

Postgres 9.6.5 - Can I use a query to calculate a value from two columns and insert the calculated value in a new column in the same table?

I am running the following version of Postgres on CentOS 7: PostgreSQL 9.6.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11), 64-bit I am new to database operations ...
3
votes
1answer
351 views

How to SUM by date range within a view

I have a view in which I need to Group By or SUM OVER Partition By year and quarter, but the year and quarter will be computed within the view. I would like to keep it all within a view instead of ...
3
votes
3answers
800 views

computed column making the select query to run slow [closed]

We are creating indexes, as per the requirements, on one server and copying all the indexes to the other servers. All servers have similar objects & schema but different resource configuration ...
3
votes
1answer
2k views

computed column as foreign key allow null

I have two tables. TABLE A: One of them stores some integer identificators, which are unique, but actually are structured entities. I will call them full_id. Actually its a two parted indetificator,...
3
votes
1answer
4k views

Mysql Pivot rows into dynamic columns with unknown number of columns

I have an output of a query as follows: bid | code | amount | ----------------------------- 2915 | BF | -10700.00 | 2915 | YQ | -300.00 | 2915 | YR | 0.00 | 2915 | YM ...
3
votes
1answer
241 views

Datatype for a Persisted Computed Column? Should I Convert/Cast When Creating It?

What is the Datatype for a Persisted Computed Column? (a) Does SQL Server automatically infer it? (b) Also is better to cast it as a datatype, so SQL does not spend time to find what data type it ...
3
votes
2answers
243 views

Is there a way to avoid having chained/nested “replace()” items when using an ISO 8601 timezone offset value in a generated column?

I am using MySQL 5.7 and have a table that has the following columns that are being used to store some data that is source from an Apache common log formatted access log; details extracted from a ...
3
votes
0answers
968 views

Does PostgreSQL support `unnest` as a column?

Does PostgreSQL document and support this behavior of the SRF unnest() as a computer/virtual column, SELECT unnest(array_agg(id)) AS id FROM foo GROUP BY CUBE (a,b,c,d) HAVING count(*) > 1 If ...
3
votes
0answers
68 views

OPTION (RECOMPILE) is Always Faster; Why? [duplicate]

Despite having read botht hese 2 questions about procedure recompile below Stored Procedure With Recompile OPTION (RECOMPILE) is Always Faster; Why? I am still having a problem - my query is ...
2
votes
4answers
595 views

Computed field when NULL

I have two fields in my table : A int, B int. I want to add two computed fields (CF1 & CF2) that uses these two fields A & B. CF1 = (case when [A]>[B] then (CF1=3) else case when [A]<[B]...
2
votes
1answer
827 views

Calculated Field Updating Every Row

I added a calculated field to my table, and only want to update NULL rows (or "new" rows). Something is going awry, and every row in my table is being updated with the current DateTime when 1 row is ...
2
votes
1answer
2k views

How to create a Hash Computed Column for Many Columns?

Does anyone know how to create a Hash Computed Column Persisted? I keep receiving an error below. Otherwise, I will have to utilize an update or trigger statement everytime. I know how to conduct for ...
2
votes
1answer
3k views

computed columns vs views [closed]

I'm trying to think about the best way to do this. If i have a table like this in sql server: StudentPointsTable StudentID TestDate Points I need to have Averages and standard deviation calculated ...
2
votes
1answer
953 views

Use a scalar hashing function in a computed column - non-deterministic?

I have a scalar function select [dbo].[fn_get_hash_fields] ('blah','dbo') to which is passed a table and a schema and which returns an expression representing a concatenation of all column values in ...
2
votes
1answer
49 views

Automatic seek on an index on a calculated column

I have a table with a varchar field than I need to be unique, but it is too long for the nonclustered index that the uniqueness constraint requires, so I have added a computed column (either using ...
2
votes
1answer
147 views

SQL Server FullText search on non-persisted columns

Let's say I have a table with 3 columns: Id, ColA and ColB. Then I created a FullText index: CREATE FULLTEXT INDEX ON [Table] ( ColA LANGUAGE 1033, ColB LANGUAGE 1033 ) KEY INDEX [...
2
votes
1answer
39 views

Create a function column that is the result of OR between 3 columns

I have a table with 3 columns (A,B,C). In each row, only one of the columns is not null. How do I create a function column or a computed column that will consist from all the non null values? ...
2
votes
1answer
97 views

How can I add a numeric version_id column to an existing table?

I have a table in a postgres db with rows versioned with a timestamp that I'd like to add a numeric version_id column to. eg. existing: ID VERSION_DATE -- ----------------------- A 2015-02-05 21:...
1
vote
1answer
134 views

Is any caching mechamism used for non-persisted computed columns?

Does SQL Server cache the result of a non-persisted computed column so it can be reused without incurring cost of recalculation? Additional Context I've always given the advice that if using computed ...
1
vote
2answers
414 views

1 minus percentage returns negative number

I have a sidewalk_inspection table: create table user.sidewalk_insp ( insp_id int, deficiency_length number(10,2), sidewalk_length number(10,2) ); insert into user.sidewalk_insp (insp_id,...