Find the percentag in DB2.

RDBMS from IBM and IBM's Hierarchical DBMS and Transaction Manager (IMS DC).
Post Reply
Deepika
New Member
Posts: 4
Joined: Sat Aug 09, 2014 7:01 pm

Find the percentag in DB2.

Post by Deepika »

Hello All,

I need to find out the percentage of a group of accessories in a given table. For example, let's assume ACCER is the main table and has got 100000 records. In this 30000 are with ACCCODE = 'STAPLE', if I know the numbers I will find out the percentage (30000/100000) x 100. But I don’t know the total number of records for both i.e. how many records are in table and how many are with STAPLE…
How can I do it in DB2 itself?
User avatar
Anuj Dhawan
Founder
Posts: 2801
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Contact:
India

Re: Find the percentag in DB2.

Post by Anuj Dhawan »

Hi,

If I got it correctly and with the information in hand, try this:

Code: Select all

SELECT (SUM(CASE WHEN ACCCODE = 'STAPLE'
                THEN DECIMAL((1),6,2)  
                ELSE DECIMAL((0),6,2)  
            END) / COUNT(*)) * 100      
 FROM TABLE;
This will give you a precision to two digits after decimal.
Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM DB2 and IMS DB/DC”