在select语句中链接查询

时间:2013-12-03 04:37:51

标签: sql oracle11g

From the query i am able to return the output as expected ..

是否可以使用select语句在下面标记的引号中编写相同的查询:

SELECT ACC.ACCOUNT_NUM AS ACCOUNT_NUMBER,
   ACC.ACCOUNT_NAME AS ACCOUNT_NAME,
   ADR.ADDRESS_1 AS BUILDING_TYPE,
   ACC.CUSTOMER_REF AS CUSTOMER_ID,
   CAT.BC AS BILL_CYCLE,
   CES.EVENT_SOURCE AS TELEPHONE_NUMBER,
   AAT.PACKAGE_NAME AS PROMO_PACKAGE,
   PRD.PRODUCT_NAME AS SERVICES,
   (SELECT COUNT (DISTINCT (EVENT_SOURCE))
      FROM CUSTEVENTSOURCE CES1
     WHERE CES1.CUSTOMER_REF = ACC.CUSTOMER_REF AND END_DTM IS NULL)
      AS TOTAL_NUMBER,
   ("SELECT LISTAGG (EVENT_SOURCE, ', ')
              WITHIN GROUP (ORDER BY EVENT_SOURCE)
      FROM CUSTEVENTSOURCE
     WHERE CUSTOMER_REF = ACC.CUSTOMER_REF AND END_DTM IS NULL")
      AS ALL_IPHONE_NUMBERS,
   CUS.COMPANY_NAME AS COMPANY_NAME,
      ADR.ADDRESS_1
   || ' '
   || ADR.ADDRESS_2
   || ' '
   || ADR.ADDRESS_3
   || ' '
   || ADR.ADDRESS_4
   || ' '
   || ADR.ADDRESS_5
      AS BILLING_ADDRESS,
   (SELECT ADR.ADDRESS_1
      FROM ADDRESS ADR
     WHERE ADR.CUSTOMER_REF = ACC.CUSTOMER_REF
           AND ADDRESS_SEQ IN
                  (SELECT ADDRESS_SEQ
                     FROM CUSTPRODUCTADDRESS CPA
                    WHERE CPA.CUSTOMER_REF = ADR.CUSTOMER_REF
                          AND PRODUCT_SEQ IN
                                 (SELECT PRODUCT_SEQ
                                    FROM CUSTPRODUCTSTATUS CPS
                                   WHERE CPS.CUSTOMER_REF =
                                            CPA.CUSTOMER_REF
                                         AND EFFECTIVE_DTM =
                                                (SELECT MAX (
                                                           EFFECTIVE_DTM)
                                                   FROM CUSTPRODUCTSTATUS CPS1
                                                  WHERE CPS1.CUSTOMER_REF =
                                                           CPS.
                                                            CUSTOMER_REF))))
      AS INSTALLATION_ADDRESS,
   (SELECT ACS.EFFECTIVE_DTM
      FROM ACCOUNTSTATUS ACS
     WHERE ACS.ACCOUNT_NUM = ACC.ACCOUNT_NUM
           AND ACS.EFFECTIVE_DTM =
                  (SELECT MAX (EFFECTIVE_DTM)
                     FROM ACCOUNTSTATUS ACS1
                    WHERE ACS1.ACCOUNT_NUM = ACS.ACCOUNT_NUM
                          AND ACCOUNT_STATUS = 'OK'))
      AS ACTIVATION_DATE,
   (SELECT ACS.ACCOUNT_STATUS
      FROM ACCOUNTSTATUS ACS
     WHERE ACS.ACCOUNT_NUM = ACC.ACCOUNT_NUM
           AND ACS.EFFECTIVE_DTM =
                  (SELECT MAX (EFFECTIVE_DTM)
                     FROM ACCOUNTSTATUS ACS1
                    WHERE ACS1.ACCOUNT_NUM = ACS.ACCOUNT_NUM))
      AS ACCOUNT_STATUS,
   (SELECT ACS.STATUS_REASON_TXT
      FROM ACCOUNTSTATUS ACS
你可以帮我解决一下..我是sql ...请新的

提前致谢..

上述内容需要在“”双引号内进行更改 输出如下:

ACCOUNT_NUMBER     ACCOUNT_NAME  BUILDING_TYPE  CUSTOMER_ID  TELEPHONE_NUMBER  PROMO_PACKAGE TOTAL_NUMBER   ALL_IPHONE_NUMBERS,

MTX000110       John             xxxx            10002         123456     yyy    3   001-003,004,007

如果帐户有3个totoal_number,那么在All_iphone_numbers列中,如果值具有序列号,那么它应该使用分隔符“ - ”,否则它必须显示数字

如果我使用“”双引号数组..我得到输出为逗号分隔值,这是非常结果...结果应该看起来像“如果值有序列号那么它应该' - '用分隔符否则它必须显示数字“。

1 个答案:

答案 0 :(得分:0)

试试这个:(注意:我没有11G R2,所以我用wm_concat测试它,不支持,但如果你有11G R2,那么使用LISTAGG)

WITH TABIBITOSAN
    AS (SELECT
             ACCOUNT_REF,
             "number",
             TO_NUMBER ( "number" )
             - ROW_NUMBER ( ) OVER (ORDER BY TO_NUMBER ( "number" ))
                 AS GRP
        FROM
             TBL),
    DATASET1
    AS (SELECT
             ACCOUNT_REF,
             CASE
                 WHEN MIN ( "number" ) = MAX ( "number" )
                 THEN
                     MIN ( "number" )
                 ELSE
                        MIN ( "number" )
                     || '-'
                     || MAX ( "number" )
             END
                 AS RANGES
        FROM
             TABIBITOSAN
        GROUP BY
             ACCOUNT_REF,
             GRP
        ORDER BY
             ACCOUNT_REF,
             MIN ( "number" ))
SELECT
      ACCOUNT_REF,
      WM_CONCAT ( RANGES ) AS NEW_RANGES
FROM
      DATASET1
GROUP BY
      ACCOUNT_REF;

<强>结果:

101 002-004,006-008,011

使用LISTAGG

SELECT
      ACCOUNT_REF,
      LISTAGG ( RANGES,
              ',' )
      WITHIN GROUP (ORDER BY RANGES)
          AS NEW_RANGES
FROM
      DATASET1
WHERE
      ACCOUNT_REF = ACC.ACCOUNT_REF
GROUP BY
      ACCOUNT_REF