troubleshooting Question

T-SQL: Cannot perform an aggregate function...

Avatar of Software Engineer
Software Engineer asked on
Microsoft SQL Server* T-SQLSQL
23 Comments1 Solution124 ViewsLast Modified:
Hi:

Below is my code, and the following are my results.

I want the finished good weight to be divided by the sum of the LOTQTYs shown in the last four records that I have highlighted in red, where the last two columns do not show records with 0.

And, I want this field to reside as the last column in the results.  The column (field) will be called "YIELD".

Please tell me what syntax can be used to accomplish this, without getting the error saying "cannot perform an aggregate function...".

Thank you!

Software Engineer

select DISTINCT [INVHDRBATCH], 
[FGBATCH], [FG], CONVERT(DECIMAL(10,2), [FGMAT]) as [FGMAT], 
CONVERT(DECIMAL(10,2), [FGLABOR]) as [FGLABOR], CONVERT(DECIMAL(10,2), [FGOH]) as [FGOH], 
CONVERT(DECIMAL(10,2), [FGCOST]) as [FGCOST], 
CONVERT(DECIMAL(10,2),[FGWEIGHT]) as [FGWEIGHT], [MATLB] as [MATLB], 
[LABORLB] as [LABORLB], 
[OHLB] as [OHLB],
--[FORMULA], [FORMULAMAT], [FORMULALABOR], [FORMULAOH], 
rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar)) as [Component],
rtrim(cast([LOT] as varchar)) as [LOT], CONVERT(DECIMAL(10,2), WIPTEST.[LOTQTY]) as [LOTQTY], 
CONVERT(DECIMAL(10,2), [LOTMATUNITCOST]) as [LOTMATUNITCOST], 
CONVERT(DECIMAL(10,2), [LOTMATWIPCOST]) as [LOTMATWIPCOST], 
case when rtrim(cast([LOT] as varchar)) LIKE rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar))+'%' 
THEN CONVERT(DECIMAL(10,2), [LOTLABORWIPCOST]) else 0 end as [LOTLABORWIPCOST], 
case when rtrim(cast([LOT] as varchar)) LIKE rtrim(cast((select DISTINCT IV30300.ITEMNMBR) as varchar))+'%' 
THEN CONVERT(DECIMAL(10,2), [LOTOHWIPCOST]) else 0 end as [LOTOHWIPCOST]
from WIPTEST
INNER JOIN IV30200 BATCH on WIPTEST.[INVHDRBATCH] = BATCH.BACHNUMB AND WIPTEST.[INVHDRDOC] = BATCH.DOCNUMBR
INNER JOIN IV30300 ON BATCH.TRXSORCE = IV30300.TRXSORCE AND BATCH.DOCNUMBR = IV30300.DOCNUMBR
INNER JOIN IV30400 ON IV30300.TRXSORCE = IV30400.TRXSORCE
INNER JOIN BM_View_SL_BatchTicketAuditLog LOG on BATCH.BACHNUMB = LOG.BatchTicket AND BATCH.DOCNUMBR = LOG.GPDocNo
where LOT IS NOT NULL and LOG.FGItem = ''
and WIPTEST.[FG] = '506' and WIPTEST.[FGBATCH] = '50418G12D' and WIPTEST.[INVHDRBATCH] = '50418G12D' 
UNION ALL
select DISTINCT '' as [INVHDRBATCH], 
'' as [FGBATCH], '' as [FG], NULL as [FGMAT], NULL as [FGLABOR], NULL as [FGOH], NULL as [FGCOST], NULL as [FGWEIGHT], NULL as [MATLB], 
NULL as [LABORLB], NULL as [OHLB], 
'' as [Component], '' as [LOT], NULL as [LOTQTY], NULL as [LOTMATUNITCOST], NULL as [LOTMATWIPCOST], NULL as [LOTLABORWIPCOST], NULL as [LOTOHWIPCOST]
from WIPTEST 
INNER JOIN IV30400 LOTNO ON WIPTEST.Component = LOTNO.ITEMNMBR AND WIPTEST.LOT = LOTNO.SERLTNUM
INNER JOIN IV30200 on WIPTEST.[INVHDRBATCH] = IV30200.BACHNUMB AND WIPTEST.[INVHDRDOC] = IV30200.DOCNUMBR
where WIPTEST.[FG] = '506' and WIPTEST.[FGBATCH] = '50418G12D' and WIPTEST.[INVHDRBATCH] = '50418G12D'

Results.xlsx
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 23 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros