If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > PostgreSQL and percentages?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-05-12, 07:02
vvalter vvalter is offline
Registered User
 
Join Date: Dec 2011
Posts: 8
PostgreSQL and percentages?

Hello

I have a table looking like:

ID | Sum | Tax
----------------
0 | 100 | 20
1 | 1000 | 200
2 | 100 | 45
3 | 1000 | 0

AND i need a query, which selects ALL rows WHERE tax IS NOT 20% of SUM.
So the result should look like this:

ID | Sum | Tax
----------------
2 | 100 | 45
3 | 1000 | 0

But i don't know, how to calculate percentages in postgresql Can anyone help?
Reply With Quote
  #2 (permalink)  
Old 01-05-12, 07:23
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by vvalter View Post
But i don't know, how to calculate percentages in postgresql
No different than with other systems by using a division: (tax / sum) * 100

Assuming sum and tax are decimal columns:

Code:
SELECT *
FROM the_table_with_no_name
WHERE (tax / sum) * 100 <> 20
If tax and sum are integer values, then you need to make sure the division is using the correct data type:
Code:
SELECT *
FROM the_table_with_no_name
WHERE (tax::decimal / sum:decimal) * 100 <> 20
Reply With Quote
  #3 (permalink)  
Old 01-05-12, 08:35
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,540
i'm not a postgresql user, but would the following not avoid the integer arithmetic issue...

WHERE 100.0 * tax / sum <> 20
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 01-05-12, 08:52
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by r937 View Post
i'm not a postgresql user, but would the following not avoid the integer arithmetic issue...

WHERE 100.0 * tax / sum <> 20
Good catch! Yes it would avoid it.
Personally I prefer explicit casts simply to document the fact that one needs to pay attention
Reply With Quote
  #5 (permalink)  
Old 01-05-12, 09:02
vvalter vvalter is offline
Registered User
 
Join Date: Dec 2011
Posts: 8
thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On