过滤子查询结果

时间:2013-01-20 09:37:40

标签: sql oracle subquery

我在查询中过滤重复值时遇到问题。这是我的查询

SELECT  cid.IDS_NO,cid.SUB_TITLE,cid.E_SUB_NAME, (cid.SUB_TITLE || ' '|| cid.E_SUB_NAME),
(SELECT(INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME FROM street_table where street_code = d.INST_ST_CODE)) 
FROM del d where d.IDS_NO = cid.IDS_NO and ROWNUM = 1), 
(SELECT INST_ST_LEVEL || '-' || INST_STUNIT FROM detail d where d.IDS_NO = cid.IDS_NO and rownum = 1),
('COUNTRY' ||' ' || pl_post),pl_post 
FROM SUBSCRIBER cid where pl_postal_district = 15 and rownum < 3001 
and hi_property_type IN ('CONDO','COMMERCIAL BUILDING');

这些是我遇到问题的部分:

(SELECT(INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME FROM street_table where street_code = d.INST_ST_CODE)) 
FROM del d where d.IDS_NO = cid.IDS_NO and ROWNUM = 1), 
(SELECT INST_ST_LEVEL || '-' || INST_STUNIT FROM detail d where d.IDS_NO = cid.IDS_NO and rownum = 1);

这些部分总是得到多条记录,我想只获得序列号最低的1条记录,所以这就是我所做的:

(SELECT(INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME FROM street_table where street_code = d.INST_ST_CODE)) 
FROM del d where d.IDS_NO = cid.IDS_NO and ROWNUM = 1 ORDER BY d.SEQ_NO DESC), 
(SELECT INST_ST_LEVEL || '-' || INST_STUNIT FROM detail d where d.IDS_NO = cid.IDS_NO and rownum = 1 ORDER BY d.SEQ_NO DESC);

但我总是得到一个错误说错误的括号,但当我删除订单时,它工作正常。我该怎么做才能使我的查询正常运行?

2 个答案:

答案 0 :(得分:3)

格式化查询以使其更易于阅读还有很长的路要走。

但是你也有一个相当棘手的问题,因为你......

  

希望只获得序列号最低的1条记录。

应该像这样工作:

SELECT s.ids_no
      ,s.sub_title
      ,s.e_sub_name
      ,(s.sub_title || ' '|| s.e_sub_name) AS title_sub_name
      ,d.some_col_name
      ,t.inst_name
      ,('COUNTRY' ||' ' || s.pl_post) AS country_post
      ,s.pl_post
FROM   subscriber s

LEFT   JOIN (
   SELECT d.ids_no
         ,(d.inst_blk_hse || ' ' || st.e_street_name) AS some_col_name
         ,ROW_NUMBER() OVER (PARTITION BY d.ids_no ORDER BY d.seq_no) AS rn
   FROM   del d
   LEFT   JOIN e_street_name st ON st.street_code = d.inst_st_code
   ) d ON d.ids_no = s.ids_no AND d.rn = 1

LEFT   JOIN (
   SELECT ids_no
         ,(inst_st_level || '-' || inst_stunit)  AS inst_name
         ,ROW_NUMBER() OVER (PARTITION BY d.ids_no ORDER BY d.seq_no) AS rn
   FROM   detail         
   ) t ON t.ids_no = s.ids_no AND t.rn = 1


WHERE  s.pl_postal_district = 15
AND    s.rownum < 3001 
AND    s.hi_property_type IN ('CONDO','COMMERCIAL BUILDING');

有关子查询如何在此相关问题下工作的更多详细信息:
Select first row in each GROUP BY group?

核心功能是ROW_NUMBER(),以获得序列号最低的记录 我重写了相关的子查询,因为它们通常会在性能方面受到影响。它们也难以阅读。
我使用LEFT JOIN,所以如果子查询找不到任何匹配的内容,你仍会得到一行。

答案 1 :(得分:1)

子查询必须只返回一行。因此,您必须将rownum = 1添加到子查询FROM del d内的子查询中。类似的东西:

SELECT  
  cid.IDS_NO,
  cid.SUB_TITLE,
  cid.E_SUB_NAME, 
  (cid.SUB_TITLE || ' '|| cid.E_SUB_NAME),
  (SELECT (INST_BLK_HSE || ' ' || (SELECT E_STREET_NAME 
                                   FROM street_table 
                                   where street_code = d.INST_ST_CODE
                                     and rownum = 1)) <<<<<<< here
   FROM del d 
   where d.IDS_NO = cid.IDS_NO 
     and ROWNUM = 1), 
  (SELECT INST_ST_LEVEL || '-' || INST_STUNIT 
   FROM detail d 
   where d.IDS_NO = cid.IDS_NO 
   and rownum = 1),
  ('COUNTRY' ||' ' || pl_post),
  pl_post 
FROM SUBSCRIBER cid 
where pl_postal_district = 15 
  and rownum < 3001 
  and hi_property_type IN ('CONDO','COMMERCIAL BUILDING');

或者,JOIN所有表格如下:

SELECT
  cid.IDS_NO,
  cid.SUB_TITLE,
  cid.E_SUB_NAME, 
  cid.SUB_TITLE || ' '|| cid.E_SUB_NAME,
  d.INST_BLK_HSE || ' ' || s.E_STREET_NAME, 
  d.INST_ST_LEVEL || '-' || d.INST_STUNIT 
  'COUNTRY' ||' ' || pl_post),
  pl_post 
FROM SUBSCRIBER cid 
INNER JOIN del          d ON d.IDS_NO      = cid.IDS_NO 
INNER JOIN detail      d2 ON d2.IDS_NO     = cid.IDS_NO 
INNER JOIN street_table s ON s.street_code = d.INST_ST_CODE
WHERE cid.pl_postal_district = 15 
  and cid.rownum < 3001 
  and cid.hi_property_type IN ('CONDO','COMMERCIAL BUILDING')
  and s.rownum = 1
  and d.rownum = 1
  and d2.rownum = 1;