1

I have a query that counts records based on various criteria, but I would like to split the results into two grouped fields based on said criteria. Is there a way to do this inside the query - or is it necessary to have two queries?

select count(PT_TASK_ASSAY_DISP) as 'Count' from vbasedata v
where 
    v.PT_TASK_ASSAY_DISP like 'SS%' or
    (v.PT_TASK_ASSAY_DISP like 'IP%' and
    v.PT_TASK_ASSAY_DISP not like 'IP CUT' and
    v.PT_TASK_ASSAY_DISP not like 'IP NEG');

I'm trying to group by IP and SS to get a total count.

4

1 回答 1

2

我不确定你想要什么。但是,如果您希望每个“SS%”计数一个,“IP%”一个计数。然后可能是这样的:

select 
    SUM(CASE WHEN PT_TASK_ASSAY_DISP LIKE 'SS%' THEN 1 ELSE 0 END) as SSCount,
    SUM(CASE WHEN PT_TASK_ASSAY_DISP LIKE 'IP%' THEN 1 ELSE 0 END) as IPCount 
from vbasedata v
where 
    v.PT_TASK_ASSAY_DISP like 'SS%' or
    (v.PT_TASK_ASSAY_DISP like 'IP%' and
    v.PT_TASK_ASSAY_DISP not like 'IP CUT' and
    v.PT_TASK_ASSAY_DISP not like 'IP NEG');
于 2012-04-13T12:52:17.400 回答