I tried this and got that is wanted the actual names in the group by but then I put the names of the fields in the group by and it still did not like it.
error and code fallow
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.PMO_DSC' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'sub.KEY_CDE' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_MAIL' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.GRO_PCT' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.OTH_CPM' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.TOTL_CST' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.AVG_REV' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.GRO_REV' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.PMO_DSC' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'sub.KEY_CDE' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
select a1, a2, a3,a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14
from(
select
CPN.PMO_DSC a1,
SUB.key_cde a2,
CPN.QTY_MAIL a3,
QTY_SOLD a4,
GRO_PCT a5,
0 a6,0 a7,0 a8,0 a9,
CAST(CPN.OTH_CPM AS DECIMAL(8,2)) a10,
CAST(CPN.TOTL_CST AS DECIMAL(8,2)) a11,
CAST(CPN.AVG_REV AS DECIMAL(8,2)) a12,
CAST(CPN.GRO_REV AS DECIMAL(8,2)) a13,
AVG(SUB.TERM) a14
from cirsub_m sub
LEFT join cpnpmo_m cpn on sub.key_cde = cpn.cpn_pmo1
left JOIN ARPPDH_M PDH ON SUB.ORD_NBR = PDH.DBT_NBR AND
SUB.PUB_CDE = PDH.SRC_REF
where cpn.pub_cde = 'FCM' AND substring(key_cde,1,1) = 'D' AND CTM_NBR = SRC_CTM AND
CPN.QTY_MAIL <> 0
union all
select
CPN.PMO_DSC a1,
SUB.key_cde a2,
0 a3,0 a4,0 a5,
COUNT(SUB.KEY_CDE) a6,
cast(ROUND(count(sub.key_cde)/qty_sold * 100,2)as decimal(8,2)) a7,
qty_sold - count(sub.key_cde) a8,
100 - cast(ROUND(count(sub.key_cde)/qty_sold * 100,2)as decimal(8,2)) a9,
0 a10,0 a11,0 a12,0 a13,0 a14
from cirsub_m sub
LEFT join cpnpmo_m cpn on sub.key_cde = cpn.cpn_pmo1
left JOIN ARPPDH_M PDH ON SUB.ORD_NBR = PDH.DBT_NBR AND
SUB.PUB_CDE = PDH.SRC_REF
where cpn.pub_cde = 'FCM' AND substring(key_cde,1,1) = 'D' AND SUB.BIL_STS = 'P' AND
CTM_NBR = SRC_CTM AND CPN.QTY_MAIL <> 0
) a
group by a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14
error and code fallow
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.PMO_DSC' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'sub.KEY_CDE' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_MAIL' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.GRO_PCT' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.OTH_CPM' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.TOTL_CST' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.AVG_REV' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.GRO_REV' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.PMO_DSC' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'sub.KEY_CDE' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
Server: Msg 8118, Level 16, State 1, Line 1
Column 'cpn.QTY_SOLD' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.
select a1, a2, a3,a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14
from(
select
CPN.PMO_DSC a1,
SUB.key_cde a2,
CPN.QTY_MAIL a3,
QTY_SOLD a4,
GRO_PCT a5,
0 a6,0 a7,0 a8,0 a9,
CAST(CPN.OTH_CPM AS DECIMAL(8,2)) a10,
CAST(CPN.TOTL_CST AS DECIMAL(8,2)) a11,
CAST(CPN.AVG_REV AS DECIMAL(8,2)) a12,
CAST(CPN.GRO_REV AS DECIMAL(8,2)) a13,
AVG(SUB.TERM) a14
from cirsub_m sub
LEFT join cpnpmo_m cpn on sub.key_cde = cpn.cpn_pmo1
left JOIN ARPPDH_M PDH ON SUB.ORD_NBR = PDH.DBT_NBR AND
SUB.PUB_CDE = PDH.SRC_REF
where cpn.pub_cde = 'FCM' AND substring(key_cde,1,1) = 'D' AND CTM_NBR = SRC_CTM AND
CPN.QTY_MAIL <> 0
union all
select
CPN.PMO_DSC a1,
SUB.key_cde a2,
0 a3,0 a4,0 a5,
COUNT(SUB.KEY_CDE) a6,
cast(ROUND(count(sub.key_cde)/qty_sold * 100,2)as decimal(8,2)) a7,
qty_sold - count(sub.key_cde) a8,
100 - cast(ROUND(count(sub.key_cde)/qty_sold * 100,2)as decimal(8,2)) a9,
0 a10,0 a11,0 a12,0 a13,0 a14
from cirsub_m sub
LEFT join cpnpmo_m cpn on sub.key_cde = cpn.cpn_pmo1
left JOIN ARPPDH_M PDH ON SUB.ORD_NBR = PDH.DBT_NBR AND
SUB.PUB_CDE = PDH.SRC_REF
where cpn.pub_cde = 'FCM' AND substring(key_cde,1,1) = 'D' AND SUB.BIL_STS = 'P' AND
CTM_NBR = SRC_CTM AND CPN.QTY_MAIL <> 0
) a
group by a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14