用于计算数量的案例

时间:2016-12-06 09:36:53

标签: sql

大家好,

我有一个要求,我需要为两种情况计算Quantity

  • 案例1:

    if (vouchernumber starts with letter `'A%' or 'B%'` ) then 
         Quantity must be Quantity*-1
    
  • 案例2:

    if (vouchernumber starts with letter `'C%'` ) then 
         Quantity is zero
    

如何在SQL中执行此操作?

2 个答案:

答案 0 :(得分:3)

只需使用包含您描述的逻辑的{ "_id" : ObjectId("58345e0e996d340bd8126149"), "genericname" : "Paracetamol Tab 500mg", "type" : "X", "isbrand" : false } 表达式:

        {
                "_id" : ObjectId("582d43d18ec3f432f3260682"),
                "patientid" : ObjectId("582aacff3894c3afd7ad4677"),
                "doctorid" : ObjectId("582a80c93894c3afd7ad4675"),
                "medicalcondition" : "high fever, cough, runny nose.",
                "diagnosis" : "Viral Flu",
                "addmissiondate" : "2016-01-12",
                "dischargedate" : "2016-01-16",
                "bhtno" : "125",
                "prescription" : [ 
                    {
                        "drug" : 
                        {
                          "_id" : ObjectId("58345e0e996d340bd8126149"),
                          "genericname" : "Paracetamol Tab 500mg",
                          "type" : "X",
                          "isbrand" : false
                        },
                        "instructions" : "Take 2 daily, after meals."
                    },
                    ...
                ]
            }

答案 1 :(得分:0)

谢谢蒂姆的工作。 语法

COALESCE((SELECT vouchernumber,
   CASE WHEN vouchernumber LIKE 'A%' OR vouchernumber LIKE 'B%' THEN -1*Quantity
        WHEN vouchernumber LIKE 'C%' THEN 0 END),0)

FROM TEMP