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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:33:38+00:00 2026-06-10T22:33:38+00:00

I m facing a problem to write a SQL query to get result in

  • 0

I m facing a problem to write a SQL query to get result in % mode, I am familer with SUM() and COUNT() functions of SQL Server but facing problem to implement logic inside query I want result in below form:-

UserName---  % of AccepectResult----   % of RejectResult

My table structure is like this with two columns Name (UserName) and Result :

NAME     Result
---------------
USer1       A
USer1       A
USer1       A
USer1       R
USer1       R
USer1       A
USer2       A
USer2       A
USer2       A
USer2       A
USer2       R

A - Accepted Result
R - Rejected Result

I’m trying to write this query like this..

select * into #t1  from 
(
    select UserName , count(Result) as Acc
    from Test where result = 'A'
    group by UserName 
) as tab1

select * into #t2 from 
(
    select UserName , count(Result) as Rej
    from Test where result = 'R'
    group by UserName 
) as tab2

select #t1.UserName , 
      #t1.Acc , 
      #t2.Rej , 
     (#t1.Acc)*100/(#t1.Acc + #t2.Rej)  as AccPercentage,
     (#t2.Rej)*100/(#t1.Acc + #t2.Rej)  as RejPercentage

 from #t1
 inner join #t2 on #t1.UserName = #t2.UserName


drop table #t1

drop table #t2

Is there any other way to write this query and any built-in function for calculating percentage in SQL Server?

  • 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-10T22:33:40+00:00Added an answer on June 10, 2026 at 10:33 pm

    You do not require to join table. Instead you can use SUM or COUNT function like this:

    Using SUM Function:

    SELECT Name, 100 * 
    SUM(CASE WHEN Result = 'A' THEN 1 ELSE 0 END)/COUNT(result)
    AS Accept_percent
    ,100 * 
    SUM(CASE WHEN Result = 'R' THEN 1 ELSE 0 END)/COUNT(result)
    AS Reject_percent
    FROM t
    Group by Name;
    

    Or using COUNT Function:

    SELECT Name, 100 * 
    COUNT(CASE WHEN Result = 'A' THEN 1 ELSE NULL END)/COUNT(result)
    AS Accept_percent
    ,100 * 
    COUNT(CASE WHEN Result = 'R' THEN 1 ELSE NULL END)/COUNT(result)
    AS Reject_percent
    FROM t
    Group by Name;
    

    Or using SubQuery:

    SELECT Name, 100 * 
    (SELECT COUNT(result) FROM t WHERE result='A' And Name = main.Name)/COUNT(result)
    AS Accept_percent
    , 100 * 
    (SELECT COUNT(result) FROM t WHERE result='R' And Name = main.Name)/COUNT(result)
    AS Reject_percent 
    FROM t main
    Group by Name;
    

    See this SQLFiddle

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

Sidebar

Related Questions

I am facing problem to get variable from query string. I have used htaccess
I am facing a problem displaying the result of my select * query on
I am facing a problem for writing an sql query which should be easy
I am trying to write a session helper and facing to problem to test
I am migrating to Postgresql, but I am facing a problem. A trick I
Hi i'm facing the following problem, i have to re-write an existant code to
I write the bitstream for a JPEG Encoder, I'm facing this problem: I built
Im trying to write very simple HTML parser with ANTLR and Im facing problem,
I'm facing a weird problem. I looked around all over stackoverflow.com and elsewhere, but
Hi i have done with status update in tumbler but i am facing problem

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.