-- View: "v_payment_other"

DROP VIEW v_payment_other;

CREATE OR REPLACE VIEW v_payment_other AS
SELECT DISTINCT on (o.orderno)
    v.orderno,
    v.itemcd,
    v.structcd,
    v.stockcd,
    v.depositstate,
    v.sellercd,
    v.lastnm,
    v.firstnm,
    v.depositdate,
    v.sellerpaydate,
    n2.name AS name2,
    o2.typediv AS typediv2,
    o2.price   AS price2,
    n3.name AS name3,
    o3.typediv AS typediv3,
    o3.price   AS price3,
    n4.name AS name4,
    o4.typediv AS typediv4,
    o4.price   AS price4,
    n5.name AS name5,
    o5.typediv AS typediv5,
    o5.price   AS price5,
    n7.name AS name7,
    o7.typediv AS typediv7,
    o7.price   AS price7,
    n8.name AS name8,
    o8.typediv AS typediv8,
    o8.price   AS price8,
    v.updcnt

FROM
    v_payment_list v

    LEFT JOIN t_order_detail o
              ON v.orderno = o.orderno
	          AND o.typediv = 1

    LEFT JOIN t_order_detail o2
              ON v.orderno = o2.orderno
              AND o2.typediv = 2

    LEFT JOIN t_order_detail o3
              ON v.orderno = o3.orderno
              AND o3.typediv = 3

    LEFT JOIN t_order_detail o4
              ON v.orderno = o4.orderno
              AND o4.typediv = 4

    LEFT JOIN t_order_detail o5
              ON v.orderno = o5.orderno
              AND o5.typediv = 5

    LEFT JOIN t_order_detail o7
              ON v.orderno = o7.orderno
              AND o7.typediv = 7

    LEFT JOIN t_order_detail o8
              ON v.orderno = o8.orderno
              AND o8.typediv = 8

    LEFT JOIN t_name n2
              ON n2.namediv = 11
              AND n2.namecd = o2.typediv

    LEFT JOIN t_name n3
              ON n3.namediv = 11
              AND n3.namecd = o3.typediv

    LEFT JOIN t_name n4
              ON n4.namediv = 11
              AND n4.namecd = o4.typediv

    LEFT JOIN t_name n5
              ON n5.namediv = 11
              AND n5.namecd = o5.typediv

    LEFT JOIN t_name n7
              ON n7.namediv = 11
              AND n7.namecd = o7.typediv

    LEFT JOIN t_name n8
              ON n8.namediv = 11
              AND n8.namecd = o8.typediv



WHERE
    o.delflg = false

ORDER BY o.orderno, o.structcd, o.stockcd, v.sellercd, o.orderrowno;

