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 8189445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:16:50+00:00 2026-06-07T03:16:50+00:00

Possible Duplicate: SQL : Count the number of occurences occuring on output column and

  • 0

Possible Duplicate:
SQL : Count the number of occurences occuring on output column and calculate some percentage based on the occurences

Here is url for test data / table : http://sqlfiddle.com/#!2/56ffd4/1

My table generates o/p for following table :

(id,       resolution) 
('abc-123', 'fail'),
('abc-456', 'pass'),
('abc-789', 'pass'),
('abc-799', 'fail'),
('abc-800', 'pass'),
('abc-900', 'pass');

my script o/p is :

id          RESOLUTION  TS              @PREV   C   RES
abc-123     fail    July, 02 2012         1     1   -
abc-456     pass    July, 02 2012         2     0   50.00%
abc-789     pass    July, 02 2012         1     0   100.00%
abc-799     fail    July, 02 2012         1     1   -
abc-800     pass    July, 02 2012         2     0   50.00%
abc-900     pass    July, 02 2012         0     0   100.00%

here is o/p script:

SELECT st.*, 
       @prev:=@counter + 1,
       @counter:= CASE 
         WHEN st.resolution = 'pass'
         THEN 0
         ELSE @counter + 1
       END c,
       CASE WHEN @counter = 0 
            THEN CONCAT(FORMAT(100/@prev, 2), '%') 
            ELSE '-' 
       END res
  FROM so_test st, (SELECT @counter:=0) sc

I need to append two columns to above output table to count occurrences for passing and fail values as:

id          RESOLUTION  TS              @PREV   C            fail   pass
    abc-123     fail    July, 02 2012         1     1   -        1
    abc-456     pass    July, 02 2012         2     0   50.00%          1
    abc-789     pass    July, 02 2012         1     0   100.00%         1
    abc-799     fail    July, 02 2012         1     1   -        1
    abc-800     pass    July, 02 2012         2     0   50.00%          1
    abc-900     pass    July, 02 2012         0     0   100.00%         1
  • 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-06-07T03:16:52+00:00Added an answer on June 7, 2026 at 3:16 am

    Well if you just want the pass and fail columns at the end of the output table as you’ve specified then put a comma after

    END res 
    

    and add the following after it:

    CASE WHEN st.resolution = 'fail'
                THEN 1
                ELSE NULL
    END fail,
    CASE WHEN st.resolution = 'pass'
                THEN 1
                ELSE NULL
    END pass
    

    Not sure what the blank is in your columns but I’ve just set them as NULL.

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

Sidebar

Related Questions

Possible Duplicate: In SQL, what's the difference between count(column) and count(*)? As per subject,
Possible Duplicate: In SQL, what's the difference between count(column) and count( )? Count( )
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server Full text Index SQL Server stored procedure parameter output I
Possible Duplicate: SQL exclude a column using SELECT * [except columnA] FROM tableA? I
Possible Duplicate: sql to pick apart a string of a persons name and output
Possible Duplicate: SQL Count records within a month using a unix timestamp I have
Possible Duplicate: SQL exclude a column using SELECT * [except columnA] FROM tableA? I
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server how to drop identity from a column How I alter

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.