PostgreSQL是否支持计算/计算列,如MS SQL Server?我在文档中找不到任何内容,但由于此功能包含在许多其他DBMS中,我认为我可能会遗漏某些内容.
postgresql materialized-views calculated-columns sql-view generated-columns
我正在创建一个跨越字段的计算列,其中一些可能为null.
问题是如果这些字段中的任何一个为null,则整个计算列将为null.我从Microsoft文档中了解到这是预期的,可以通过SET CONCAT_NULL_YIELDS_NULL设置关闭.但是,我不想更改此默认行为,因为我不知道它对SQL Server其他部分的影响.
有没有办法让我只检查一列是否为空,如果它不为空,只在计算列公式中附加其内容?
我需要在我的数据库中有一列由数据库计算为(行数之和) - (行数b).我正在使用代码优先模型来创建我的数据库.
这就是我的意思:
public class Income {
[Key]
public int UserID { get; set; }
public double inSum { get; set; }
}
public class Outcome {
[Key]
public int UserID { get; set; }
public double outSum { get; set; }
}
public class FirstTable {
[Key]
public int UserID { get; set; }
public double Sum { get; set; }
// This needs to be calculated by DB as
// ( Select sum(inSum) FROM Income WHERE UserID …
Run Code Online (Sandbox Code Playgroud) c# sql-server calculated-columns ef-code-first entity-framework-5
我有一个使用以下行创建的计算列:
alter table tbPedidos
add restricoes as (cast(case when restricaoLicenca = 1 or restricaoLote = 1 then 1 else 0 end as bit))
Run Code Online (Sandbox Code Playgroud)
但是,现在我需要更改此列,例如:
alter table tbPedidos
alter column restricoes as (cast(case when restricaoLicenca = 1 or restricaoLote = 1 or restricaoValor = 1 then 1 else 0 end as bit))
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我正在尝试为case语句输入另一个条件,但它不起作用.
非常感谢!
首先:感谢@MattDowle; data.table
是我开始使用以来发生过的最好的事情之一R
.
第二:我知道变量列名的各种用例的许多变通方法data.table
,包括:
可能更多我没有参考.
但是:即使我学会了上面记录的所有技巧,以至于我从来不必查看它们以提醒自己如何使用它们,我仍然会发现使用作为参数传递给函数的列名非常繁琐的任务.
我正在寻找的是以下解决方法/工作流程的"最佳实践认可"替代方案.考虑到我有一堆类似数据的列,并希望对这些列或它们的集合执行一系列类似的操作,其中操作具有任意高的复杂性,并且列名称组传递给指定的每个操作在变量中.
我意识到这个问题听起来很人为,但我却以惊人的频率遇到它.这些例子通常非常混乱,很难将与这个问题相关的功能分开,但我最近偶然发现了一个相当简单的简化用作MWE的方法:
library(data.table)
library(lubridate)
library(zoo)
the.table <- data.table(year=1991:1996,var1=floor(runif(6,400,1400)))
the.table[,`:=`(var2=var1/floor(runif(6,2,5)),
var3=var1/floor(runif(6,2,5)))]
# Replicate data across months
new.table <- the.table[, list(asofdate=seq(from=ymd((year)*10^4+101),
length.out=12,
by="1 month")),by=year]
# Do a complicated procedure to each variable in some group.
var.names <- c("var1","var2","var3")
for(varname in var.names) {
#As suggested in an answer to Link 3 above
#Convert the column name to a …
Run Code Online (Sandbox Code Playgroud) 考虑一下这个表: c_const
code | nvalue
--------------
1 | 10000
2 | 20000
Run Code Online (Sandbox Code Playgroud)
和另一张桌子 t_anytable
rec_id | s_id | n_code
---------------------
2 | x | 1
Run Code Online (Sandbox Code Playgroud)
目标是s_id
基于以下公式成为计算列:
rec_id*(select nvalue from c_const where code=ncode)
Run Code Online (Sandbox Code Playgroud)
这会产生错误:
在此上下文中不允许子查询.只允许标量表达式.
如何使用另一个表的列作为输入来计算此计算列的值?
我在postgres中使用计算列时遇到问题.下面给出了一个在SQL中工作的类似代码,是否可以重新创建它PostgreSQL
?
select cost_1, quantity_1, cost_2, quantity_2,
(cost_1 * quantity_1) as total_1,
(cost_2 * quantity_2) as total_2,
(calculated total_1 + calculated total_2) as total_3
from data;
Run Code Online (Sandbox Code Playgroud)
在PostgreSQL
类似的代码中返回错误:
列total_1和total_2不存在.
(新的SQLAlchemy用户警报)我有三个表:一个人,从特定日期开始的人每小时费率和每日时间报告.我正在寻找正确的方法,以便在当天的人员每小时费率中扣除时间费用.
是的,我可以在创建时计算该值,并将其作为模型的一部分,但请将此视为总结幕后更复杂数据的示例.我如何计算Time.cost?它是hybrid_propery,column_property还是完全不同的东西?
class Person(Base):
__tablename__ = 'person'
personID = Column(Integer, primary_key=True)
name = Column(String(30), unique=True)
class Payrate(Base):
__tablename__ = 'payrate'
payrateID = Column(Integer, primary_key=True)
personID = Column(Integer, ForeignKey('person.personID'))
hourly = Column(Integer)
starting = Column(Date)
__tableargs__ =(UniqueConstraint('personID', 'starting',
name='uc_peron_starting'))
class Time(Base):
__tablename__ = 'entry'
entryID = Column(Integer, primary_key=True)
personID = Column(Integer, ForeignKey('person.personID'))
workedon = Column(Date)
hours = Column(Integer)
person = relationship("Person")
def __repr__(self):
return "<{date} {hours}hrs ${0.cost:.02f}>".format(self,
date=self.workedon.isoformat(), hours=to_hours(self.hours))
@property
def cost(self):
'''Cost of entry
'''
## This is where I …
Run Code Online (Sandbox Code Playgroud) 这是我的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 0.680566 0.538655 0.038236 1.169403
Run Code Online (Sandbox Code Playgroud)
我想在行中明智地总结数据:
ds['sum']=ds.sum(axis=1)
ds
A B C D sum
1 0.095389 0.556978 1.646888 1.959295 4.258550
2 1.076190 2.668270 0.825116 1.477040 6.046616
3 0.245034 1.066285 0.967124 0.791606 3.070049
Run Code Online (Sandbox Code Playgroud)
现在,我的问题来了!我想创建4个新列,并计算每行中总和(总和)的百分比值.因此,第一个新列中的第一个值应为(0.095389/4.258550),第二个新列中的第一个值(0.556978/4.258550)......依此类推......请帮助
我有以下用户定义的函数:
create function [dbo].[FullNameLastFirst]
(
@IsPerson bit,
@LastName nvarchar(100),
@FirstName nvarchar(100)
)
returns nvarchar(201)
as
begin
declare @Result nvarchar(201)
set @Result = (case when @IsPerson = 0 then @LastName else case when @FirstName = '' then @LastName else (@LastName + ' ' + @FirstName) end end)
return @Result
end
Run Code Online (Sandbox Code Playgroud)
我无法使用此函数在计算列上创建索引,因为它不是确定性的.有人可以解释为什么它不是确定性的,最终如何修改以使其具有确定性?谢谢
sql function calculated-columns non-deterministic sql-server-2008
sql ×4
postgresql ×2
python ×2
sql-server ×2
alter ×1
c# ×1
data.table ×1
edit ×1
function ×1
null ×1
pandas ×1
r ×1
sql-view ×1
sqlalchemy ×1