使用join

时间:2017-10-22 23:36:52

标签: sql oracle

这不是我第一次编写SQL语句,对我来说真的很奇怪为什么我会一直收到这个错误。

   SELECT
   CASE WHEN  VENDORDELIVERY IS NOT NULL THEN VENDORDELIVERY   ELSE 'No Delivery' END as " Vendor Delivery No.",
        DATE_TIME as "Delivery Date/Time",
        'z0' AS "Icon1",
        'z0' AS "Icon2",
        'z0' AS "Icon3",
        'z0' AS "Icon4",
            NVL(completion,0) AS "Progress",
        'd0' AS "Active",
        MATERIALCODE as "Material Code",
        MATERIAL as "Material",
        DELIVERY_QTY as "Delivery Qty",
        ASN_UNLOADINGSTATION AS "Unload. Manf",
        ASN_DESTINATIONTANK AS "DEST. Tank",
        STATUS AS "Status" , 'NO_DATA' AS "NoData1"
  FROM  XVW_ASN_OVERVIEW z
        RIGHT OUTER JOIN ( SELECT 'NO_DATA' AS NoData2 FROM DUAL ) b  
  ON z.NoData1 = b.NoData2;

我得到的错误是ORA-00904:“Z”。“NODATA1”:标识符无效 00904. 00000 - “%s:无效标识符” *原因:
*行动: 行错误:23列:10

XVW_ASN_OVERVIEW是一个视图,我甚至尝试在此视图中创建一个列。

两列都有效,当我独立运行SQL语句时,不会发生错误。这么奇怪。我错过了什么?也许我需要另外一双眼睛指出我的错误。

感谢您的建议。

3 个答案:

答案 0 :(得分:1)

对于NoData1,您在select中使用带引号的标识符,在join语句中使用非带引号的标识符。所以尝试改变

`<div class="left">
    <p>left div</p>
</div>`

`<div class="right">
    <p>right div</p>
</div>`

`<footer>Footer</footer>`

`.left {
  position: absolute;
  left: 10%;
  background-color: red;
}`

`.right {
  position: absolute;
  left: 80%;
  background-color: blue;
}`

`footer {
  width: 100%;
  background-color: green;
  text-align: center;
}`

      STATUS AS "Status" , 'NO_DATA' AS "NoData1"

答案 1 :(得分:0)

这并不奇怪。您无法在同一查询的其他位置使用SELECT中定义的别名。

相反,这是一个解决方案:

SELECT (CASE WHEN  VENDORDELIVERY IS NOT NULL THEN VENDORDELIVERY ELSE 'No Delivery' END) as " Vendor Delivery No.",
        DATE_TIME as "Delivery Date/Time",
       'z0' AS "Icon1",
       'z0' AS "Icon2",
       'z0' AS "Icon3",
       'z0' AS "Icon4",
        COALESCE(completion, 0) AS "Progress",
        'd0' AS "Active",
        MATERIALCODE as "Material Code",
        MATERIAL as "Material",
        DELIVERY_QTY as "Delivery Qty",
        ASN_UNLOADINGSTATION AS "Unload. Manf",
        ASN_DESTINATIONTANK AS "DEST. Tank",
        STATUS AS "Status",
        'NO_DATA' AS "NoData1"
  FROM ( SELECT 'NO_DATA' AS NoData2 FROM DUAL
       ) b LEFT JOIN
       XVW_ASN_OVERVIEW z
      b  
      ON 1 = 1;

也就是说,你并不需要一个条件。

请注意其他一些变化:

  • 我切换到LEFT JOIN。我发现这些更容易理解 - 因为保留了第一个表中的所有行。
  • 我更喜欢COALESCE()NVL(),因为前者是ANSI标准。

答案 2 :(得分:0)

我看到你想要一条记录说&#34; No_DATA&#34;如果查询没有返回任何行。为此,您还需要NoData2列。 解析器在评估所选列之前想要连接表,因此它不知道您添加了NoData1。除非您将查询用作这样的临时表:

SELECT *
FROM 
(SELECT 'NO_DATA' as NoData2 FROM DUAL) b
LEFT JOIN
(SELECT
  CASE WHEN  VENDORDELIVERY IS NOT NULL THEN VENDORDELIVERY   ELSE 'No Delivery' END as "Vendor Delivery No.",
  DATE_TIME as "Delivery Date/Time",
  'z0' AS "Icon1",
  'z0' AS "Icon2",
  'z0' AS "Icon3",
  'z0' AS "Icon4",
  NVL(completion,0) AS "Progress",
  'd0' AS "Active",
  MATERIALCODE as "Material Code",
  MATERIAL as "Material",
  DELIVERY_QTY as "Delivery Qty",
  ASN_UNLOADINGSTATION AS "Unload. Manf",
  ASN_DESTINATIONTANK AS "DEST. Tank",
  STATUS AS "Status" , 'NO_DATA' AS "NoData1"
FROM  XVW_ASN_OVERVIEW) Z
ON NoData1 = NoData2