Questions tagged [calculated-columns]

A calculated column is calculated from an expression that can use other columns in the same table

A calculated column is calculated from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators. The expression cannot be a subquery.

2235 questions
17
votes
4 answers

Loop through columns in MySQL trigger

Is it possible to loop through the all column names while inside a trigger? Scenario: To log all the columns of a table that have been modified. If some values did not change, do not log those ones. DROP TRIGGER IF EXISTS…
Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41
17
votes
4 answers

SQL Server - index on a computed column?

I'm joining to a table dozens of different times, and every time, I join (or filter) based on the results of a SUBSTRING of one of the columns (it's a string, but left-padded with zeros, and I don't care about the last four digits). As a result,…
SqlRyan
  • 33,116
  • 33
  • 114
  • 199
16
votes
3 answers

Computed bit column that returns whether another column is null

I try to have this computed column: CREATE TABLE dbo.Item ( ItemId int NOT NULL IDENTITY (1, 1), SpecialItemId int NULL, --I tried this IsSpecialItem AS ISNULL(SpecialItemId, 0) > 0, --I tried this IsSpecialItem AS…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
16
votes
5 answers

How can I cache a calculated column in rails?

I have a tree of active record objects, something like: class Part < ActiveRecord::Base has_many :sub_parts, :class_name => "Part" def complicated_calculation if sub_parts.size > 0 return self.sub_parts.inject(0){ |sum, current| sum +…
two-bit-fool
  • 4,978
  • 6
  • 28
  • 26
16
votes
5 answers

Create Computed Column using data from another table

I have a SQL Server 2008 R2 database. This database has two tables called Pictures and PictureUse. Picture table has the following columns: Id (int) PictureName (nvarchar(max)) CreateDate (datetime ) PictureUse table has the following columns…
Smartboy
  • 986
  • 6
  • 22
  • 37
15
votes
3 answers

SQL Server: Is there a performance cost to computed columns?

If I include a computed column in a table, does it place a perfomance burden on it? For example, if I have a table defined as follows: CREATE TABLE users( givenname …, familyname …, fullname AS givenname+' '+familyname ); … does it…
Manngo
  • 14,066
  • 10
  • 88
  • 110
15
votes
1 answer

Cannot persist computed column - not deterministic

I have this function for a computed column : CREATE FUNCTION [dbo].[GetAllocatedStartTime](@Year INT, @Week INT) RETURNS DATETIME WITH schemabinding AS BEGIN RETURN…
sprocket12
  • 5,368
  • 18
  • 64
  • 133
14
votes
2 answers

SQL Server: Computed Column defintions retrievable from Database?

I would like to query the definition of a computed column from the database, but can't find a command that seems to do what I want... For instance, if a column is defined as: CallDT AS…
Rimer
  • 2,054
  • 6
  • 28
  • 43
14
votes
6 answers

Python: create a new column from existing columns

I am trying to create a new column based on both columns. Say I want to create a new column z, and it should be the value of y when it is not missing and be the value of x when y is indeed missing. So in this case, I expect z to be [1, 8, 10, 8]. …
Kexin Xu
  • 691
  • 3
  • 10
  • 20
14
votes
2 answers

Computed column based on another computed column?

I have a computed column called Cost that returns money. I want to have another column that returns (Cost * 2), but it doesn't allow me.
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
13
votes
3 answers

How do I create a new column based on multiple conditions from multiple columns?

I'm trying add a new column to a data frame based on several conditions from other columns. I have the following data: > commute <- c("walk", "bike", "subway", "drive", "ferry", "walk", "bike", "subway", "drive", "ferry", "walk", "bike", "subway",…
12
votes
2 answers

How to compute weighted sum of all elements in a row in pandas?

I have a pandas data frame with multiple columns. I want to create a new column weighted_sum from the values in the row and another column vector dataframe weight weighted_sum should have the following value: row[weighted_sum] = row[col0]*weight[0]…
ask
  • 2,160
  • 7
  • 31
  • 42
12
votes
3 answers

Best Practices for Computed Column in SQL Server

I'm working with a user table and want to put a "end of probational period date". Basically, each new user has 2 full months from when they join as part of their probation period. I saw that I can put a formula in the column for my user table, but…
Brian
  • 131
  • 1
  • 1
  • 4
12
votes
3 answers

Pivot Calculated formula: SUM(Field1)/AVG(Field2)

I've a simple table with some amount and interval in sec by date and product name. Month | Product | Amount | Interval in sec ------------------------------------------ 05-'12| Prod A | 10 | 5 05-'12| Prod A | 3 | 5 05-'12| Prod B | 4 …
Bas
  • 597
  • 5
  • 10
  • 22
11
votes
4 answers

TSQL divide by zero encountered despite no columns containing 0

I've been trying to understand why I get a "divide by zero encountered" (Msg 8134) with my SQL query, but I must be missing something. I would like like to know the why for the specific case below, I am not looking for NULLIF, CASE WHEN... or…
Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89