如何在查询中设置表达式变量选择oracle sql

时间:2018-08-13 10:22:31

标签: sql oracle oracle-sqldeveloper

我有这样的Oracle SQL:

SELECT  
z."date", z.id_outlet as idOutlet, z.name as outletName, z.matClass, z.targetBulanan, z.targetBulanan/totalVisit as targetAwal,
z.actual,rownumber = tartot + rownumber as targetTotal
                FROM (SELECT 
                b.visit_date as "date", a.id_outlet, max(o.name) as name, max(a.target_sales) as targetBulanan, a.id_material_class as matClass, 
                max(x.totalVisit) as totalVisit, NVL(SUM(d.billing_value),0) as actual 
                FROM (
                select * from target_bulanan 
                where deleted = 0 and enabled = 1 and id_salesman = :id_salesman AND id_material_class like :id_material_class AND id_outlet like :id_outlet AND month = TO_NUMBER(TO_CHAR(current_date,'mm')) and year = to_number(TO_CHAR(current_date,'YYYY')) 
                ) a 
                INNER JOIN outlet o ON o.id_outlet = a.id_outlet 
                LEFT JOIN visit_plan b ON b.deleted = 0 and a.id_salesman = b.id_salesman AND a.month =  TO_NUMBER(TO_CHAR(b.visit_date,'mm')) AND a.year = to_number(TO_CHAR(b.visit_date,'yyyy')) AND a.id_outlet = b.id_outlet 
                LEFT JOIN so_header c ON SUBSTR(c.id_to,'0',1) = 'TO' AND  a.id_salesman = c.id_salesman AND a.id_outlet = c.id_outlet 
                LEFT JOIN assign_billing d ON c.no_so_sap = d.no_so_sap AND d.billing_date = b.visit_date AND a.id_material_class = (SELECT id_material_class FROM material WHERE id = d.id_material) 
                LEFt JOIN (SELECT id_salesman, to_char(visit_date,'mm') as month, to_char(visit_date,'yyyy') as year, id_outlet, COUNT(*) as totalVisit  FROM visit_plan 
                WHERE deleted = 0 
                group by id_salesman, id_outlet,to_char(visit_date,'mm'), to_char(visit_date,'yyyy')) x on 
                x.id_salesman = a.id_salesman AND x.month =  a.month AND x.year = a.year AND x.id_outlet = a.id_outlet
                GROUP BY b.visit_date, a.id_outlet, a.id_material_class) z 
                CROSS JOIN (SELECT 0 as rownumber FROM DUAL ) r
                CROSS JOIN (SELECT 0 as tartot FROM DUAL ) t
                CROSS JOIN (SELECT '' as mat FROM DUAL ) m
                CROSS JOIN (SELECT '' as outlet FROM DUAL ) o
                ORDER by outletName, z.matClass, z."date"

我希望在我的选择查询中,行号的值是公式,但此消息的结果是错误

  

ORA-00923:在预期位置找不到FROM关键字   00923. 00000-“未在预期的位置找到FROM关键字”

有人可以帮助我吗?谢谢

2 个答案:

答案 0 :(得分:0)

此地址为原始问题。

您的查询中可能有多个问题。毕竟,调试和编写查询的最佳方法是从简单开始,逐步增加复杂性。

但是,您确实有一个明显的错误。在最外面的select中:

SELECT z."date", z.id_outlet as idOutlet, z.name as outletName, 
       z.matClass, z.targetBulanan, z.targetBulanan/totalVisit as targetAwal,
       z.actual,
       rownumber = rownumber + 1 as row_number

=不是Oracle语法-它看起来像是SQL Server扩展名,用于命名列或MySQL使用变量。

我怀疑您想枚举行。如果是这样,则一种语法为row_number()

SELECT z."date", z.id_outlet as idOutlet, z.name as outletName, 
       z.matClass, z.targetBulanan, z.targetBulanan/totalVisit as targetAwal,
       z.actual,
       row_number() over (order by outletName, z.matClass, z."date") as row_number

在Oracle中,您也可以这样做:

       rownum as row_number

答案 1 :(得分:0)

仅用于枚举- 替换行

rownumber = rownumber + 1 AS row_number

与此

rownum AS row_number

rownum是Oracle的内置函数,它枚举结果集的每条记录并自动递增

Gordon Linoff在回答中提到,您的查询中还有其他问题。

乍一看(不执行它),我可以列出有问题的行-

AND month = TO_NUMBER(TO_CHAR(current_date,'mm')) 
AND year = to_number(TO_CHAR(current_date,'YYYY'))

而不是current_date使用sysdate

LEFT JOIN so_header c ON SUBSTR(c.id_to,'0',1) = 'TO'

我想,你打算这样做-

LEFT JOIN so_header c ON SUBSTR(c.id_to,0,2) = 'TO'

即从索引0到2个字符的子字符串

此外,不需要那些交叉联接