简体   繁体   English

SQL计数或聚合错误

[英]SQL Count or Aggregation Error

New to the forum here. 这里是论坛的新手。 My question is how can I get a count of a column and have it presented based on a case statement? 我的问题是如何获得一列的数量,并根据案例陈述进行展示? I have searched the forum and have found answers related to obtaining counts , sums and aggregations but have not found any questions that relate to an added condition of case statement. 我在论坛上进行了搜索,找到了与获取计数,总计和总计有关的答案,但是没有找到与案例陈述的附加条件有关的任何问题。 Below is my current code. 下面是我当前的代码。 The code works but does not bring the information back as desired. 该代码可以正常工作,但不能将信息带回所需的位置。

In example lets say there were 10 visits at 5 different sites in a specific program. 例如,假设某个特定程序在5个不同的站点进行了10次访问。 How can I get the output to appear as such: 我如何使输出显示为:

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

And the query is: 查询是:

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

Thanks for your assistance 谢谢你的协助

UPDATED SYNTAX 更新语法

I have updated the syntax with recommendations of using subquery and adding the case statement to the group by section but am still not getting results. 我已经更新了语法,并建议使用子查询并将case语句添加到group by节中,但仍然没有得到结果。 The code is resulting in errors like incorrect syntax near group and etc. 该代码导致错误,例如组附近语法错误等。

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

i would recommend you to add more info with sample records in temporary table. 我建议您使用临时表中的示例记录添加更多信息。 however this code can help you to build your idea more clear for other to help you or you can deduce it for your use. 但是,此代码可以帮助您更清晰地提出自己的想法,以供其他人使用,或者可以推断出要使用的想法。 -- assumption: head office want to see that person visited different states which office region (west coast or east coast) related to him -假设:总部希望看到此人访问了与他相关的办公区域(西海岸或东海岸)的不同州

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

hope it helps 希望能帮助到你

It sounds like all you need to do is change what is in your group by. 听起来您需要做的就是更改分组依据。 You need to group by exactly the groups you want to see in your results, not by all the fields you used to create them. 您需要完全按照要在结果中看到的组进行分组,而不是按照用于创建它们的所有字段进行分组。 So, you will either have to copy your case statements into the group by, exactly as they appear in the select (without aliases), or you can use what you have, selecting the people_id instead of counting it (and removing the group by entirely), and wrapping it in a layer that counts. 因此,您要么必须将case语句完全按照select中显示的方式(不使用别名)复制到case语句中,要么可以使用已有的内容,选择people_id而不是对其进行计数(然后完全删除该组),并将其包装在很重要的一层中。 That way if you need to update your case statement for location, it will just be in one place. 这样,如果您需要更新案例声明的位置,它将只放在一个地方。

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