dprfe04886 2013-04-30 19:10
浏览 1068
已采纳

MySQL条件选择2个字段中的1个,然后按2个字段的组合进行分组

I'm a bit new to conditional statements in MySQL queries, I'm wondering if someone can help me out with the following?

I have the following table:

Company | Billing_State | Delivery_State | Revenue

How would I conditionally select the billing state if the company name equals 'XYZ' but otherwise select delivery state if not -- and then group by states, regardless of whether it's a billing state or delivery state? Let's say for the purpose of aggregating sales revenue reports by US states.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douwen5681 2013-04-30 19:13
    关注

    You are looking for something like:

    select if(company = 'XYZ',billing_state,delivery_state), sum(revenue)
    from companies
    group by if(company = 'XYZ',billing_state,delivery_state)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样