SUM和IF混合用法

格式:IF(Condition,A,B)

意义:当Condition为TRUE时,返回A;当Condition为FALSE时,返回B。

作用:作为条件语句使用。

例子

SELECT

d.item_variation_sku AS SKU,

SUM(

IF(

d.add_time BETWEEN '2017-03-01' AND '2017-03-04',

d.item_amount,

0

)

) AS sku_count,

SUM(

IF(

d.add_time BETWEEN '2017-02-27' AND '2017-03-01',

d.item_amount,

0

)

) AS compare_sku_count,

ROUND(

SUM(

IF(

d.add_time BETWEEN '2017-03-01' AND '2017-03-04',

d.item_amount * d.item_price_after_discount / d.local_rite,

0

)

),

2

) AS Money,

ROUND(

SUM(

IF(

d.add_time BETWEEN '2017-02-27' AND '2017-03-01',

d.item_amount * d.item_price_after_discount / d.local_rite,

0

)

),

2

) AS Compare_Money,

SUM(

IF(

d.add_time BETWEEN '2017-03-01' AND '2017-03-04',

d.item_amount,

0

)

) - SUM(

IF(

d.add_time BETWEEN '2017-02-27' AND '2017-03-01',

d.item_amount,

0

)

) AS diff_sku_count,

ROUND(

SUM(

IF(

d.add_time BETWEEN '2017-03-01' AND '2017-03-04',

d.item_amount * d.item_price_after_discount / d.local_rite,

0

)

),

2

) - ROUND(

SUM(

IF(

d.add_time BETWEEN '2017-02-27' AND '2017-03-01',

d.item_amount * d.item_price_after_discount / d.local_rite,

0

)

),

2

) AS diff_money

FROM

(

SELECT

id,

order_table_id,

`item_variation_sku`,

item_amount,

item_price_after_discount,

`local_rite`,

`add_time`

FROM

orders_detail1702

UNION ALL

SELECT

id,

order_table_id,

`item_variation_sku`,

item_amount,

item_price_after_discount,

`local_rite`,

`add_time`

FROM

orders_detail1703

) AS d,

(

SELECT

id,

account_id

FROM

orders_table1702

UNION ALL

SELECT

id,

account_id

FROM

orders_table1703

) AS t,

shopee_admin.accounts AS a

WHERE

d.order_table_id = t.id AND t.account_id = a.id AND d.add_time BETWEEN '2017-02-27' AND '2017-03-04'

GROUP BY

d.item_variation_sku

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 时间穿过我们的身体 一面稀释年轻 一面对耳朵吹气 细胞分裂、死亡、新生 像是一场巨大的迁徙 只有我留在原地 ——《...
    段童阅读 2,286评论 0 0
  • 文/洛夕璇 最近得了一种病,叫文字抓狂病。不知道有没有朋友和我一样,就是你进入了自己很喜欢的圈子,拿着自己自认...
    洛夕璇阅读 3,610评论 13 7
  • 其实,你不能否认人都是贱的,上了高中莫名怀念初中生活,上了大学便觉得高中才是最不可替代的,步入社会又开始恨...
    白芸茹阅读 2,839评论 0 3

友情链接更多精彩内容