【问题标题】:How to select based on a certain condition, but I don't want that condition to list in the result如何根据特定条件进行选择,但我不希望该条件列在结果中
【发布时间】:2014-08-01 01:50:03
【问题描述】:

我有产品编号、仓库、现有数量。 我们有 3 个仓库 A、B、C。 如何仅通过一条语句进行选择,而不必做临时表来仅从仓库 A 和 B 中提取现有数量? 如果我将过滤条件设置为 A,B,那么这些仓库将列在结果中,这是我不想要的。对所有仓库的手头总和很容易,但说到这个,我有点困惑。

【问题讨论】:

    标签: sql group-by


    【解决方案1】:

    您只需要在没有group by 的聚合查询中使用where 子句:

    select sum(onhandqty)
    from tablet
    where warehouse in ('A', 'B');
    

    您不提供示例数据或表格布局,但您应该能够根据您的结构进行调整。

    编辑:

    如果字段在数据中,您可以按其他字段汇总,即使是您在问题中未提及的字段。

    select product, sum(onhandqty)
    from tablet
    where warehouse in ('A', 'B')
    group by product;
    

    【讨论】:

    • 谢谢 Gordon,问题是我还想列出 Product-sum on hand qty,这就是为什么我认为我必须在其中包括“仓库”。
    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 2012-10-03
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多