0

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

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);

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

4

2 回答 2

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');

有关子查询如何在此相关问题下工作的更多详细信息:
选择每个 GROUP BY 组中的第一行?

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

于 2013-01-20T10:15:35.647 回答
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;
于 2013-01-20T09:55:36.997 回答