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
8
votes
2 answers

how to create calculated field in mysql?

i have a table like below: create table info (username varchar(30),otherinfo varchar(100)); now i want to alter this table to have new field and this field has to have default value as md5(username) something like below: alter table info add…
Alaa
  • 4,471
  • 11
  • 50
  • 67
8
votes
3 answers

How to create mean and s.d. columns in data.table

The following code/outcome baffles me as to why data.table returns NA for the mean functions and not the sd function. library(data.table) test <- data.frame('id'=c(1,2,3,4,5), 'A'=seq(2,9,length=5), …
nfmcclure
  • 3,011
  • 3
  • 24
  • 40
8
votes
5 answers

Computed column should result to string

Here is a snap of my database. Both col1 and col2 are declared as int. My ComputedColumn currently adds the Columns 1 and 2, as follows... col1 col2 ComputedColumn 1 2 3 4 1 5 Instead of this, my ComputedColumn should join the…
OrElse
  • 9,709
  • 39
  • 140
  • 253
8
votes
1 answer

Dealing with NaN when calculating means

I want to create a new column that contains the average two other columns. For example by original table (dat) looks like this: A B 1 1 NaN 2 3 2 3 2 5 4 4 4 5 6 NaN 6 5 3 I now want a column C that averages A and B,…
melanopygus
  • 131
  • 1
  • 1
  • 4
8
votes
1 answer

Sql Server Computed Column Formula syntax

I want to use a computed bit column that will be true if another column in the table is not null. What's the correct formula for this? HasLabel = computed column (bit) Label = varchar NULL The following formula does not validate. - what am I…
Jeremy
  • 9,023
  • 20
  • 57
  • 69
8
votes
2 answers

SQL Server add computed column in VIEW with conditions

Here is my situation: In my table i two fields: - Price (decimal (15,4) - TaxId (int) TaxId value is not stored anywhere but there are only two values (actualy 3). - 1 (8.5%) - 2 (20%) - NULL (no tax) Now i need calculated column in my view that…
no9
  • 6,424
  • 25
  • 76
  • 115
7
votes
2 answers

Persisted computed column with subquery

I have something like this create function Answers_Index(@id int, @questionID int) returns int as begin return (select count([ID]) from [Answers] where [ID] < @id and [ID_Question] = @questionID) end go create table Answers ( [ID] int not…
Lukáš Novotný
  • 9,012
  • 3
  • 38
  • 46
7
votes
4 answers

Adding virtual columns to current table in Doctrine?

I'm using Doctrine 1.2 with Symfony 1.4. Let's say I have a User model, which has one Profile. These are defined as: User: id username password created_at updated_at Profile: id user_id first_name last_name address city postal_code I would…
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
7
votes
1 answer

Adding a computed column to an ActiveRecord query

I am running a query using a scope and some conditions. Something like this: conditions[:offset] = (options[:page].to_i - 1) * PAGE_SIZE unless options[:page].blank? conditions[:limit] = options[:limit] ||= PAGE_SIZE scope =…
rmw
  • 1,253
  • 1
  • 12
  • 20
7
votes
2 answers

Modelling stock considering orders in Rails

I am developing a rental marketplace application with Rails 5. It has a quite simple structure with users, products and orders, with the products belonging to users, who actually create them. In order to let the users manage a "live" stock of their…
7
votes
3 answers

How do I add a calculated column to my EF4 model?

Given a "User" table and a "Login" table in MS SQL 2008: CREATE TABLE [dbo].[User_User]( [UserID] [int] IDENTITY(1000,1) NOT NULL, [UserName] [varchar](63) NOT NULL, [UserPassword] [varchar](63) NOT NULL ) CREATE TABLE…
7
votes
2 answers

PostgreSQL Views: Referencing one calculated field in another calculated field

I have the same question as #1895500, but with PostgreSQL not MySQL. How can I define a view that has a calculated field, for example: (mytable.col1 * 2) AS times_two ... and create another calculated field that's based on the first one: …
John
  • 267
  • 2
  • 4
  • 9
7
votes
1 answer

Using computed DateTime columns in Entity Framework

StoreGeneratedPattern="Computed" works flawlessly on columns of type timestamp. But you can't display a TimeStamp as a human-readable form. So you can't show the user "This row was modified on {TimeStamp}" because you simply can't parse a SQL…
SeToY
  • 5,777
  • 12
  • 54
  • 94
6
votes
2 answers

define a computed column reference another table

I have two database tables, Team (ID, NAME, CITY, BOSS, TOTALPLAYER) and Player (ID, NAME, TEAMID, AGE), the relationship between the two tables is one to many, one team can have many players. I want to know is there a way to define a TOTALPLAYER…
James
  • 2,570
  • 7
  • 34
  • 57
6
votes
2 answers

How to use a MySQL column alias for calculations?

How can I use my column alias (lat and lng) from the two subqueries to make the distance calcuation underneath? What I am basically trying to do is is to calculate the distance between two locations using longitude and latitude values. But somehow…
chrismaaz
  • 127
  • 4
  • 12