SQL:如何在聚合Select语句中包括非聚合

时间:2018-09-18 00:46:21

标签: sql teradata

我在Teradata查询下面有这个

create volatile table Sales_Record as (select max(a2.MDL_DESC),max(a3.DMD_DESC) FROM 
MDL_DD_DESC a2, DMD_DD_DESC a3);

尝试执行以下查询时:

create volatile table Sales_Record as (select a1.SAL_DT SAL_DT,max(a2.MDL_DESC),max(a3.DMD_DESC) FROM SAL_DD_DT a1,
MDL_DD_DESC a2, DMD_DD_DESC a3);

得到错误:

Selected non-aggregate values must be part of the associated group.

如何向聚合函数查询中添加a1.SAL_DT SAL_DT

1 个答案:

答案 0 :(得分:2)

您需要一个GROUP BY clause

select a1.SAL_DT SAL_DT,max(a2.MDL_DESC),max(a3.DMD_DESC) 
FROM SAL_DD_DT a1, MDL_DD_DESC a2, DMD_DD_DESC a3 
GROUP BY a1.SAL_DT

您确定这是您想要联接这些表的方式吗?似乎您正在获得它们之间所有可能组合的结果,而没有任何东西可以将表彼此关联,这几乎是任何人想要的。