DateAdd()中的嵌套SELECT语句

时间:2015-01-28 15:26:01

标签: sql sql-server tsql select nested

我正在尝试获取列的MAX()并使用该值执行DATEADD()操作,

CONVERT(Datetime, DATEADD(MONTH, tb_files.file_retentiondate, MAX((SELECT tb_records.rec_record_date FROM tb_records)))) AS ExpiryDate

我试过的是:

SELECT tb_files.file_pk AS 'File Number',
       tb_files.file_subject AS 'File Subject',
       CONVERT(Datetime, DATEADD(MONTH, tb_files.file_retentiondate, MAX(
                                                                           (SELECT tb_records.rec_record_date
                                                                            FROM tb_records)))) AS ExpiryDate,
       tb_compartment.comp_cab_fk AS 'Storage Number',
       tb_compartment.comp_part AS 'Compartment Number',
       tb_departments.dept_code AS 'Department',
       tb_location.locationno AS 'Location',
       tb_users.usr_fullname AS 'Created by',
       tb_files.file_datecreated AS 'Date Created'
FROM tb_files
INNER JOIN tb_fileparts ON tb_files.file_pk = tb_fileparts.fp_file_fk
INNER JOIN tb_records ON tb_fileparts.fp_pk = tb_records.rec_filepart_fk
INNER JOIN tb_users ON tb_files.file_createdby_usr_fk = tb_users.usr_pk
INNER JOIN tb_compartment ON tb_files. file_comp_fk = tb_compartment. comp_pk
INNER JOIN tb_cabinet ON tb_compartment.comp_cab_fk = tb_cabinet.cab_pk
INNER JOIN tb_location ON tb_cabinet.cab_location_fk = tb_location.location_pk
INNER JOIN tb_departments ON tb_cabinet.cab_dept_fk = tb_departments.dept_pk
WHERE (tb_files.file_active = 1)
  AND (tb_records.rec_active = 1)
GROUP BY tb_files.file_pk,
         tb_files.file_subject,
         tb_files.file_retentiondate,
         tb_compartment.comp_cab_fk,
         tb_compartment.comp_part,
         tb_departments.dept_code,
         tb_location.locationno,
         tb_users.usr_fullname,
         tb_files.file_datecreated HAVING(Convert(DateTime, DateAdd(MONTH, tb_files.file_retentiondate, MAX(tb_records.rec_record_date))) <= getdate())

我收到此错误消息:

Msg 130, Level 15, State 1, Line 2
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

你能解释一下我做错了吗?

3 个答案:

答案 0 :(得分:2)

错误消息非常具有描述性。 SQL不允许您将子查询用于聚合函数的参数。简单的解决方案是在子查询中移动聚合:

   . . .
   CONVERT(Datetime, DATEADD(MONTH, tb_files.file_retentiondate,
           (SELECT MAX(tb_records.rec_record_date)
            FROM tb_records
           )
          ) AS ExpiryDate,

答案 1 :(得分:1)

你不能用这样的MAX()函数包装一个子查询...试试这个:

SELECT tb_files.file_pk AS 'File Number',
       tb_files.file_subject AS 'File Subject',
       CONVERT(Datetime, DATEADD(MONTH, tb_files.file_retentiondate, (SELECT MAX(tb_records.rec_record_date) FROM tb_records))) AS ExpiryDate,
       tb_compartment.comp_cab_fk AS 'Storage Number',
       tb_compartment.comp_part AS 'Compartment Number',
       tb_departments.dept_code AS 'Department',
       tb_location.locationno AS 'Location',
       tb_users.usr_fullname AS 'Created by',
       tb_files.file_datecreated AS 'Date Created'
FROM tb_files
INNER JOIN tb_fileparts ON tb_files.file_pk = tb_fileparts.fp_file_fk
INNER JOIN tb_records ON tb_fileparts.fp_pk = tb_records.rec_filepart_fk
INNER JOIN tb_users ON tb_files.file_createdby_usr_fk = tb_users.usr_pk
INNER JOIN tb_compartment ON tb_files. file_comp_fk = tb_compartment. comp_pk
INNER JOIN tb_cabinet ON tb_compartment.comp_cab_fk = tb_cabinet.cab_pk
INNER JOIN tb_location ON tb_cabinet.cab_location_fk = tb_location.location_pk
INNER JOIN tb_departments ON tb_cabinet.cab_dept_fk = tb_departments.dept_pk
WHERE (tb_files.file_active = 1)
  AND (tb_records.rec_active = 1)
GROUP BY tb_files.file_pk,
         tb_files.file_subject,
         tb_files.file_retentiondate,
         tb_compartment.comp_cab_fk,
         tb_compartment.comp_part,
         tb_departments.dept_code,
         tb_location.locationno,
         tb_users.usr_fullname,
         tb_files.file_datecreated HAVING(Convert(DateTime, DateAdd(MONTH, tb_files.file_retentiondate, MAX(tb_records.rec_record_date))) <= getdate())

答案 2 :(得分:0)

将最大查询更改为..

SELECT MAX(A.rec_record_date) FROM tb_records A

在您编写查询的方式中,将对结果集中的每一行反复计算整个表的MAX record_Date。我希望您创建一个CTE/subquery并重用值...