尝试找出导致我的SQL Server查询无法返回结果的问题。我有一个查询,它会调出单个采购订单上大小级别有多个唯一“请求交货日期”的地方。我是使用COUNT
和DISTINCT
执行此操作的。它是完美的,直到我添加字段'PO_ITEM_NUMBER'和'REQ_DELIV_DATE' - 这是业务部门要求的。我不确定为什么会导致问题 - 作为参考,我们的PO表层是标题,项目,大小 - 大小是最精细的,它是基于SAP的。
查询:
SELECT E.TEAM_MEMBER_NAME [EMPLOYEE],
H.PO_TYPE,
H.PO_ISSUE_DATE,
S.PO_NUMBER,
S.MATERIAL,
M.DESCRIPTION,
H.PO_ORDERED_QUANTITY [PO_QUANTITY], -- if you sue SUM(S.PO_ORDERED_QUANTITY - you get more results but wrong totals
K.BUSINESS_SEGMENT_DESC,
S.PO_REQ_DELIV_DATE,
S.PO_ITEM_NUMBER
FROM PDX_SAP_USER..VW_PO_SIZE S --- you can use .. insetead of .dbo.
JOIN ADI_USER_MAINTAINED..SCM_PO_Employee_Name E --- join the po to employee assigment table
ON S.PO_NUMBER = E.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_HEADER H
ON E.PO_NUMBER = H.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_ITEM I
ON E.PO_NUMBER = I.PO_NUMBER
JOIN PDX_SAP_USER..VW_MM_MATERIAL M
ON E.MATERIAL = M.MATERIAL
JOIN PDX_SAP_USER..vw_kd_BUSINESS_SEGMENT K
ON M.BUSINESS_SEGMENT_CODE = K.BUSINESS_SEGMENT_CODE
WHERE I.PO_BALANCE_QUANTITY > 0 ---exclude any fully received PO's
AND NOT EXISTS (SELECT * FROM VW_PO_ITEM I1 WHERE DEL_INDICATOR = 'L' AND I.PO_NUMBER = I1.PO_NUMBER)
GROUP BY S.PO_NUMBER,
E.TEAM_MEMBER_NAME,
H.PO_TYPE,
H.PO_ISSUE_DATE,
S.MATERIAL,
M.DESCRIPTION,
K.BUSINESS_SEGMENT_DESC,
H.PO_ORDERED_QUANTITY,
S.PO_REQ_DELIV_DATE,
S.PO_ITEM_NUMBER
HAVING COUNT(DISTINCT S.PO_REQ_DELIV_DATE) > 1
ORDER BY S.PO_NUMBER
添加与结果一起使用的查询:
SELECT E.TEAM_MEMBER_NAME [EMPLOYEE],
H.PO_TYPE,
CONVERT(VARCHAR(12),H.PO_ISSUE_DATE,101) [PO_ISSUE_DATE],
S.PO_NUMBER,
S.MATERIAL,
M.DESCRIPTION,
H.PO_ORDERED_QUANTITY [PO_QUANTITY], --- if you use SUM(S.PO_ORDERED_QUANTITY) - you get more results but wrong totals
K.BUSINESS_SEGMENT_DESC
FROM PDX_SAP_USER..VW_PO_SIZE S --- you can use .. insetead of .dbo.
JOIN ADI_USER_MAINTAINED..SCM_PO_Employee_Name E --- join the po to employee assigment table
ON S.PO_NUMBER = E.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_HEADER H
ON E.PO_NUMBER = H.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_ITEM I
ON E.PO_NUMBER = I.PO_NUMBER
JOIN PDX_SAP_USER..VW_MM_MATERIAL M
ON E.MATERIAL = M.MATERIAL
JOIN PDX_SAP_USER..vw_kd_BUSINESS_SEGMENT K
ON M.BUSINESS_SEGMENT_CODE = K.BUSINESS_SEGMENT_CODE
WHERE I.PO_BALANCE_QUANTITY > 0 ---exclude any fully received PO's
AND NOT EXISTS (SELECT * FROM VW_PO_ITEM I1 WHERE DEL_INDICATOR = 'L' AND I.PO_NUMBER = I1.PO_NUMBER)
GROUP BY S.PO_NUMBER,
E.TEAM_MEMBER_NAME,
H.PO_TYPE,
H.PO_ISSUE_DATE,
S.MATERIAL,
M.DESCRIPTION,
K.BUSINESS_SEGMENT_DESC,
H.PO_ORDERED_QUANTITY
HAVING COUNT(DISTINCT S.PO_REQ_DELIV_DATE) > 1
ORDER BY S.PO_NUMBER
结果:
EMPLOYEE PO_TYPE PO_ISSUE_DATE PO_NUMBER MATERIAL DESCRIPTION PO_QUANTITY BUSINESS_SEGMENT_DESC
------------------------------ ------- ------------- ---------- ------------------ ---------------------------------------- --------------------------------------- ----------------------------------------------------------------------------------------------------
Christopher Olson NB 01/19/2017 0282238419 CD7078 ESS 3S PANT WVN 2054 CORE APP MEN SPORT ADIDAS
Juan Gomez NB 02/23/2017 0282524995 S98775 ESS LIN P/O FT 103 CORE APP MEN SPORT ADIDAS
Christopher Olson NB 03/09/2017 0282598957 BK7410 ESS LGO T P SJ 619 ATHLETICS APP MEN ADIDAS
Juan Gomez NB 03/28/2017 0282706115 S97155 ESS LIN TIGHT 961 CORE APP WOMEN SPORT ADIDAS
Juan Gomez NB 09/21/2017 0283752965 CF8152 BOS LABEL 7900 ATHLETICS APP MEN ADIDAS
Julie Lange-May 12 10/02/2017 0283796594 DQ1421 WOVEN JACKET W 1020 ATHLETICS APP WOMEN ADIDAS
Kekai Ariola NB 10/10/2017 0283837426 AC7366 PW HU HOLI Tennis Hu MC 5655 STATEMENT FTW ADIDAS
Cody Lofquist NB 11/10/2017 0283944933 DB2061 PREDATOR TANGO 18.1 TR 1756 FOOTBALL FTW ADIDAS
Andrew Zapata 05 11/13/2017 0283961402 CG6440 NEMEZIZ 18.1 FG W 543 FOOTBALL FTW ADIDAS
Christopher Olson NB 11/20/2017 0283981666 CV7748 ASSITA 17 GK Y 1648 FOOTBALL APP GENERIC ADIDAS
Cody Lofquist NB 11/21/2017 0283984539 DB2165 COPA 18.1 FG 501 FOOTBALL FTW ADIDAS
Julie Lange-May NB 11/26/2017 0284043157 CE4368 I GRPHC STSET 1333 ORIGINALS APP KIDS ADIDAS
Trey Pflug NB 11/27/2017 0284048754 CQ3168 SOLAR BOOST M 3500 RUNNING FTW MEN ADIDAS
Dave Laws NB 11/28/2017 0284059045 DB2966 YEEZY 500 15334 YEEZY FTW ADIDAS
Dave Laws NB 11/28/2017 0284059047 DB2966 YEEZY 500 12584 YEEZY FTW ADIDAS
Christopher Olson NB 12/06/2017 0284094060 BJ9165 TASTIGO17 SHO W 7522 FOOTBALL APP GENERIC ADIDAS
Christopher Olson NB 12/06/2017 0284094212 BK0350 TIRO17 TRG PNTW 7091 FOOTBALL APP GENERIC ADIDAS
Cody Lofquist NB 12/08/2017 0284107301 DB2062 PREDATOR TANGO 18.1 TR 2110 FOOTBALL FTW ADIDAS
Trey Pflug NB 12/11/2017 0284115640 BC0674 SOLAR BOOST W 1752 RUNNING FTW WOMEN ADIDAS
Kim Moreland NB 12/12/2017 0284137355 DJ3033 D2M K SHT 1730 CORE APP WOMEN SPORT ADIDAS
Cody Lofquist NB 12/12/2017 0284141196 DB2126 PREDATOR TANGO 18.3 IN 1988 FOOTBALL FTW ADIDAS
Cody Lofquist NB 12/12/2017 0284141253 AQ0612 NEMEZIZ MESSI TANGO 18.3 TF 526 FOOTBALL FTW ADIDAS
Dave Laws NB 12/15/2017 0284170426 DB2966 YEEZY 500 2918 YEEZY FTW ADIDAS
Cody Lofquist NB 12/16/2017 0284174671 DB2248 X 18.1 FG 668 FOOTBALL FTW ADIDAS
Cody Lofquist NB 12/16/2017 0284174673 DB2039 PREDATOR 18.1 FG 489 FOOTBALL FTW ADIDAS
Christopher Olson ER 12/20/2017 0284207872 BS4250 TASTIGO17 SHO 404 FOOTBALL APP GENERIC ADIDAS
Ben Paul NB 12/19/2017 0284208137 CG0584 REAL A JSY 811 FOOTBALL APP LICENSED ADIDAS
Julie Lange-May NB 01/07/2018 0284316616 DN4273 UAS BEANIE 120 ORIGINALS APP MEN ADIDAS
Cody Lofquist NB 01/08/2018 0284319552 DB2063 PREDATOR TANGO 18.1 TR 2001 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464341 DB2214 X 18+ FG 582 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464343 DB2013 PREDATOR 18+ FG 2201 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464344 DB2072 NEMEZIZ 18+ FG 1467 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464346 DB2251 X 18.1 FG 620 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464348 DB2167 COPA 18.1 FG 1714 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464349 DB2089 NEMEZIZ MESSI 18.1 FG 988 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464350 DB2040 PREDATOR 18.1 FG 2061 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284465944 DB2001 PREDATOR 18.3 FG 7008 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/23/2018 0284489924 772109 SAMBA CLASSIC 419 FOOTBALL FTW ADIDAS
Andrew Zapata 05 02/02/2018 0284539184 DH3869 CLIMA 3.0 TEE 1853 ACTION SPORTS APP ADIDAS
Cody Lofquist NB 02/06/2018 0284550445 BB0571 Goletto VI FG J 5562 FOOTBALL FTW ADIDAS
Cody Lofquist NB 02/24/2018 0284666220 DM2092 MLS ASG OMB 424 FOOTBALL ACC HW ADIDAS
Christopher Olson NB 01/19/2018 0284666914 BP9111 D2M 3S SHORT 11811 CORE APP MEN SPORT ADIDAS
Cody Lofquist NB 02/27/2018 0284684097 019228 MUNDIAL TEAM 657 FOOTBALL FTW ADIDAS
Cody Lofquist NB 03/06/2018 0284704098 CD4683 GENERICWCBOX 80000 FOOTBALL ACC HW ADIDAS
Kekai Ariola NB 03/08/2018 0284728508 BB7619 Sobakov 1775 ORIGINALS FTW MEN ADIDAS
Kim Moreland NB 03/08/2018 0284730274 BP9733 ULT SS T 2557 TRAINING APP MEN ADIDAS
Kekai Ariola 05 03/27/2018 0284865999 B37532 EQT SUPPORT SK PK W 347 ORIGINALS FTW WOMEN ADIDAS
Kekai Ariola 05 03/27/2018 0284866000 B37545 EQT SUPPORT SK PK W 357 ORIGINALS FTW WOMEN ADIDAS
Kim Moreland NB 04/06/2018 0284914322 DH3591 Tech Tee 10042 TRAINING APP WOMEN ADIDAS
Cody Lofquist NB 04/10/2018 0284930265 CW5627 Pred FS JR MN 1651 FOOTBALL ACC HW ADIDAS
Kekai Ariola NB 04/10/2018 0284930449 B41794 PW TENNIS HU 315 ORIGINALS FTW MEN ADIDAS
(51 row(s) affected)
答案 0 :(得分:0)
If you added HAVING clause recently then comment and check.
If not then change the INNER JOIN TO LEFT join from bottom to top one at a time and execute the query and check.
This way you can troubleshoot the issue easily.
Unless we have the sample data for all the tables you are used the query it will be difficult to answer.
答案 1 :(得分:0)
WITH POsToReturn AS (
SELECT S.PO_NUMBER
FROM PDX_SAP_USER..VW_PO_SIZE S
JOIN PDX_SAP_USER..VW_PO_ITEM I
ON I.PO_NUMBER = S.PO_NUMBER
WHERE I.PO_BALANCE_QUANTITY > 0
GROUP BY S.PO_NUMBER
HAVING COUNT(DISTINCT S.PO_REQ_DELIV_DATE) > 1
AND COUNT(CASE WHEN I.DEL_INDICATOR = 'L' THEN 1 END) = 0
)
SELECT <your columns>
FROM POsToReturn P
JOIN PDX_SAP_USER..VW_PO_SIZE S
ON S.PO_NUMBER = P.PO_NUMBER
... <join the rest of the tables for the detail columns>
-- leave out the entire group by!
或者你可以采用这样的方法:
with data as (
select
count(distinct S.PO_REQ_DELIV_DATE)
over (partition by S.PO_NUMBER) as rdd_count
<insert rest of main query>
)
select ... from data
where rdd_count > 1
order by S.PO_NUMBER;
至于理解原因,我认为您已经看到其他人在group by
子句中添加列的查询,因此他们可以绕过关于非聚合的神秘错误消息。如果不是那样,那么您已经看到那些容易混淆地允许非标准行为的系统,这些行为可以产生没有错误的结果。
通常人们想要完成的事情是这样的:我已经对客户ID进行了分组,但我也希望在结果中使用客户名称。因此,他们通过&#39;将额外的列添加到&#39;组。列表,一切正常。但是如果你考虑一下,原因是新专栏根本没有改变组,因为每个客户ID总是拥有相同的客户名称,它最终只是一个简单的方法来摆脱错误。在您的查询中,这不是正确的:您在所有所关注的群组中确实有多个日期。
在我看来,使用像min(Customer Name) as CustomerName
这样的虚拟聚合更好。请记住,聚合函数的目的是将多个值折叠为单个值。必要时,某些系统会随机选择一个值,而不会警告您这样做。许多MySQL和Sybase开发人员在依赖这种古怪的行为和/或从未真正了解它应该如何工作时就被此焚烧了。
还要记住,通常一个组是一组折叠成一行的多行。要使count(distinct)
工作,需要在单个组中查看的多个日期。但这与您需要将输出保持为单独的行相冲突。
基本上,该行本身并不能为您提供足够的信息来决定是否保留它。我通过使用第二个查询来确定满足条件的PO_Number
列表然后将其用作过滤器(通过内部联接)来解决这个问题。
在第二个例子中,我使用了一个窗口函数,它允许你在每行之外查看group by
的行折叠行为。这两个基本上都让你做having
想要完成的事情。