0

I have a query that I am using for a report. Each line is a row of data containing a medical history, the client_id is repeated for each medical history.

I'm grouping by client_id and summing their conditions but I want to use a sub-query to find any conditions that are above a preset score. My current query is giving me the total for the whole table, not just the specific client_id.

Can someone help me out? Thanks!

Here my query:

select 
    DATEADD(month, DATEDIFF(month, 0, taken_on), 0), 
    client_id, 
    count(hscore), 
    sum(hscore),
    (select count(hscore) 
            from amexmedscreen 
            where hscore >= '3.5') 
    from amexmedscreen 
    group by taken_on, client_id`
1
  • So you want count(hscore) for each client_id and taken_on, but in that last value you only want to count hscores >= 3.5? Commented Mar 9, 2011 at 16:23

3 Answers 3

6

It should be sufficient to use CASE to get 1 for relevant rows, and sum those:

select 
    ...
    sum(hscore),
    sum(CASE WHEN hscore >= 3.5 THEN 1 ELSE 0 END)
group by taken_on, client_id`
Sign up to request clarification or add additional context in comments.

4 Comments

I think he wants the sum to be relative to the client aggregation only, not client and taken_on
@WinstonSmith: You might be right, but it's difficult to tell. The OP only mentions grouping by clientid even though the query groups by client_id and taken_on. In the other case i think I'd first do that select, only grouping by client_id, and nest it in another query grouping by taken_on and client_id.
END is knocking on the door and yelling 'Let me in!', but no one seems to hear...
@AndriyM: Thanks for noticing that poor thing, fixed ;)
1

Your subquery is a separate query. It's not constrained by what's going on in the main query. You need to tell it to do the count only for the client_id of the current record in the outer query. You'll be referring to the same table twice in two different queries, so you'll have to use a different alias for each one.

Something like this should work:

select DATEADD(month, DATEDIFF(month, 0, taken_on), 0), 
     client_id, 
     count(hscore), 
     sum(hscore),
     (select count(hscore) 
      from amexmedscreen subq 
      where 
          hscore >= '3.5' 
          and subq.client_id = outerq.client_id )
from amexmedscreen outerq
group by taken_on, client_id

Comments

0
select DATEADD(month, DATEDIFF(month, 0, taken_on), 0), 
client_id, 
count(hscore), 
sum(hscore),
tb1.COUNT_HSCORE
from amexmedscreen 
INNER JOIN 
(select client_id, count(hscore) as COUNT_HSCORE
 from amexmedscreen 
 where hscore >= '3.5' 
 group by client_ID) as tb1
ON tb1.client_id = client id
group by taken_on, client_id,tb1.COUNT_HSCORE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.