SQL ORACLE有关查询的帮助(ORA-00907:缺少右括号)

时间:2015-02-13 21:23:11

标签: sql oracle

我试图获得从未拥有每种类型汽车中最受欢迎的汽车的人的名字(每年所有汽车,suv,轿车......等)。在给定年份中最受欢迎的车型是制造商和车型,其车型比其他车型更多。

这些是我创建的表格 auto_sale(transaction_id,seller_id(sin),buyer_id(sin),vehicle_id(serial_no),s_date(yyyymmdd),price) 人(罪,名) 车辆(serial_no,制造商,型号,年份,颜色,type_id)

这是我提出的查询工作的问题,它一直在说太多的值,我试着在vehicle.maker / model =(..的子查询中使用别名) 。)澄清联合表格,但它一直在说错过正确的括号,所以请在这个问题上给我一些亮点。

select people.name
from auto_sale,people
where name not in(select people.name from people,auto_sale,vehicle where buyer_id = people.sin and
                  auto_sale.vehicle_id=vehicle.serial_no and
                  vehicle.maker = (select vehicle.maker
                                from (auto_sale
                                     join
                                     vehicle
                                     on auto_sale.vehicle_id = vehicle.serial_no)v1
                                group by extract(year from to_date(v1.s_date,'yyyymmdd'), maker
                                having count(*) >= all(select count(*)
                                                       from (auto_sale
                                                       join
                                                       vehicle
                                                       on auto_sale.vehicle_id = vehicle.serial_no)v2
                                                       where extract(year from to_date(v1.s_date,'yyyymmdd')=
                                                       extract(year from to_date(v2.s_date,'yyyymmdd')
                                                       group by extract(year from to_date(v2.s_date,'yyyymmdd'),ma\
ker,type_id) ) and
                  vehicle.model = (select vehicle.model
                                from (auto_sale
                                     join
                                     vehicle
                                     on auto_sale.vehicle_id = vehicle.serial_no)v3
                                group by extract(year from todate(v3.s_date,'yyyymmdd'), model
                                having count(*) >= all(select count(*)
                                                       from (auto_sale
                                                       join
                                                       vehicle
                                                       on auto_sale.vehicle_id = vehicle.serial_no)v4
                                                       where extract(year from to_date(v3.s_date,'yyyymmdd')=
                                                       extract(year from to_date(v4.s_date,'yyyymmdd')
                                                       group by extract(year from to_date(s_date,'yyyymmdd'),v4mod\
el,v4.type_id))
                   group by people.name);

2 个答案:

答案 0 :(得分:2)

起初我正在编写我正在修改语法问题所做的所有修改,但是当我意识到这是我写过的最长的答案而且它的任何部分都没有真正有用时,我决定只需复制固定查询。

select people.name
from auto_sale,people
where name not in(select people.name 
                    from people,
                         auto_sale,
                         vehicle 
                    where buyer_id = people.sin 
                      and auto_sale.vehicle_id = vehicle.serial_no
                      and vehicle.maker = (select vehicle.maker
                                            from auto_sale
                                            join vehicle on auto_sale.vehicle_id = vehicle.serial_no v1
                                            group by extract(year from to_date(v1.s_date,'yyyymmdd')), maker
                                            having count(*) >= all(select count(*)
                                                                        from auto_sale
                                                                        join vehicle on auto_sale.vehicle_id = vehicle.serial_no v2
                                                                        where extract(year from to_date(v1.s_date,'yyyymmdd')) = extract(year from to_date(v2.s_date,'yyyymmdd'))
                                                                        group by extract(year from to_date(v2.s_date,'yyyymmdd'),maker,type_id))) 
                      and vehicle.model = (select vehicle.model
                                            from auto_sale
                                            join vehicle on auto_sale.vehicle_id = vehicle.serial_no v3
                                            group by extract(year from todate(v3.s_date,'yyyymmdd')), model
                                            having count(*) >= all(select count(*)
                                                                    from auto_sale
                                                                    join vehicle on auto_sale.vehicle_id = vehicle.serial_no) v4
                                            where extract(year from to_date(v3.s_date,'yyyymmdd'))= extract(year from to_date(v4.s_date,'yyyymmdd'))
                                            group by extract(year from to_date(s_date,'yyyymmdd')),v4.model,v4.type_id))
group by people.name;

我不知道你是如何能够遇到这么多语法问题的。这里有一些提示可以避免语法问题:

  • 在编辑器中编写代码,在语法奇怪时发出警告
  • 确保正确缩进代码
  • 使用与团队中每个人相同的编辑器,以便处理相同代码的不同人员不会破坏语法
  • 某些编辑器(示例toad)允许定义格式模板。编写完查询后,只需单击格式即可获得可读语法,这样可以节省大量的调试时间。

请注意,我没有检查查询逻辑是否正常,只是确保修复所有语法问题,因为这是您的初始问题。我在这里假设其余的都很好。

答案 1 :(得分:1)

从子查询返回的列数应与要与之比较的列匹配。

...  and 
      vehicle.maker = (select year(auto_sale.s_date), vehicle.maker
                        from 
...

此外,

... and
      vehicle.model = (select year(auto_sale.s_date), vehicle.model
                            from
...

应该是

之类的东西
... vehicle.maker = (select  vehicle.maker from ...

需要注意的是,在使用' ='时,子查询应该只返回1行。

编辑:在第二次采取时,有很多东西看起来不适合这个sql。

where year(s_date)=year(s_date)

年份似乎是您表格中的一列,s_date也是如此。不知道你用年份(s_date)完成了什么。即使它是有效的语法(它不是'),方程的两边都是相同的,这意味着那里不需要WHERE子句。您可能希望在每个级别对表进行别名并相应地使用这些条件。

人员和auto_sale在顶级查询中交叉加入。不确定这是否正确。