选择列表错误中的子查询列“XX”在选择列表中无效

时间:2018-04-05 11:42:42

标签: sql sql-server sql-server-2014

我在选择列表中有一个子查询,但即使我使用group by子句,它也会返回错误

  

“列'OINV.DocEntry'在选择列表中无效,因为它是   不包含在聚合函数或GROUP BY子句“

需要一些指导来解决这个问题。

   SELECT  T0.isIns
         , T0.DocNum
         , T0.CardCode
         , T0.CardName
         , T0.DocDate                                     AS [Posting Date]
         , SUM(T1.GTotal)                                 AS [Total Gross Price]
         , SUM(T1.LineVat)                                AS [Total Tax Amount]
         , ISNULL(( SELECT     SUM(ISNULL(INV1.LineTotal, 0))
                      FROM       OINV
                      INNER JOIN INV1
                              ON OINV.DocEntry = INV1.DocEntry
                      WHERE      ( OINV.isIns = 'Y' )
                             AND INV1.ItemCode = 'SRRE00000002'
                             AND T0.DocEntry   = OINV.DocEntry
                             AND T1.DocEntry   = INV1.DocEntry
                      GROUP      BY OINV.DocEntry
                                    , INV1.DocEntry ), 0) [MarketReturn]
FROM       OINV AS T0
INNER JOIN INV1 AS T1
        ON T0.DocEntry = T1.DocEntry
WHERE      ( T0.isIns = 'Y' )
GROUP      BY T0.isIns
              , T0.DocNum
              , T0.CardCode
              , T0.CardName
              , T0.DocDate 

1 个答案:

答案 0 :(得分:0)

@JatinPatel提到的最终答案

SELECT  T0.isIns
         , T0.DocNum
         , T0.CardCode
         , T0.CardName
         , T0.DocDate                                     AS [Posting Date]
         , SUM(T1.GTotal)                                 AS [Total Gross Price]
         , SUM(T1.LineVat)                                AS [Total Tax Amount]
         , ISNULL(( SELECT     SUM(ISNULL(INV1.LineTotal, 0))
                      FROM       OINV
                      INNER JOIN INV1
                              ON OINV.DocEntry = INV1.DocEntry
                      WHERE      ( OINV.isIns = 'Y' )
                             AND INV1.ItemCode = 'SRRE00000002'
                             AND T0.DocEntry   = OINV.DocEntry
                             AND T1.DocEntry   = INV1.DocEntry
                      GROUP      BY OINV.DocEntry
                                    , INV1.DocEntry ), 0) [MarketReturn]
FROM       OINV AS T0
INNER JOIN INV1 AS T1
        ON T0.DocEntry = T1.DocEntry
WHERE      ( T0.isIns = 'Y' )
GROUP      BY T0.isIns
              , T0.DocNum
              , T0.CardCode
              , T0.CardName
              , T0.DocDate 
          ,T0.DocEntry
          ,T1.DocEntry