使用SQL Developer计算table1表中头等邮费商品占比的技术求助
Hey there! Let's work through how to calculate that percentage for you. Here's a simple, effective SQL query you can run directly in SQL Developer:
SELECT ROUND( (SUM(CASE WHEN postage_class = 1 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2 ) AS first_class_percentage FROM table1;
Breakdown of what this does:
SUM(CASE WHEN postage_class = 1 THEN 1 ELSE 0 END): Counts all items where the postage class is 头等邮费 (postage_class=1). Every matching item gets a 1, non-matching items get 0, and summing these values gives the total number of first-class items.COUNT(*): Gets the total number of items in the table, no exceptions.* 100.0: Using100.0instead of just100ensures we perform floating-point division, so you won't run into integer rounding errors (like getting 0 instead of 33.33 if exactly one-third of items are first-class).ROUND(..., 2): Rounds the final percentage to 2 decimal places for readability—feel free to adjust the number if you need more or less precision (e.g.,ROUND(..., 1)for one decimal place, or remove theROUNDfunction entirely if you want the full raw value).
Alternatively, you can use COUNT instead of SUM for the first-class count, since COUNT ignores NULL values:
SELECT ROUND( (COUNT(CASE WHEN postage_class = 1 THEN 1 END) * 100.0) / COUNT(*), 2 ) AS first_class_percentage FROM table1;
Either query will give you the exact percentage you're looking for. Just paste it into SQL Developer, run it, and you'll get your result!
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

