使用SQL查询减少输出

时间:2017-03-30 19:44:04

标签: sql oracle

表中的数据是:

票务表

Ticket_Nbr            ,  aaa.pnr_ticket_id
2303167095666         ,  5889503
2         ,  2345
3         ,  3456
4         ,  4567
5         ,  5678

优惠券表

CPN_FRM_SER       ,  CPN_NUM,  FRM_CITY,  TO_CITY
2303167095666     ,  1      ,  SCL     ,  PTY
2303167095666     ,  2      ,  PTY     ,  SFO
2303167095666     ,  3      ,  SFO     ,  SEA
2303167095666     ,  4      ,  SEA     ,  SFO

查询是:

    SELECT 
    XMLElement
    (
        "Eticket", 
        XMLElement("Ticket", XMLAttributes(t.ticket_nbr AS "NBR")), 
        (
            SELECT XMLAGG(XMLElement("COUPON", XMLAttributes(c.cpn_num AS "COUPON_NUM", c.frm_city AS "FROM_CITY", c.to_city AS "TO_CITY")))
            FROM coupon c
            WHERE c.cpn_frm_ser = t.ticket_nbr
        ) AS "coupon"
    )
FROM ticket t,
    coupon c
WHERE c.cpn_frm_ser = t.ticket_nbr
    AND t.aaa_pnr_ticket_id = '5889503';

产生的输出是:

<Eticket>
  <Ticket NBR="2302167019566"></Ticket>
  <AAA_PNR_TICKET ISSUE_DATE="2017-02-13" HA_CC="CM"></AAA_PNR_TICKET>
  <COUPON COUPON_NUM="1" FROM_CITY="SCL" TO_CITY="PTY"></COUPON>
  <COUPON COUPON_NUM="2" FROM_CITY="PTY" TO_CITY="SFO"></COUPON>
  <COUPON COUPON_NUM="3" FROM_CITY="SFO" TO_CITY="SEA"></COUPON>
  <COUPON COUPON_NUM="4" FROM_CITY="SEA" TO_CITY="SFO"></COUPON>
</Eticket>
<Eticket>
  <Ticket NBR="2302167019566"></Ticket>
  <AAA_PNR_TICKET ISSUE_DATE="2017-02-13" HA_CC="CM"></AAA_PNR_TICKET>
  <COUPON COUPON_NUM="1" FROM_CITY="SCL" TO_CITY="PTY"></COUPON>
  <COUPON COUPON_NUM="2" FROM_CITY="PTY" TO_CITY="SFO"></COUPON>
  <COUPON COUPON_NUM="3" FROM_CITY="SFO" TO_CITY="SEA"></COUPON>
  <COUPON COUPON_NUM="4" FROM_CITY="SEA" TO_CITY="SFO"></COUPON>
</Eticket>
<Eticket>
  <Ticket NBR="2302167019566"></Ticket>
  <AAA_PNR_TICKET ISSUE_DATE="2017-02-13" HA_CC="CM"></AAA_PNR_TICKET>
  <COUPON COUPON_NUM="1" FROM_CITY="SCL" TO_CITY="PTY"></COUPON>
  <COUPON COUPON_NUM="2" FROM_CITY="PTY" TO_CITY="SFO"></COUPON>
  <COUPON COUPON_NUM="3" FROM_CITY="SFO" TO_CITY="SEA"></COUPON>
  <COUPON COUPON_NUM="4" FROM_CITY="SEA" TO_CITY="SFO"></COUPON>
</Eticket>
<Eticket>
  <Ticket NBR="2302167019566"></Ticket>
  <AAA_PNR_TICKET ISSUE_DATE="2017-02-13" HA_CC="CM"></AAA_PNR_TICKET>
  <COUPON COUPON_NUM="1" FROM_CITY="SCL" TO_CITY="PTY"></COUPON>
  <COUPON COUPON_NUM="2" FROM_CITY="PTY" TO_CITY="SFO"></COUPON>
  <COUPON COUPON_NUM="3" FROM_CITY="SFO" TO_CITY="SEA"></COUPON>
  <COUPON COUPON_NUM="4" FROM_CITY="SEA" TO_CITY="SFO"></COUPON>
</Eticket>

同一行输出数据重复4次。

我只寻找一条输出数据线,而不是4条线,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

您无需在外面加入优惠券表。只需相关的子查询即可。

select XMLElement("Eticket",
            XMLElement("Ticket", XMLAttributes(t.ticket_nbr as "NBR")),
            (
            select XMLAGG(
                XMLElement(
                    "COUPON", 
                    XMLAttributes(
                        c.cpn_num as "COUPON_NUM",
                        c.frm_city as "FROM_CITY",
                        c.to_city as "TO_CITY"
                        )
                    )
                )
            from coupon c
            where c.cpn_frm_ser = t.ticket_nbr
            ) as "coupon")
from ticket t
where t.aaa_pnr_ticket_id = '5889503';

Demo