Jump to content

Problem with count() in query


hadoob024

Recommended Posts

I'm having problems writing this one particular query.  I have two tables, CARRIER and PATIENTS.  There's a one-to-many relationship between the two.  Basically, in one query, I want to pull the CARRIERNAME and CARRIERADDRESS of every CARRIER record in addition to a count of how many PATIENTS records have that particular CARRIERID.  Does that make sense?  So far I've tried:

 

SELECT ic.id,ic.name,ic.carrieraddress,ic.carriercity,ic.carrierstate,ipp.id
FROM carrier ic, patient_procedure ipp
WHERE ic.deleted=0 AND
ic.id = ipp.carrier_id
ORDER BY ic.name;

 

Which doesn't work, and:

 

SELECT ic.id,ic.name,ic.carrieraddress,ic.carriercity,ic.carrierstate,count(ipp.id)
FROM carrier ic, patient_procedure ipp
WHERE ic.deleted=0 AND
ic.id = ipp.carrier_id
ORDER BY ic.name;

 

 

Which also doesn't work.  Any thoughts on where I'm going wrong?  Thanks!

Link to comment
Share on other sites

Huh?  Sorry.  I don't get what you're asking.  Here's the table structure though for both tables:

 

CARRIER:

id

name

carrieraddress

carriercity

carrierstate

 

 

PATIENT_PROCEDURE:

id

name

carrier_id

 

 

So I need a list of every carrier along with a count of how many patient_procedure records have that particular carrier_id.

Link to comment
Share on other sites

Nope.  No error message.  It's just that the result set is not what I was looking for.  I want my result set to look like:

 

ID      NAME      CARRIERADDRESS      CARRIERSTATE      CARRIERCITY      count of patient_procedures

123    BCBS      999 main st.                TX                        Dallas                15

234    Cigna      123 main st.              AZ                        Phoenix              31

.

.

.

 

And so on.

Link to comment
Share on other sites

:shrug:

 

okey, and then?

 

post your php code. probably your fetch mode is not what you like...

 

and user 'AS' like that:

 

SELECT ic.id AS ID,ic.name AS NAME,ic.carrieraddress AS CARRIERADDRESS... and so on

Link to comment
Share on other sites

Here's the query:

SELECT ic.id,ic.name,ic.carrieraddress,ic.carriercity,ic.carrierstate,ipp.id
FROM carrier ic, patient_procedure ipp
WHERE ic.deleted=0 AND
ic.id = ipp.carrier_id
ORDER BY ic.name;

 

 

Spits out the result set:

 

ID      NAME      CARRIERADDRESS      CARRIERSTATE      CARRIERCITY      PATIENT_PROCEDURE ID

123    BCBS      999 main st.                TX                        Dallas                  123

123    BCBS      999 main st.                TX                        Dallas                  234

123    BCBS      999 main st.                TX                        Dallas                  345

123    BCBS      999 main st.                TX                        Dallas                  456

234    Cigna      123 main st.                AZ                        Phoenix              345

234    Cigna      123 main st.                AZ                        Phoenix              456

234    Cigna      123 main st.                AZ                        Phoenix              567

234    Cigna      123 main st.                AZ                        Phoenix              678

.

.

.

 

 

But I need a result set like the following:

 

ID      NAME      CARRIERADDRESS      CARRIERSTATE      CARRIERCITY      count of patient_procedures

123    BCBS      999 main st.                TX                        Dallas                5

234    Cigna      123 main st.                AZ                        Phoenix              4

.

.

.

 

Link to comment
Share on other sites

Perfect!!!  That worked.  My final query (which works) is:

 

SELECT ic.id,ic.name,ic.carrieraddress,ic.carriercity,ic.carrierstate,count(ipp.id)
FROM carrier ic, patient_procedure ipp
WHERE ic.deleted=0 AND
ic.id = ipp.carrier_id
GROUP BY ic.id
ORDER BY ic.name;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.