如何成功消除Group By?

时间:2018-05-02 20:57:42

标签: group-by

当我运行以下查询时,我得到一个包含238"礼物的单行结果。"如何查询返回238 LINES以便我可以看到与EACH礼物相关的收入?我试图消除Group By然后它生气并说我的选择没有意义(错误:"列' campcode.campcode'在选择列表中无效,因为它不是包含在聚合函数或GROUP BY子句中。")。

select
c2.campcode,
substring(s1.source,1,6) as Package,
p.descrip as Panel,
pc.packcode,
pc.descrip,
COUNT(m1.names_id) as Mailed,
SUM(case when s1.numbermail=0 then 0 else (s1.acost/s1.numbermail) end) as Cost,
COUNT(c1.camt) as Gifts,
SUM(c1.camt) as Income


from mailing    m1 
left join source    s1 with (nolock) on m1.source_key=s1.source_key
left join campcode c2 with (nolock) on s1.camp_key=c2.camp_key
left join packcode pc with (nolock) on pc.pack_key=s1.pack_key
left join panelcode p with (nolock) on p.panel_key = s1.panel_key
left join contrib c1 with (nolock) on m1.source_key = c1.source_key and 
m1.names_id = c1.names_id 
and     c1.moneytype <> -1            
and     c1.type <> 6  
and     c1.camt >0

where       c2.campcode like '18D9Y'   
and substring(s1.source,1,6) like '18D9YC' 
and     c1.cdate between '2017-09-01' and '2018-04-23'

group by    
c2.campcode,
substring(s1.source,1,6),
p.descrip,
pc.packcode,
pc.descrip

0 个答案:

没有答案