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?
Find the percentag in DB2.
- Anuj Dhawan
- Founder
- Posts: 2824
- Joined: Sun Apr 21, 2013 7:40 pm
- Location: Mumbai, India
- Contact:
Re: Find the percentag in DB2.
Hi,
If I got it correctly and with the information in hand, try this:
This will give you a precision to two digits after decimal.
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;
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.
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.
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