sql server - Grouping multiple queries on same table , TQSL -
i have rather large table of inormation needs filtered differently several different work departments. created below query returns same number of columns should able union'd up. want make sure no duplicate rows returned, hence reason used union, keep getting error "msg 8120, level 16, state 1, line 53 column 'act_dw.dbo.inv-act.dnum' invalid in select list because not contained in either aggregate function or group clause."
see issue here? looking 1 big table of records grouped deskname field per workgroup if possible.
declare @inventoryasofeod date, @start date, @end date set @start = cast(dateadd(month, datediff(month, 0, getdate()),0) date) set @end = cast(dateadd(day, -(day(dateadd(month, 1, getdate()))), dateadd(month, 1, getdate())) date) if datename(dw, getdate()) = 'monday' begin set @inventoryasofeod = cast(dateadd(d, -3, getdate())as date) end else set @inventoryasofeod = cast(dateadd(d, -1, getdate()) date) print @inventoryasofeod print @start print @end select dnum, ssn, dtrfirstname, dtrlastname, assigndate, [desk], deskname, pymtamt1, duedate, pni, pibalatsetup, ttl_balance, pmgroup, progtype, office, client, batch, placetype, pymtdt1, ttl_starts (select dnum, ssn, dtrfirstname, dtrlastname, assigndate, [desk], deskname, pymtamt1, progstartdt duedate, sum([prinbal]+[intbal]) pni, pibalatsetup, balance ttl_balance, pmgroup, progtype, office, client, batch, placetype, pymtdt1, count(ssn) ttl_starts [act_dw].[dbo].[inv-act] with(index([ix_inv-act_inventoryasofeod]),nolock) inventoryasofeod = @inventoryasofeod , bc ='gsl' , progtype not in('pmt plan', 'fallout', 'pif', 'sif') , progtype not null , pymtdt1 between @start , @end union --get oregon starts select dnum, ssn, dtrfirstname, dtrlastname, assigndate, [desk], deskname, pymtamt1, progstartdt duedate, sum([prinbal]+[intbal]) pni, pibalatsetup, balance ttl_balance, pmgroup, progtype, office, client, batch, placetype, pymtdt1, count(ssn) ttl_starts [act_dw].[dbo].[inv-act] with(index([ix_inv-act_inventoryasofeod]),nolock) inventoryasofeod = @inventoryasofeod , client 'ordor%' , progtype not in ('null', 'fallout') , pymtdt1 between @start , @end union --get perkins starts select dnum, ssn, dtrfirstname, dtrlastname, assigndate, [desk], deskname, pymtamt1, progstartdt duedate, sum([prinbal]+[intbal]) pni, pibalatsetup, balance ttl_balance, pmgroup, progtype, office, client, batch, placetype, pymtdt1, count(ssn) ttl_starts [act_dw].[dbo].[inv-act] with(index([ix_inv-act_inventoryasofeod]),nolock) inventoryasofeod = @inventoryasofeod , bc in ('pe1', 'pe2', 'pe3', 'nu1', 'nu2', 'nds') , progtype not in ('null', 'fallout') , pymtdt1 between @start , @end union --get starts tuition select dnum, ssn, dtrfirstname, dtrlastname, assigndate, [desk], deskname, pymtamt1, progstartdt duedate, sum([prinbal]+[intbal]) pni, pibalatsetup, balance ttl_balance, pmgroup, progtype, office, client, batch, placetype, pymtdt1, count(ssn) ttl_starts [act_dw].[dbo].[inv-act] with(index([ix_inv-act_inventoryasofeod]),nolock) inventoryasofeod = @inventoryasofeod , bc in ('ar1', 'ar2', 'in1', 'in2', 'in3', 'tu1', 'tu2', 'tu3') , progtype not in ('null', 'fallout') , pymtdt1 between @start , @end union --get starts chase select dnum, ssn, dtrfirstname, dtrlastname, assigndate, [desk], deskname, pymtamt1, progstartdt duedate, sum([prinbal]+[intbal]) pni, pibalatsetup, balance ttl_balance, pmgroup, progtype, office, client, batch, placetype, pymtdt1, count(ssn) ttl_starts [act_dw].[dbo].[inv-act] with(index([ix_inv-act_inventoryasofeod]),nolock) inventoryasofeod = @inventoryasofeod , client 'ag%' , progtype not in ('null', 'fallout') , pymtdt1 between @start , @end) startstable group dnum, ssn, dtrfirstname, dtrlastname, assigndate, pymtamt1, duedate, pibalatsetup, ttl_balance, progtype, desk, deskname, pmgroup, office, client, batch, placetype, pymtdt1 order pmgroup, desk
you need include group by
clause each subquery includes aggregates. instead union rows , then aggregate, different query.
Comments
Post a Comment