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.

1837 questions
124
votes
7 answers

Computed / calculated / virtual / derived columns in PostgreSQL

Does PostgreSQL support computed / calculated columns, like MS SQL Server? I can't find anything in the docs, but as this feature is included in many other DBMSs I thought I might be missing something. Eg:…
88
votes
10 answers

SQL Server String Concatenation with Null

I am creating a computed column across fields of which some are potentially null. The problem is that if any of those fields is null, the entire computed column will be null. I understand from the Microsoft documentation that this is expected and…
Alex
  • 71,233
  • 79
  • 245
  • 337
85
votes
7 answers

Calculated column in EF Code First

I need to have one column in my database calculated by database as (sum of rows) - (sum of rowsb). I'm using code-first model to create my database. Here is what I mean: public class Income { [Key] public int UserID { get; set; } …
69
votes
5 answers

PostgreSQL: using a calculated column in the same query

I am having trouble using a calculated column in postgres. A similar code which works in SQL is given below, is it possible to recreate this in PostgreSQL? select cost_1, quantity_1, cost_2, quantity_2, (cost_1 * quantity_1) as total_1, …
user1146150
  • 691
  • 1
  • 5
  • 3
62
votes
3 answers

How can one work fully generically in data.table in R with column names in variables

First of all: thanks to @MattDowle; data.table is among the best things that ever happened to me since I started using R. Second: I am aware of many workarounds for various use cases of variable column names in data.table, including: Select /…
Philip
  • 6,273
  • 3
  • 21
  • 28
62
votes
4 answers

How can I alter this computed column in SQL Server 2008?

I have a computed column created with the following line: alter table tbPedidos add restricoes as (cast(case when restricaoLicenca = 1 or restricaoLote = 1 then 1 else 0 end as bit)) But, now I need to change this column for something like: alter…
André Miranda
  • 5,980
  • 20
  • 67
  • 94
49
votes
2 answers

formula for computed column based on different table's column

Consider this table: c_const code | nvalue -------------- 1 | 10000 2 | 20000 and another table t_anytable rec_id | s_id | n_code --------------------- 2 | x | 1 The goal is to have s_id be a computed column, based…
Adnan M. TÜRKEN
  • 1,434
  • 3
  • 22
  • 35
39
votes
2 answers

Sql Server deterministic user-defined function

I have the following user-defined function: create function [dbo].[FullNameLastFirst] ( @IsPerson bit, @LastName nvarchar(100), @FirstName nvarchar(100) ) returns nvarchar(201) as begin declare @Result nvarchar(201) set @Result =…
39
votes
2 answers

pandas dataframe create new columns and fill with calculated values from same df

Here is a simplified example of my df: ds = pd.DataFrame(np.abs(randn(3, 4)), index=[1,2,3], columns=['A','B','C','D']) ds A B C D 1 1.099679 0.042043 0.083903 0.410128 2 0.268205 0.718933 1.459374 0.758887 3 …
jonas
  • 10,523
  • 21
  • 53
  • 71
38
votes
2 answers

SQLAlchemy calculated column

(New SQLAlchemy user alert) I have three tables: a person, the persons hourly rate starting at a specific date, and daily time reporting. I am looking for the correct way to have the cost for a Time base off of the persons hourly rate on that…
Frustrated
  • 646
  • 1
  • 7
  • 14
37
votes
1 answer

Absolute value for column in Python

How could I convert the values of column 'count' to absolute value? A summary of my dataframe this: datetime count 0 2011-01-20 00:00:00 14.565996 1 2011-01-20 01:00:00 10.204177 2 2011-01-20 02:00:00 -1.261569 3 2011-01-20…
Yari
  • 707
  • 3
  • 7
  • 13
34
votes
8 answers

getting "No column was specified for column 2 of 'd'" in sql server cte?

I have this query, but its not working as it should, with c as (select month(bookingdate) as duration, count(*) as totalbookings from entbookings group by month(bookingdate) …
30
votes
5 answers

Calculated properties in Entity Framework

Suppose I have an Employee object with the following properties: string Name { get; } float Hours { get; } float Wage { get; } I want to add a property, Salary, which equals Hours * Wage. In an ordinary business object, I would simply code that up…
Chris B. Behrens
  • 6,384
  • 8
  • 41
  • 67
28
votes
2 answers

SQL Server 2005 Computed Column Is Persisted

I have some computed columns in a table and need to know if I should set Is Persisted to true. What are the advantages? Are there any disadvantages? What does 'Is Persisted' mean?
hilary
  • 634
  • 3
  • 8
  • 11
28
votes
2 answers

The condition has length > 1 and only the first element will be used

I have a dataframe, trip: > head(trip.mutations) Ref.y Variant.y 1 T C 2 G C 3 A C 4 T C 5 C A 6 G A I want to add a third column, mutType, that follows these rules: for (i in 1:nrow(trip)) { if(trip$Ref.y=='G' &…
soosus
  • 1,161
  • 4
  • 16
  • 23
1
2 3
99 100