Hi @NBoddie ,
Welcome to the microsoft TSQL Q&A forum!
SQL Server does not support the parameter of the aggregate function to be a subquery or to include an aggregate function.In the following code, max is included in the parameter of the max function:
max(case when coalesce((SELECT MAX(date_shipped_sm) FROM jobscopedb.ppshipm WHERE ((carrier like '%KIT%') and ltrim(rtrim(substring(job_shipment_sm,1, charindex(' ', job_shipment_sm)))) = ltrim(rtrim(job_number_jbmstr)))), 0) = 0 then 99999999
else
(SELECT MAX(date_shipped) FROM JobscopeMeyer.dbo.ppshipm WHERE ((carrier NOT like '%KIT%') and ltrim(rtrim(substring(job_shipment,1, charindex(' ', job_shipment)))) = ltrim(rtrim(job_number))))
end) as ship_dt
To solve this problem, you can separate the max in the parameter through the derivation table:
select
ltrim(rtrim(job_number)) as job,
max(ltrim(rtrim(paint_code))) as paint_code,
max(ltrim(rtrim(d31.alphavalue))) as shipped_day,
max(COMPANY_CODE) as plant,
max(ltrim(rtrim(customer_name))) as customer,
max(date_promised) as promise_dt,
max(date_requested) as requested_dt,
max(ship_dt) as ship_dt,
max(price_curr) as sales_amount
from (select *,case when coalesce((SELECT MAX(date_shipped_sm) FROM jobscopedb.ppshipm WHERE ((carrier like '%KIT%') and ltrim(rtrim(substring(job_shipment_sm,1, charindex(' ', job_shipment_sm)))) = ltrim(rtrim(job_number_jbmstr)))), 0) = 0 then 99999999
else
(SELECT MAX(date_shipped) FROM JobscopeMeyer.dbo.ppshipm WHERE ((carrier NOT like '%KIT%') and ltrim(rtrim(substring(job_shipment,1, charindex(' ', job_shipment)))) = ltrim(rtrim(job_number))))
end) as ship_dt) from JobscopeMeyer.dbo.ipjobm) JobscopeMeyer.dbo.ipjobm
If you have any question, please feel free to let me know.
If the response is helpful, please click "Accept Answer" and upvote it.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.