聚合可能不会出现在计算列表达式或检查约束中. 使用 MAX 函数时出错



我正在接收

聚合可能不会出现在计算列表达式中或检查约束错误

同时使用日期列的MAX函数来创建表格。以下是我用于创建表的代码:

Create table Bills
( 
BillNo INT NOT NULL, 
MeterNo INT NOT NULL, 
FirstDate datetime NOT NULL, 
FirstDateReading INT NOT NULL, 
CurrentDate as MAX(FirstDate),
CurrentDateReadng INT NOT NULL, 
LastDate as DATEADD(m,-1,CurrentDate),
LastDateReading INT NOT NULL, 
Usageinm3 as (CurrentDateReadng-LastDateReading), 
Usageinlitres as (Usageinm3 * 1000), 
TotalBill as (UsageinLitres * 3) 
Primary Key (BillNo), 
Foreign Key (MeterNo) References MeterReading(MeterNo) 
);

由于您正在定义表结构,因此您应该为每列输入数据类型,而不是值。

Create table Bills
( 
BillNo INT NOT NULL, 
MeterNo INT NOT NULL, 
FirstDate datetime NOT NULL, 
FirstDateReading INT NOT NULL, 
CurrentDate as DATE, <--should be the data type
...

如果要基于其他表创建表,则应使用类似

SELECT billno,
meterno,
firstdate,
firstdatereading,
MAX(firstdate)
...
INTO Bills
FROM sourcetable
WHERE conditions
GROUP BY billno,
meterno,
firstdate,
firstdatereading
...

最新更新