You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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: Using 100.0 instead of just 100 ensures 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 the ROUND function 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:22:39