简体   繁体   中英

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. 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. 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. 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.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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