Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3456080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:41:33+00:00 2026-05-18T09:41:33+00:00

I have a LIST (query output)that has the distinct ID’s from a table Account.

  • 0

I have a LIST (query output)that has the distinct ID’s from a table “Account”.
Using that LIST I am LOOPING over a SELECT query and basically extracting some COUNT .
I am having trouble getting the COUNT value on an individual basis.

The table “Account” has the fields
1) contract_ID ,
2)vAccountID(primary key) and
3) Status_id (values=’’,V,C).

I am doing the following query

    <cfquery name="qryGetid" datasource="#datasource#">
 SELECT DISTINCT(contract_ID )  
 FROM Account
    ORDER BY contract_ID DESC
</cfquery>


<!---   account details for each --->
<cfset  Z =#ValueList(qryGetid.ID)# >
<cfloop list="#Z#" index="Y"   >  
<cfquery name="qryGetNiceAccounts"  datasource="#dataSource#">
 SELECT 
 DISTINCT(a.contract_ID )
 ,(SELECT count(vAccountID) FROM Account
        WHERE c _ID IN (<cfqueryparam value="#x#" list="yes" cfsqltype="cf_sql_integer" separator=",">)
        AND Status_id = 'V' )   AS Valid_AcntV
 ,(SELECT count(vAccountID) FROM Account
        WHERE c _ID IN (<cfqueryparam value="#x#" list="yes" cfsqltype="cf_sql_integer" separator=",">)
        AND Status_id = 'C' )   AS Valid_AcntC 

  FROM  Account a
  WHERE   
 a.contract_ID  IN (<cfqueryparam value="#x#" list="yes" cfsqltype="cf_sql_integer" separator="," >)
 ORDER BY   contract_ID   DESC

</cfquery>

The query =”qryGetNiceAccounts” is returning only one value for “Valid_AcntCount” even for different “c_ID” in the list .

Example if the “Account” table has the values

contract_ID      count(vID)/ v_Accoun t=’v’     count(vID)/ v_Accoun t=’c’

    123           10                                                  220
    124           05                                                  110
    123           01                                                     0

 contract_ID   count(vID)/ v_Accoun t=’v’     count(vID)/ v_Accoun t=’c’
    123           10                                                  220
    124           10                                                  220
    123           10                                                  220

Basically I am having trouble getting COUNTS for individual IDs.

Side note:-When I do a dump the Input “contract_ID ” is showing as 123, 123 123 rather than 123,124,125

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:41:34+00:00Added an answer on May 18, 2026 at 9:41 am

    […] Using that LIST I am LOOPING over a SELECT query and basically extracting some COUNT. […]

    This is generally a bad idea. Especially “getting some counts” is one of the easiest things to do directly in SQL and more often than not there is no reason to execute a SELECT query in a loop. Avoid that whenever you can for performance reasons.

    And in your case it’s perfectly avoidable (it even makes your code a lot simpler), just change your SQL:

    <cfquery name="qryGetNiceAccounts"  datasource="#dataSource#">
      SELECT
        contract_ID,
        (SELECT COUNT(vAccountID) FROM v_Account 
          WHERE c_ID = a.contract_ID AND Status_id = 'V'
        ) AS Valid_AcntV,
        (SELECT COUNT(vAccountID) FROM v_Account 
          WHERE c_ID = a.contract_ID AND Status_id = 'C'
        ) AS Valid_AcntC
      FROM
        (SELECT contract_ID FROM Account GROUP BY contract_ID) AS a
      ORDER BY
        contract_ID DESC
    </cfquery>
    

    You don’t need the other query at all, neither do you need the loop.

    An alternative way to express the same would be this:

    SELECT
      a.contract_ID,
      SUM(CASE Status_id WHEN 'V' THEN 1 ELSE 0 END) AS Valid_AcntV,
      SUM(CASE Status_id WHEN 'C' THEN 1 ELSE 0 END) AS Valid_AcntC
    FROM
      Account AS a
      INNER JOIN v_Account AS c ON c.c_id = a.contract_ID
    GROUP BY 
      a.contract_ID
    ORDER BY
      a.contract_ID DESC
    

    This would hit the v_Account view only once. You must determine for yourself what is the most efficient query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following query to list the employees of two table. i need
So I have this Query: var list = (from x in xList join a
I have a select query which returns a list of data and I need
I have a query that looks like this: it takes a list of IDs
I have a linq query that returns a list of employees and a job
I have two tables, one table has a list of videos, one has a
Problem I have a Fetch XML query that has an aggregate count on a
I have a table that has several columns: HarvestID, HarvestDate, UserID to mention the
I have some IDs inserted into a temp table #A as follows: SELECT DISTINCT
I am using Dotnet Highchart that is based on C# code that has output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.