SQL计数或聚合错误

SQL计数或聚合错误,sql,count,group-by,aggregate-functions,Sql,Count,Group By,Aggregate Functions,这里是论坛的新成员。我的问题是,如何获得一列的计数,并根据案例陈述进行呈现?我搜索了论坛,找到了与获取计数、总数和聚合相关的答案,但没有找到任何与案例陈述附加条件相关的问题。下面是我当前的代码。代码可以工作,但不能按需要返回信息 在这个例子中,我们假设在一个特定的项目中有5个不同站点的10次访问。如何使输出显示为: visits e4pv.site_providing_service_name, State location program_name 2 a b c

这里是论坛的新成员。我的问题是,如何获得一列的计数,并根据案例陈述进行呈现?我搜索了论坛,找到了与获取计数、总数和聚合相关的答案,但没有找到任何与案例陈述附加条件相关的问题。下面是我当前的代码。代码可以工作,但不能按需要返回信息

在这个例子中,我们假设在一个特定的项目中有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语句添加到GROUPBY section中,但仍然没有得到结果。代码会导致错误,如组附近的语法错误等

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

希望它能有所帮助。听起来你所需要做的就是改变你小组中的内容。您需要准确地按照您希望在结果中看到的组进行分组,而不是按照您用来创建这些组的所有字段进行分组。因此,您必须将案例语句复制到group by中,就像它们在“无别名选择”中显示的那样,或者您可以使用现有的内容,选择人员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.]

您需要逐字地将select语句中的所有非聚合表达式重复到组中。有些RBBMS允许使用别名,如Clause在您的案例中的位置。我不知道其他人,但如果在这个问题中有一些CREATE TABLE语句和一些INSERT语句示例,这将有助于我回答这个问题。然后我可以把它放在本地安装或小提琴上,并尝试一下我的一些查询想法。例如,我不知道rpt_critical_info_脚本的模式是什么样子。我可以尝试创建所有这些表,但我可能会犯错误和不正确的假设,以使所有的工作。在我看来,这要求回答者付出很多。如果你能简化示例模式就更好了。有没有可能用sql fiddle构建你的模式?@cha谢谢,我明天早上会试试@JeremyPridemore由于这是我的第一篇文章,我将确保下次提供样本数据。然而,就这个问题而言,表格本身并不重要。为了简化,我想计算一下办公室的总访问量。每次就诊都以行的形式记录在就诊表上。这些行没有office statecase语句DateOfVisite。我怎样才能获得按办公室和州显示的总访问次数?谢谢@FaheemAhmad,我回到办公室后会尝试这样做