INNER JOIN在select子句'中导致&non-group-by表达式

时间:2018-03-27 14:01:18

标签: sql reporting-services

来自noob SQL用户的问题,使用MS SQL Server Report Builder 2016。

我试图在三个表上构建查询: PV_Job(作业的标题), PV_JobLine(附加到作业标题的作业行)和 PV_SOrderLine(销售订单行)。

我特别尝试的是,对于每个PV_Job,找到附加的PV_JobLines,然后返回PV_JobLine的OrderedQty字段的SUM。 (还有其他字段和左连接,我从下面的查询中省略了,因为我只想尽可能地隔离错误。)

我是通过从PV_Job左边连接到PV_JobLine来做到这一点的。

PV_Job还需要内部联接到PV_SOrderLine,因为每个作业行都需要存在销售订单行。

我尝试运行查询时遇到错误:

Non-group-by expression in select clause

这是我试图在下面运行的查询:

SELECT 
    "PV_Job1"."JobCode", 
    "PV_Job1"."JobName", 
    "PV_Job1"."CompNum", 
    "PV_Job1"."CompletedDate", 
    "PV_Job1"."JobCreateDate", 
    "PV_Customer1"."CustName", 
    "PV_Job1"."ActTotalCost",
    "JobLine"."SumOrdQty" AS "OrdQty"

FROM  "VISION"."PUB"."PV_Job" "PV_Job1"

        LEFT JOIN "VISION"."PUB"."PV_Customer" "PV_Customer1" 
             WITH (INDEX(CustCode))
               ON ("PV_Job1"."CompNum"="PV_Customer1"."CompNum")
              AND ("PV_Job1"."CustCode"="PV_Customer1"."CustCode")

        LEFT JOIN (SELECT 
                       "PV_JobLine1"."CompNum",
                       "PV_JobLine1"."JobCode",
                       "PV_JobLine1"."PlantCode",
                       "PV_JobLine1"."JobLineNum",
                       SUM("PV_JobLine1"."OrderedQty") AS "SumOrdQty"

                   FROM "VISION"."PUB"."PV_JobLine" "PV_JobLine1"

                   INNER JOIN "VISION"."PUB"."PV_SOrderLine" "PV_SOrderLine1" WITH (INDEX(OrderLine))
                           ON ("PV_JobLine1"."CompNum"="PV_SOrderLine1"."CompNum")
                          AND ("PV_JobLine1"."PlantCode"="PV_SOrderLine1"."PlantCode")
                          AND ("PV_JobLine1"."SOrderNum"="PV_SOrderLine1"."SOrderNum")
                          AND ("PV_JobLine1"."SOrderLineNum"="PV_SOrderLine1"."SOrderLineNum")

                    WITH (INDEX(JobLineIdx))

                    GROUP BY "PV_JobLine1"."CompNum",
                             "PV_JobLine1"."JobCode",
                             "PV_JobLine1"."PlantCode",
                             "PV_JobLine1"."JobLineNum"

        ) "JobLine"
         ON ("PV_Job1"."CompNum"="JobLine"."CompNum")
        AND ("PV_Job1"."JobCode"="JobLine"."JobCode")
        AND ("PV_Job1"."PlantCode"="JobLine"."PlantCode")
        AND ("JobLine"."JobLineNum" IN (1,2,3,4,5))



 WHERE  ("PV_Job1"."CompNum"=7 
    AND "PV_Job1"."CompletedDate" IS  NULL  
     OR "PV_Job1"."CompletedDate">{d '2018-02-21'} 
    AND "PV_Job1"."JobCreateDate"<={d '2018-02-21'} 
    AND "PV_Job1"."ActTotalCost"<>0)

 GROUP BY
    "PV_Job1"."JobCode", 
    "PV_Job1"."JobName", 
    "PV_Job1"."CompNum", 
    "PV_Job1"."CompletedDate", 
    "PV_Job1"."JobCreateDate", 
    "PV_Customer1"."CustName", 
    "PV_Job1"."ActTotalCost",
    "OrdQty"

如果我删除整个INNER JOIN块,查询将会运行;添加它后,它失败并出现上述错误。我无法完全理解我需要做些什么来进一步纠正它,或者是否可能有更好的方法。

(我在PV_JobLine区域中执行SUM,因为此查询最初对其他字段有更多LEFT JOINS,并且导致重复问题,因此尝试使用此处的答案进行修复:Prevent duplicate values in LEFT JOIN

道歉,如果有什么内容含糊不清或似乎被误解了......请告诉我是否可以进一步澄清任何内容!

0 个答案:

没有答案