繁体   English   中英

SQL计数或聚合错误

[英]SQL Count or Aggregation Error

这里是论坛的新手。 我的问题是如何获得一列的数量,并根据案例陈述进行展示? 我在论坛上进行了搜索,找到了与获取计数,总计和总计有关的答案,但是没有找到与案例陈述的附加条件有关的任何问题。 下面是我当前的代码。 该代码可以正常工作,但不能将信息带回所需的位置。

例如,假设某个特定程序在5个不同的站点进行了10次访问。 我如何使输出显示为:

visits  e4pv.site_providing_service_name,   State   location    program_name
2   a   b   c   d
2   b   b   c   d
2   c   b   c   d
2   d   b   c   d
2   e   b   c   d

查询是:

select distinct
count (e4pv.people_id) as visits,--field is not numeric
e4pv.site_providing_service_name,
e4pv.actual_date,
CASE

when --case_out_locations_based_on_states

end as State,       
CASE
when rcis.program_name in ('')  
    then rcis.managing_office_name
when rcis.program_name not in ('')  
    then rcis.profile_name
end as location,rcis.program_name, e4pv.event_name
from dbo.rpt_events_4people_view as e4pv
join dbo.rpt_critical_info_script as rcis with (nolock) on
e4pv.people_id = rcis.people_id and
e4pv.site_providing_service = rcis.managing_office_id
left outer join dbo.program_modifier_enrollment_view as pmev with(nolock) on
    rcis.people_id = pmev.people_id 
where   
rcis.program_name not in ('')                           
and e4pv.event_name in ('')
and date between '07/01/2015' and '06/30/2016'
GROUP BY 
e4pv.people_id,
e4pv.site_providing_service_name,
e4pv.actual_date,
rcis.managing_office_name,
rcis.profile_name,
rcis.program_name

谢谢你的协助

更新语法

我已经更新了语法,并建议使用子查询并将case语句添加到group by节中,但仍然没有得到结果。 该代码导致错误,例如组附近语法错误等。

select  visits,  State,  location, rcis.program_name, e4pv.event_name
 from (
 select distinct
count(e4pv.people_id) as visits,
e4pv.actual_date,
   CASE
   --cities mapped to states
    end as State,       
    CASE
 when rcis.program_name in ('') 
    then rcis.managing_office_name
 when rcis.program_name not in ('') 
    then rcis.profile_name
      end as location,
     rcis.program_name,
     e4pv.event_name

     from dbo.rpt_events_4people_view as e4pv

     join dbo.rpt_critical_info_script as rcis with (nolock) on
     e4pv.people_id = rcis.people_id and
     e4pv.site_providing_service = rcis.managing_office_id

     where  
   rcis.program_name not in ('')                            
   and e4pv.event_name in ('A')

       )

    GROUP BY 

     count(e4pv.people_id),
     rcis.profile_name,
     rcis.program_name,
      e4pv.event_name

我建议您使用临时表中的示例记录添加更多信息。 但是,此代码可以帮助您更清晰地提出自己的想法,以供其他人使用,或者可以推断出要使用的想法。 -假设:总部希望看到此人访问了与他相关的办公区域(西海岸或东海岸)的不同州

declare @table table (namee varchar(50),id int, stateeVisit varchar(5))
 insert @table select 'Gordin' ,1 , 'CA'
 insert @table select 'LingOff' ,2 , 'NY'
 insert @table select 'Ane' ,3 , 'CA'
 insert @table select 'Faheem' ,4 , 'PH'
 insert @table select 'George' ,5 , 'WA'
 --sample records added

; WITH CTE_AfterCase as (
 select 
    namee,
    id,
    stateeVisit,
    VisitToCoast = CASE WHEN stateeVisit in ('CA','WA') THEN 'WESTCOAST' ELSE         stateeVisit END 
 from @table ), 
 -- added conditional column in column table expression
 CTE_GroupedVisitCount as (
     select 
         A.VisitToCoast 
        ,A.stateeVisit as Statee
        ,count( A.id ) as visitcount

     from CTE_AfterCase A
     group by
       A.VisitToCoast 
        ,A.stateeVisit

        )  
-- grouped the required data
        select g.visitcount     ,rawdata.stateeVisit,rawdata.namee,rawdata.id,g.VisitToCoast  from     CTE_GroupedVisitCount g
        left join @table rawdata on g.statee = rawdata.stateeVisit
-- showed the grouping info with each record

希望能帮助到你

听起来您需要做的就是更改分组依据。 您需要完全按照要在结果中看到的组进行分组,而不是按照用于创建它们的所有字段进行分组。 因此,您要么必须将case语句完全按照select中显示的方式(不使用别名)复制到case语句中,要么可以使用已有的内容,选择people_id而不是对其进行计数(然后完全删除该组),并将其包装在很重要的一层中。 这样,如果您需要更新案例声明的位置,它将只放在一个地方。

select count(people_id) visits, state, program_name [etc.]
from (your existing query with no count, and no group by ) x
group by state, program_name, [etc.]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM