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

  • SEARCH
  • Home
  • 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 6234863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:34:03+00:00 2026-05-24T10:34:03+00:00

I have the following SQL DECLARE @StatusOK int DECLARE @StatusFailed int SELECT @StatusOK =

  • 0

I have the following SQL

DECLARE @StatusOK int
DECLARE @StatusFailed int

SELECT @StatusOK = COUNT(Status) FROM tblAuditServiceLog WHERE Status = '1'
SELECT @StatusFailed = COUNT(Status) FROM tblAuditServiceLog WHERE Status = '2'

SELECT CompanyId, MethodCalled, tblSystemCompany.Description, @StatusOK as StatusOK, @StatusFailed as StatusFail
FROM tblAuditServiceLog 
INNER JOIN tblSystemCompany ON tblAuditServiceLog.CompanyId = tblSystemCompany.SystemCompanyId
GROUP BY MyId, MethodCalled, tblSystemCompany.Description

This works but the count isn’t based on the CompanyId (row), its returning the count across the table, so its a bit worthless.

From two tables like this

:: tblAuditServiceLog 
| CompanyId | MethodCalled | Status |
| 232       | GetProducts  |    1   |

:: tblSystemCompany
| SystemCompanyId | Description |
| 232             | MyCompany   |

Where Status = 1 is OK, 2 is Fail

Ideally I’d get an output like this…

| CompanyId | MethodCalled | Description | StatusOK | StatusFail |
| 232       | GetProducts  | MyCompany   |    8     |     2      |
| 236       | GetProducts  | MyCompanyB  |    8     |     2      |
| 256       | GetBrands    | MyCompanyC  |    8     |     2      |

I’m considering using cursors and just looping through and dumping into a temp table but I’m assuming there’s a more optimal way of doing this.

Any help from some SQL Server geniuses appreciated

The results above are (as per the two Select @ queries) the results of counting all the Status = 1 (status ok) and all the status = 2 (status fail) and not, as it should be, Status = AND CompanyId = 232

So assuming multiple company’s are logged doing various calls to the web-service, the count would be based on the total count of “OK Statuses” rather than the count for that company.

  • 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-24T10:34:04+00:00Added an answer on May 24, 2026 at 10:34 am

    You just need a conditional COUNT.

    SELECT
         MyId, MethodCalled, Description,
         COUNT(CASE Status WHEN 1 THEN tblAuditServiceLog.MyId END) AS StatusOK, 
         COUNT(CASE Status WHEN 2 THEN tblAuditServiceLog.MyId END) AS StatusFail 
    FROM 
        ...
    
    • There is an implied ELSE NULL in this CASE
    • COUNT ignores NULL
    • You may need DISTINCT in the COUNT so (COUNT(DISTINCT CASE...) but probably not

    Observations:

    • Your WHERE is not needed. The “table match condition” is already in the JOIN
    • Use aliases and qualify columns

    So (assumptions made about MethodCalled and Description columns)

    SELECT  ASL.CompanyId, 
            ASL.MethodCalled, 
            SI.Description, 
            COUNT(CASE ASL.Status WHEN 1 THEN ASL.CompanyId END) AS StatusOK, 
            COUNT(CASE ASL.Status WHEN 2 THEN ASL.CompanyId END) AS StatusFail
    
    FROM    dbo.tblAuditServiceLog AS ASL INNER JOIN
            dbo.tblSystemCompany AS 
            SI ON ASL.CompanyId = SI.SystemCompanyId
    
    GROUP BY ASL.CompanyId, ASL.MethodCalled, SI.Description
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following statement in a stored procedure: DECLARE @Count INT EXEC @Count
I have the following SQL script: DECLARE @temp table ( ID int IDENTITY(1, 1),
I have the following piece of SQL that does not work: declare @id INT;
I have the following SQL query: Declare @Total_SysDown as int, @Login_SysDown as int Set
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have the following SQL Statement. I need to select the latest record for
I have the following SQL statement: Select Choose(1,Orders.Employee, Orders.Customer) as Name1, Choose(2,Orders.Employee, Orders.Customer) as

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.