将每一行的值与不同的值相乘。

huangapple go评论71阅读模式
英文:

MULTIPLY EACH ROW VALUES WITH DIFFERENT VALUES

问题

大家好,

在这里,我试图将每行乘以不同的值相乘。
例如..,
我有3列,

`

customer_id(int) , product_name(varchar) , price(int)`
          1                    LV                5
          2                    MK                15
          3                    YSL               16

我想要将Product_name = LV, MK的每个产品乘以10,Product_name = YSL的产品乘以20。
请问我如何实现这个目标..,

您的帮助将不胜感激

英文:

Hello everyone,

Here I am trying to do the multiplication of reach row multiplied by different values.
For example..,
I have 3 columns with

> `

customer_id(int) , product_name(varchar) , price(int)`
          1                    LV                5
          2                    MK                15
          3                    YSL               16

I would like to multiply each Product_name = LV, MK by 10 and Product_name = YSL by 20.
Could you please tell me how can I achieve this..,

Your help could be appreciated

答案1

得分: 2

你可以通过在SQL中直接使用CASE语句来完成。

SELECT
  customer_id,
  product_name,
  CASE
    WHEN product_name IN ('LV', 'MK') THEN price * 10
    WHEN product_name = 'YSL' THEN price * 20
    ELSE price
  END as adjusted_price
FROM TABLENAME;
英文:

You can get it done by incorporating a CASE statement directly in the SQL.

SELECT
  customer_id,
  product_name,
  CASE
    WHEN product_name IN ('LV', 'MK') THEN price * 10
    WHEN product_name = 'YSL' THEN price * 20
    ELSE price
  END as adjusted_price
FROM TABLENAME;

huangapple
  • 本文由 发表于 2023年2月16日 17:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定