在where子句中使用case时出错

时间:2015-05-22 06:04:55

标签: sql

我使用了以下代码..但它有一些错误...请帮助我

    SELECT VoucherTypes.types, 
           VoucherHead.VoucherNo, 
           VoucherHead.VoucherDate, 
           SUM(VoucherDetail.Debit) AS debit, 
           SUM(VoucherDetail.Credit) AS credit,
           VoucherHead.VoucherStatus
    FROM  VoucherHead 
          INNER JOIN VoucherTypes 
          ON VoucherHead.VoucherType = VoucherTypes.vtypeid 
          INNER JOIN VoucherDetail 
          ON VoucherHead.VoucherID = VoucherDetail.VoucherID
    WHERE(VoucherHead.VoucherDate >= @fromdate) 
    AND (VoucherHead.VoucherDate <= @todate) 
    AND (VoucherTypes.types = @vtype) 
    AND (VoucherHead.Branchno = @branchid)
    and case(VoucherHead.VoucherStatus)
         when @val=1 then 
              VoucherHead.VoucherStatus='true' 
         when @val=2 then 
              VoucherHead.VoucherStatus=false 
         when @val=0 then 
              VoucherHead.VoucherStatus=true or 
              VoucherHead.VoucherStatus=false or 
              VoucherHead.VoucherStatus is null
 GROUP BY VoucherTypes.types, 
          VoucherHead.VoucherNo, 
          VoucherHead.VoucherDate, 
          VoucherHead.VoucherStatus

3 个答案:

答案 0 :(得分:2)

试试这个,可能适合你。通过你的问题很难理解你的要求。

   SELECT VoucherTypes.types, 
           VoucherHead.VoucherNo, 
           VoucherHead.VoucherDate, 
           SUM(VoucherDetail.Debit) AS debit, 
           SUM(VoucherDetail.Credit) AS credit,
           VoucherHead.VoucherStatus    
    FROM  VoucherHead 
    INNER JOIN VoucherTypes ON VoucherHead.VoucherType = VoucherTypes.vtypeid 
    INNER JOIN VoucherDetail ON VoucherHead.VoucherID = VoucherDetail.VoucherID
    WHERE (VoucherHead.VoucherDate >= @fromdate) 
      AND (VoucherHead.VoucherDate <= @todate) 
      AND (VoucherTypes.types = @vtype) 
      AND (VoucherHead.Branchno = @branchid)
      AND (@val=0 OR VH.VoucherStatus = case when @val=1 then 'true' 
          when @val=2 then 'false' END)
    GROUP BY VoucherTypes.types, VoucherHead.VoucherNo, VoucherHead.VoucherDate, VoucherHead.VoucherStatus

答案 1 :(得分:1)

常规<item name="android:actionModeCloseDrawable">@drawable/up_button</item> 语法(稍微简化):

CASE

case when x = y then value-expression1 when z = q then value-expression2 etc... [ else value-expressionx ] end - 子句是可选的。所有返回值表达式必须具有兼容的数据类型。

答案 2 :(得分:1)

您应该将查询编写为:

SELECT VT.[types], 
       VH.VoucherNo, 
       VH.VoucherDate,
       SUM(VD.Debit) AS debit,
       SUM(VD.Credit) AS credit,
       VH.VoucherStatus
    FROM  VoucherHead VH 
    INNER JOIN VoucherTypes VT ON VH.VoucherType = VT.vtypeid
    INNER JOIN VoucherDetail VD ON VH.VoucherID = VD.VoucherID
    WHERE (VH.VoucherDate >= @fromdate) 
    AND (VH.VoucherDate <= @todate) 
    AND (VT.[types] = @vtype) 
    AND (VH.Branchno = @branchid)
    and 1 = ( CASE 
              when @val=1 AND  VH.VoucherStatus='true' THEN 1
              when @val=2 AND  VH.VoucherStatus='false' THEN 1
              --when @val= 0 AND  (VH.VoucherStatus='true' or
              --VH.VoucherStatus='false' or VH.VoucherStatus is NULL)
              --THEN 1
              when @val=3 THEN 1  ELSE 0 END )
    GROUP BY VT.[types], VH.VoucherNo, VH.VoucherDate, VH.VoucherStatus