在上下文中指定的非布尔类型的表达式,其中条件预期接近' type'

时间:2015-11-10 04:01:15

标签: sql-server

我是SSRS主题的新手,我在准备报告时遇到了这个错误,请帮助我解决这个问题。我一直在这里查询,请仔细阅读。

SELECT        T0.DocNum AS 'SO#', T6.DocNum as 'PO#', t1.LineTotal AS 'SO Amount', T6.DocEntry, T0.CardName, T6.U_enduserName, T11.QryGroup10 AS 'Major A/C Y/N', 
                         T6.DocEntry AS Expr1, T6.CardCode, T6.CardName AS Expr2, T6.NumAtCard, T6.DocDate, t5.ItemCode, t5.Dscription, T7.U_itmdes, t5.Quantity, t5.LineTotal, 
                         CASE WHEN T7.[U_itmdes] LIKE '%Desktop%%' THEN T5.[LineTotal] * 2.5 ELSE T5.[LineTotal] END AS 'LineTotal with DTS amt', T10.ItmsGrpNam, T8.Location, 
                         CASE WHEN T1.[Dscription] LIKE '%Renewal%%' THEN 'Renewal' ELSE 'Product' END AS 'Lic Type'

If((((T10.ItmsGrpNam="ADESK-AEC" and T10.ItmsGrpNam="ADESK-AEC-INFRA") and T1.Lic Type="Product" and T11.QryGroup10="No")),"AEC - ENI",If(((T10.ItmsGrpNam="ADESK-OPEN" and T10.ItmsGrpNam="ADESK-HORIZONTAL") and T1.Lic Type="Product" and T11.QryGroup10="No"),"PSEB",If(((T10.ItmsGrpNam="ADESK-M&E" or T10.ItmsGrpNam="ADESK-MFG") and T1.Lic Type="Product" and T11.QryGroup10="No"),T10.ItmsGrpNam,If(((T10.ItmsGrpNam="ADESK-AEC" and T10.ItmsGrpNam="ADESK-AEC-INFRA") and T1.Lic Type="Renewal" and T11.QryGroup10="No"),"AEC & ENI - Sub Renewal",If(((T10.ItmsGrpNam="ADESK-OPEN" and T10.ItmsGrpNam="ADESK-HORIZONTAL") and T1.Lic Type="Renewal" and T11.QryGroup10="No"),"PSEB - Sub Renewal",If(((T10.ItmsGrpNam="ADESK-M&E" or T10.ItmsGrpNam="ADESK-MFG") and T1.Lic Type="Renewal" and T11.QryGroup10="No"),"MFG - Sub Renewal","MFG - Sub Renewal"))))))



FROM            OCRD AS T11 INNER JOIN
                         ORDR AS T0 ON T11.CardCode = T0.CardCode INNER JOIN
                         RDR1 AS t1 ON T0.DocEntry = t1.DocEntry LEFT OUTER JOIN
                         POR1 AS t3 ON t3.BaseEntry = t1.DocEntry AND t3.BaseLine = t1.LineNum AND t3.ItemCode = t1.ItemCode LEFT OUTER JOIN
                         PDN1 AS t4 ON t4.BaseEntry = t3.DocEntry AND t4.BaseLine = t3.LineNum AND t4.ItemCode = t3.ItemCode INNER JOIN
                         PCH1 AS t5 ON t5.BaseEntry = t4.DocEntry AND t5.ItemCode = t4.ItemCode AND t5.BaseLine = t4.LineNum AND t5.TargetType <> 19 INNER JOIN
                         OPCH AS T6 ON t5.DocEntry = T6.DocEntry INNER JOIN
                         OITM AS T7 ON t5.ItemCode = T7.ItemCode INNER JOIN
                         OLCT AS T8 ON t5.LocCode = T8.Code CROSS JOIN
                         OITG AS T10
WHERE        (T10.ItmsTypCod = (CASE WHEN T7.[QryGroup1] = 'y' THEN 1 WHEN T7.[QryGroup2] = 'y' THEN 2 WHEN T7.[QryGroup3] = 'y' THEN 3 WHEN T7.[QryGroup4] = 'y' THEN
                          4 WHEN T7.[QryGroup5] = 'y' THEN 5 WHEN T7.[QryGroup6] = 'y' THEN 6 WHEN T7.[QryGroup7] = 'y' THEN 7 WHEN T7.[QryGroup8] = 'y' THEN 8 WHEN T7.[QryGroup9]
                          = 'y' THEN 9 WHEN T7.[QryGroup60] = 'y' THEN 60 ELSE 0 END)) AND (T6.CardCode <> 'VDD1-100233')
and T6.[DocDate] >=(@date1) and  T6.[DocDate] <=(@date2) 

2 个答案:

答案 0 :(得分:1)

您在选择部分的几个地方有以下内容:

T1.Lic Type="Product"

您必须分隔该列。改为:

T1.[Lic Type]="Product"

如此多的IIF运营商是一种痛苦。更改为CASE表达式。您将获得代码的可读性和标准化。同时将双引号更改为单引号。

case when T10.ItmsGrpNam='ADESK-AEC' and T10.ItmsGrpNam='ADESK-AEC-INFRA' and T1.[Lic Type]='Product' and T11.QryGroup10='No"' then 'AEC - ENI'
     when T10.ItmsGrpNam='ADESK-OPEN' and T10.ItmsGrpNam='ADESK-HORIZONTAL' and T1.[Lic Type]='Product' and T11.QryGroup10='No' then 'PSEB'
     when (T10.ItmsGrpNam='ADESK-M&E' or T10.ItmsGrpNam='ADESK-MFG') and T1.[Lic Type]='Product' and T11.QryGroup10='No' then T10.ItmsGrpNam
     when T10.ItmsGrpNam='ADESK-AEC' and T10.ItmsGrpNam='ADESK-AEC-INFRA' and T1.[Lic Type]='Renewal' and T11.QryGroup10='No' then'"AEC & ENI - Sub Renewal'
     when T10.ItmsGrpNam='ADESK-OPE' and T10.ItmsGrpNam='ADESK-HORIZONTAL' and T1.[Lic Type]='Renewal' and T11.QryGroup10='No' then 'PSEB - Sub Renewal'
     when (T10.ItmsGrpNam='ADESK-M&E' or T10.ItmsGrpNam='ADESK-MFG') and T1.[Lic Type]='Renewal' and T11.QryGroup10='No' then 'MFG - Sub Renewal'
     else 'MFG - Sub Renewal' end

编辑:

以下是完整的声明:

DECLARE @date1 DATETIME, @date2 DATETIME

SELECT  T0.DocNum AS 'SO#' ,
        T6.DocNum AS 'PO#' ,
        t1.LineTotal AS 'SO Amount' ,
        T6.DocEntry ,
        T0.CardName ,
        T6.U_enduserName ,
        T11.QryGroup10 AS 'Major A/C Y/N' ,
        T6.DocEntry AS Expr1 ,
        T6.CardCode ,
        T6.CardName AS Expr2 ,
        T6.NumAtCard ,
        T6.DocDate ,
        t5.ItemCode ,
        t5.Dscription ,
        T7.U_itmdes ,
        t5.Quantity ,
        t5.LineTotal ,
        CASE WHEN T7.[U_itmdes] LIKE '%Desktop%%' THEN T5.[LineTotal] * 2.5
             ELSE T5.[LineTotal]
        END AS 'LineTotal with DTS amt' ,
        T10.ItmsGrpNam ,
        T8.Location ,
        CASE WHEN T1.[Dscription] LIKE '%Renewal%%' THEN 'Renewal'
             ELSE 'Product'
        END AS 'Lic Type' ,
        CASE WHEN T10.ItmsGrpNam = 'ADESK-AEC'
                  AND T10.ItmsGrpNam = 'ADESK-AEC-INFRA'
                  AND T1.[Lic Type] = 'Product'
                  AND T11.QryGroup10 = 'No"' THEN 'AEC - ENI'
             WHEN T10.ItmsGrpNam = 'ADESK-OPEN'
                  AND T10.ItmsGrpNam = 'ADESK-HORIZONTAL'
                  AND T1.[Lic Type] = 'Product'
                  AND T11.QryGroup10 = 'No' THEN 'PSEB'
             WHEN ( T10.ItmsGrpNam = 'ADESK-M&E'
                    OR T10.ItmsGrpNam = 'ADESK-MFG'
                  )
                  AND T1.[Lic Type] = 'Product'
                  AND T11.QryGroup10 = 'No' THEN T10.ItmsGrpNam
             WHEN T10.ItmsGrpNam = 'ADESK-AEC'
                  AND T10.ItmsGrpNam = 'ADESK-AEC-INFRA'
                  AND T1.[Lic Type] = 'Renewal'
                  AND T11.QryGroup10 = 'No' THEN '"AEC & ENI - Sub Renewal'
             WHEN T10.ItmsGrpNam = 'ADESK-OPE'
                  AND T10.ItmsGrpNam = 'ADESK-HORIZONTAL'
                  AND T1.[Lic Type] = 'Renewal'
                  AND T11.QryGroup10 = 'No' THEN 'PSEB - Sub Renewal'
             WHEN ( T10.ItmsGrpNam = 'ADESK-M&E'
                    OR T10.ItmsGrpNam = 'ADESK-MFG'
                  )
                  AND T1.[Lic Type] = 'Renewal'
                  AND T11.QryGroup10 = 'No' THEN 'MFG - Sub Renewal'
             ELSE 'MFG - Sub Renewal'
        END
FROM    OCRD AS T11
        INNER JOIN ORDR AS T0 ON T11.CardCode = T0.CardCode
        INNER JOIN RDR1 AS t1 ON T0.DocEntry = t1.DocEntry
        LEFT OUTER JOIN POR1 AS t3 ON t3.BaseEntry = t1.DocEntry
                                      AND t3.BaseLine = t1.LineNum
                                      AND t3.ItemCode = t1.ItemCode
        LEFT OUTER JOIN PDN1 AS t4 ON t4.BaseEntry = t3.DocEntry
                                      AND t4.BaseLine = t3.LineNum
                                      AND t4.ItemCode = t3.ItemCode
        INNER JOIN PCH1 AS t5 ON t5.BaseEntry = t4.DocEntry
                                 AND t5.ItemCode = t4.ItemCode
                                 AND t5.BaseLine = t4.LineNum
                                 AND t5.TargetType <> 19
        INNER JOIN OPCH AS T6 ON t5.DocEntry = T6.DocEntry
        INNER JOIN OITM AS T7 ON t5.ItemCode = T7.ItemCode
        INNER JOIN OLCT AS T8 ON t5.LocCode = T8.Code
        CROSS JOIN OITG AS T10
WHERE   ( T10.ItmsTypCod = ( CASE WHEN T7.[QryGroup1] = 'y' THEN 1
                                  WHEN T7.[QryGroup2] = 'y' THEN 2
                                  WHEN T7.[QryGroup3] = 'y' THEN 3
                                  WHEN T7.[QryGroup4] = 'y' THEN 4
                                  WHEN T7.[QryGroup5] = 'y' THEN 5
                                  WHEN T7.[QryGroup6] = 'y' THEN 6
                                  WHEN T7.[QryGroup7] = 'y' THEN 7
                                  WHEN T7.[QryGroup8] = 'y' THEN 8
                                  WHEN T7.[QryGroup9] = 'y' THEN 9
                                  WHEN T7.[QryGroup60] = 'y' THEN 60
                                  ELSE 0
                             END ) )
        AND ( T6.CardCode <> 'VDD1-100233' )
        AND T6.[DocDate] >= ( @date1 )
        AND T6.[DocDate] <= ( @date2 ) 

答案 1 :(得分:1)

首先尝试这样做,

DECLARE @date1 AS DATETIME = '' --your date1
DECLARE @date2 AS DATETIME = '' --your date2
SELECT *
   FROM OCRD AS T11 

   INNER JOIN ORDR AS T0 ON T11.CardCode=T0.CardCode 
   INNER JOIN RDR1 AS t1 ON T0.DocEntry=t1.DocEntry 
   LEFT OUTER JOIN POR1 AS t3 ON t3.BaseEntry =t1.DocEntry AND t3.BaseLine=t1.LineNum
           AND t3.ItemCode=t1.ItemCode LEFT OUTER JOIN PDN1 AS t4 ON t4.BaseEntry=t3.DocEntry
           AND t4.BaseLine=t3.LineNum
           AND t4.ItemCode=t3.ItemCode INNER JOIN PCH1 AS t5 ON t5.BaseEntry=t4.DocEntry
           AND t5.ItemCode=t4.ItemCode
           AND t5.BaseLine=t4.LineNum
           AND t5.TargetType<>19 INNER JOIN OPCH AS T6 ON t5.DocEntry=T6.DocEntry INNER 
       JOIN OITM AS T7 ON t5.ItemCode=T7.ItemCode INNER JOIN OLCT AS T8 ON t5.LocCode 
      =T8.Code CROSS JOIN OITG AS T10 WHERE (
           T10.ItmsTypCod=(
               CASE 
                    WHEN T7.[QryGroup1]='y' THEN 1
                    WHEN T7.[QryGroup2]='y' THEN 2
                    WHEN T7.[QryGroup3]='y' THEN 3
                    WHEN T7.[QryGroup4]='y' THEN 4
                    WHEN T7.[QryGroup5]='y' THEN 5
                    WHEN T7.[QryGroup6]='y' THEN 6
                    WHEN T7.[QryGroup7]='y' THEN 7
                    WHEN T7.[QryGroup8]='y' THEN 8
                    WHEN T7.[QryGroup9]='y' THEN 9
                    WHEN T7.[QryGroup60]='y' THEN 60
                    ELSE 0
               END
           )
       )
   AND (T6.[CardCode] <> 'VDD1-100233')
   AND T6.[DocDate] >= (@date1)
   AND T6.[DocDate] <= (@date2)

如果它给你结果,那么尝试逐个添加下面的变量,

T0.DocNum       AS 'SO#'
      ,T6.DocNum       AS 'PO#'
      ,t1.LineTotal    AS 'SO Amount'
      ,T6.DocEntry
      ,T0.CardName
      ,T6.U_enduserName
      ,T11.QryGroup10  AS 'Major A/C Y/N'
      ,T6.DocEntry     AS Expr1
      ,T6.CardCode
      ,T6.CardName     AS Expr2
      ,T6.NumAtCard
      ,T6.DocDate
      ,t5.ItemCode
      ,t5.Dscription
      ,T7.U_itmdes
      ,t5.Quantity
      ,t5.LineTotal
      ,CASE 
            WHEN T7.[U_itmdes] LIKE '%Desktop%%' THEN T5.[LineTotal]*2.5
            ELSE T5.[LineTotal]
       END             AS 'LineTotal with DTS amt'
      ,T10.ItmsGrpNam
      ,T8.Location
      ,CASE 
            WHEN T1.[Dscription] LIKE '%Renewal%%' THEN 'Renewal'
            ELSE 'Product'
       END             AS 'Lic Type'

IF (
       (
           (
               (
                   T10.ItmsGrpNam="ADESK-AEC"
                   AND T10.ItmsGrpNam="ADESK-AEC-INFRA"
               )
               AND T1.[Lic TYPE]="Product"
               AND T11.QryGroup10="No"
           )
       )
      ,[AEC - ENI]
      ,IF(
           (
               (
                   T10.ItmsGrpNam="ADESK-OPEN"
                   AND T10.ItmsGrpNam="ADESK-HORIZONTAL"
               )
               AND T1.[Lic TYPE]="Product"
               AND T11.QryGroup10="No"
           )
          ,"PSEB"
          ,IF(
               (
                   (T10.ItmsGrpNam="ADESK-M&E" OR T10.ItmsGrpNam="ADESK-MFG")
                   AND T1.[Lic TYPE]="Product"
                   AND T11.QryGroup10="No"
               )
              ,T10.ItmsGrpNam
              ,IF(
                   (
                       (
                           T10.ItmsGrpNam="ADESK-AEC"
                           AND T10.ItmsGrpNam="ADESK-AEC-INFRA"
                       )
                       AND T1.[Lic TYPE]="Renewal"
                       AND T11.QryGroup10="No"
                   )
                  ,"AEC & ENI - Sub Renewal"
                  ,IF(
                       (
                           (
                               T10.ItmsGrpNam="ADESK-OPEN"
                               AND T10.ItmsGrpNam="ADESK-HORIZONTAL"
                           )
                           AND T1.[Lic TYPE]="Renewal"
                           AND T11.QryGroup10="No"
                       )
                      ,"PSEB - Sub Renewal"
                      ,IF(
                           (
                               (T10.ItmsGrpNam="ADESK-M&E" OR T10.ItmsGrpNam="ADESK-MFG")
                               AND T1.[Lic TYPE]="Renewal"
                               AND T11.QryGroup10="No"
                           )
                          ,"MFG - Sub Renewal"
                          ,"MFG - Sub Renewal"
                       )
                   )
               )
           )
       )
   )

注意:它会抛出错误:

  

&#39;,&#39;附近的语法不正确。

根据您的要求制作。如果是别名,那么确保它应该作为单个变量工作。

此外,我注意到您在 AS&#39; Lic Type&#39; 之后提取了不同的变量但未放置。检查一下。

使用CASE进行查询,

DECLARE @date1 AS DATETIME
DECLARE @date2 AS DATETIME
SELECT T0.DocNum       AS 'SO#'
      ,T6.DocNum       AS 'PO#'
      ,t1.LineTotal    AS 'SO Amount'
      ,T6.DocEntry
      ,T0.CardName
      ,T6.U_enduserName
      ,T11.QryGroup10  AS 'Major A/C Y/N'
      ,T6.DocEntry     AS Expr1
      ,T6.CardCode
      ,T6.CardName     AS Expr2
      ,T6.NumAtCard
      ,T6.DocDate
      ,t5.ItemCode
      ,t5.Dscription
      ,T7.U_itmdes
      ,t5.Quantity
      ,t5.LineTotal
      ,CASE 
            WHEN T7.[U_itmdes] LIKE '%Desktop%%' THEN T5.[LineTotal]*2.5
            ELSE T5.[LineTotal]
       END             AS 'LineTotal with DTS amt'
      ,T10.ItmsGrpNam
      ,T8.Location
      ,CASE 
            WHEN T1.[Dscription] LIKE '%Renewal%%' THEN 'Renewal'
            ELSE 'Product'
       END             AS 'Lic Type'
,
CASE 
     WHEN T10.ItmsGrpNam='ADESK-AEC' AND T10.ItmsGrpNam='ADESK-AEC-INFRA' AND T1.[Lic TYPE]='Product' AND T11.QryGroup10='No"' THEN 'AEC - ENI'
     WHEN T10.ItmsGrpNam='ADESK-OPEN' AND T10.ItmsGrpNam='ADESK-HORIZONTAL' AND 
          T1.[Lic TYPE]='Product' AND T11.QryGroup10='No' THEN 'PSEB'
     WHEN (T10.ItmsGrpNam='ADESK-M&E' OR T10.ItmsGrpNam='ADESK-MFG') AND T1.[Lic 
          TYPE]='Product' AND T11.QryGroup10='No' THEN T10.ItmsGrpNam
     WHEN T10.ItmsGrpNam='ADESK-AEC' AND T10.ItmsGrpNam='ADESK-AEC-INFRA' AND T1.[Lic 
          TYPE]='Renewal' AND T11.QryGroup10='No' THEN '"AEC & ENI - Sub Renewal'
     WHEN T10.ItmsGrpNam='ADESK-OPE' AND T10.ItmsGrpNam='ADESK-HORIZONTAL' AND 
          T1.[Lic TYPE]='Renewal' AND T11.QryGroup10='No' THEN 
          'PSEB - Sub Renewal'
     WHEN (T10.ItmsGrpNam='ADESK-M&E' OR T10.ItmsGrpNam='ADESK-MFG') AND [T1.Lic 
          TYPE]='Renewal' AND T11.QryGroup10='No' THEN 'MFG - Sub Renewal'
     ELSE 'MFG - Sub Renewal'
END
   FROM OCRD AS T11 

   INNER JOIN ORDR AS T0 ON T11.CardCode=T0.CardCode 
   INNER JOIN RDR1 AS t1 ON T0.DocEntry=t1.DocEntry 
   LEFT OUTER JOIN POR1 AS t3 ON t3.BaseEntry =t1.DocEntry AND t3.BaseLine=t1.LineNum
           AND t3.ItemCode=t1.ItemCode LEFT OUTER JOIN PDN1 AS t4 ON t4.BaseEntry=t3.DocEntry
           AND t4.BaseLine=t3.LineNum
           AND t4.ItemCode=t3.ItemCode INNER JOIN PCH1 AS t5 ON t5.BaseEntry=t4.DocEntry
           AND t5.ItemCode=t4.ItemCode
           AND t5.BaseLine=t4.LineNum
           AND t5.TargetType<>19 INNER JOIN OPCH AS T6 ON t5.DocEntry=T6.DocEntry INNER 
       JOIN OITM AS T7 ON t5.ItemCode=T7.ItemCode INNER JOIN OLCT AS T8 ON t5.LocCode 
      =T8.Code CROSS JOIN OITG AS T10 WHERE (
           T10.ItmsTypCod=(
               CASE 
                    WHEN T7.[QryGroup1]='y' THEN 1
                    WHEN T7.[QryGroup2]='y' THEN 2
                    WHEN T7.[QryGroup3]='y' THEN 3
                    WHEN T7.[QryGroup4]='y' THEN 4
                    WHEN T7.[QryGroup5]='y' THEN 5
                    WHEN T7.[QryGroup6]='y' THEN 6
                    WHEN T7.[QryGroup7]='y' THEN 7
                    WHEN T7.[QryGroup8]='y' THEN 8
                    WHEN T7.[QryGroup9]='y' THEN 9
                    WHEN T7.[QryGroup60]='y' THEN 60
                    ELSE 0
               END
           )
       )
   AND (T6.[CardCode] <> 'VDD1-100233')
   AND T6.[DocDate] >= (@date1)
   AND T6.[DocDate] <= (@date2)