Ok the only diffrence I see is the 'a' near the closing of the main select statement and I got this error:
Server: Msg 8155, Level 16, State 2, Line 1
No column was specified for column 6 of 'a'.
Server: Msg 8155, Level 16, State 1, Line 1
No column was specified for column 7 of 'a'.
Server: Msg 8155, Level 16, State 1, Line 1
No column was specified for column 8 of 'a'.
Server: Msg 8155, Level 16, State 1, Line 1
No column was specified for column 9 of 'a'.
UNion statements must have the exact same structure in each one of the union selects, Ie they must each have the same number of fields of the same datatype. The query will take it's names fromthe first selct inthe union. You didn;t have a column a6 named inthe first statement so it threw an error.
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,
SUB.key_cde,
0,0,0,
COUNT(SUB.KEY_CDE),
cast(ROUND(count(sub.key_cde)/qty_sold * 100,2)as decimal(8,2)),
qty_sold - count(sub.key_cde),
100 - cast(ROUND(count(sub.key_cde)/qty_sold * 100,2)as decimal(8,2)) ,
0,0,0,0,0
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
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.
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.