troubleshooting Question

SQL query for a view -  problem with grouping

Avatar of GracefBase
GracefBaseFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft SQL Server 2008
4 Comments1 Solution184 ViewsLast Modified:
Hello there experts,
Trying to amend a view so I can show the total of small cartons as well as the total cartons for a shipment.
I'm getting the error
"Column 'AutoPick.Status' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
If I add this to the GROUP BY clause instead of getting one line of data I get one for each of these Autopick.Statuses - here is the query.

select m.mrid, .exdate1, invAddr.companyid as invoiceAddr,
      invAddr.code, m.sourceid, invAddr.Dropseqnumber, dest.companyid as destid, dest.pc,
      case (dest.pcgdesc)
      when '' then (case m.TimeExp when '00:00:00' then
      'false' else 'true' End )
      else 'true' END
      as timed,
      case (dest.DropSeqNumber)
      when 1 then 'true'
      else 'false' END
      as tailLift,


sum(autopick.Amount)
as ctns,

(Select COUNT(OriginalPID) from Pallets WHERE  SType = 6 AND Autopick.Status = Pallets.Status
GROUP by Pallets.OriginalPID) as SmlCtns


FROM  m
INNER JOIN Companies source on m.SourceId = source.CompanyId
INNER JOIN companies invAddr on (dbo.invoiceTo(m.mrid) = invAddr.CompanyId)
INNER JOIN Companies dest on mrheader.DestId = dest.CompanyId
INNER JOIN MrLines on m.MrId=MrLines.MrId
INNER JOIN AutoPick on mrlines.MrId = autopick.MrIdOut and mrlines.MrLineNo=autopick.MrLineNoOut


WHERE       m.allowcharge=1
      
      
      AND dbo.invoiceTo(mrheader.mrid)=invAddr.companyid
      AND mrtype=2
      and locateit.dbo.invoiceto(mrheader.mrid) is not null
      AND invAddr.Code  <> 'CKF'
      AND DestId not in (1342, 7308)
      AND ExDate1 > '2011-05-31 00:00:00'
      
      group by m.mrid, m.ExDate1, m.TimeExp, m.sourceid,
      invAddr.companyid, invAddr.code, invAddr.Dropseqnumber, dest.companyid, dest.pc, dest.pcgdesc,
      dest.DropSeqNumber

Thanks in advance for any help.
ASKER CERTIFIED SOLUTION
dqmq

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
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 4 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