我有这个查询。
SELECT DISTINCT
'CONTRACT',
CO.OBJID,
CO.ORDERNUMBER
(SELECT SUM(CASE WHEN CT.SHORTNAME = 'BGL'
THEN TG.QUANTITY
ELSE 0 END) AS Cartons,
SUM(CASE WHEN CT.SHORTNAME = 'CT'
THEN TG.QUANTITY
ELSE 0 END) AS Boxes,
SUM(CASE WHEN CT.SHORTNAME = 'PL'
THEN TG.QUANTITY
ELSE 0 END) AS Trailer
FROM GOODS TG,
XTYPE CT
WHERE TG.ORDERID = CO.OBJID
AND TG.CONTOBJECTID = CT.OBJID
AND CT.SHORTNAME IN ('BGL', 'CT', 'PL')
)
FROM XCUSTORDER CO,
XEDIPARTNER EP
WHERE CIP.CUSTOID = CO.OBJECTID
AND CO.EDIPARTNER_OBJECTID = EP.OBJECTID
AND EP.ILNNUMBER = 'NASA'
查询不起作用我怎样才能获得我需要的结果。我不想为每个纸箱,盒子和预告片运行不同的选择
答案 0 :(得分:1)
尝试:如果您想在单个查询中执行所有操作。您必须单独查询每列,如下所示。我不确定它会起作用,因为它没有经过测试,但我相信它会给你一个好主意。您正在加入CIP.CUSTOID
,但在查询中的任何位置都没有提到包含CIP
别名的表格,因此我在第二个查询中执行了EP
。
SELECT DISTINCT
'CONTRACT',
CO.OBJID,
CO.ORDERNUMBER,
(SELECT SUM(TG.QUANTITY)
FROM GOODS TG,
XTYPE CT
WHERE TG.ORDERID = CO.OBJID
AND TG.CONTOBJECTID = CT.OBJID
AND CT.SHORTNAME IN ('BGL')) AS Cartons,
(SELECT SUM(TG.QUANTITY)
FROM GOODS TG,
XTYPE CT
WHERE TG.ORDERID = CO.OBJID
AND TG.CONTOBJECTID = CT.OBJID
AND CT.SHORTNAME IN ('CT')) AS Boxes,
(SELECT SUM(TG.QUANTITY)
FROM GOODS TG,
XTYPE CT
WHERE TG.ORDERID = CO.OBJID
AND TG.CONTOBJECTID = CT.OBJID
AND CT.SHORTNAME IN ('PL')) AS Trailer
FROM XCUSTORDER CO,
XEDIPARTNER EP
WHERE CIP.CUSTOID = CO.OBJECTID
AND CO.EDIPARTNER_OBJECTID = EP.OBJECTID
AND EP.ILNNUMBER = 'NASA'
您可以通过以下方式更改查询:
SELECT
'CONTRACT',
CO.OBJID,
CO.ORDERNUMBER,
SUM(CASE WHEN CT.SHORTNAME = 'BGL'
THEN TG.QUANTITY
ELSE 0 END) AS Cartons,
SUM(CASE WHEN CT.SHORTNAME = 'CT'
THEN TG.QUANTITY
ELSE 0 END) AS Boxes,
SUM(CASE WHEN CT.SHORTNAME = 'PL'
THEN TG.QUANTITY
ELSE 0 END) AS Trailer
FROM XCUSTORDER CO
INNER JOIN XEDIPARTNER EP ON EP.CUSTOID = CO.OBJECTID
AND CO.EDIPARTNER_OBJECTID = EP.OBJECTID
AND EP.ILNNUMBER = 'NASA'
LEFT JOIN GOODS TG ON TG.ORDERID = CO.OBJID
LEFT JOIN XTYPE CT ON TG.CONTOBJECTID = CT.OBJID
AND CT.SHORTNAME IN ('BGL', 'CT', 'PL')
GROUP BY CO.OBJID,
CO.ORDERNUMBER