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
103
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; } …
99
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
  • 75,813
  • 86
  • 255
  • 348
93
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
  • 931
  • 1
  • 6
  • 3
71
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
  • 6,420
  • 20
  • 70
  • 94
64
votes
3 answers

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

What I'm looking for is a "best-practices-approved" alternative to the following workaround / workflow. Consider that I have a bunch of columns of similar data, and would like to perform a sequence of similar operations on these columns or sets of…
Philip
  • 7,253
  • 3
  • 23
  • 31
53
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. TURKEN
  • 1,554
  • 4
  • 21
  • 36
48
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
  • 772
  • 1
  • 8
  • 16
48
votes
9 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) …
47
votes
2 answers

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 …
Yari
  • 867
  • 3
  • 9
  • 13
46
votes
3 answers

Create new columns and fill with calculated values from same dataframe

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['sum'] = ds.sum(axis=1) which looks like A B C D sum 1 0.095389 0.556978 1.646888 …
jonas
  • 13,559
  • 22
  • 57
  • 75
42
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 =…
40
votes
3 answers

Use a calculated column in a where clause

I'm trying to use a calculated column in a where clause. I've been trying everything from CROSS APPLY, to sub-query select but it does not give me anything near what I need. My query so far: SELECT p.Code, c.AccountNumber, Sales = (SUM(p.UnitPrice)…
JadedEric
  • 1,943
  • 2
  • 26
  • 49
32
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,255
  • 8
  • 45
  • 71
31
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,211
  • 4
  • 18
  • 27
31
votes
3 answers

Can I create computed columns in SQLite?

What would be the syntax (if it's possible), for example, to create a table called Car_Model that has a foreign key to a table Car_Make, and give Car_Make a column which is the number of Car_Models that exist of that Car_Make. (If this seems trivial…
Ej.
  • 321
  • 1
  • 3
  • 4
1
2 3
99 100