Объединение созданной таблицы и запроса Listagg - PullRequest
0 голосов
/ 28 июня 2018

В настоящее время у меня есть два раздела кода, один для создания таблицы, другой для listagg таблицы. Код выглядит примерно так:

Create table NewTable nologging as
select  distinct x as customerNumber,
y as items bought,
z as prices
from 
    customerNum,
    numItems,
    itemPrice
where  (z not in ('2.00','3.00','NA'))

и затем Листагг, следующий за

create table formattingSection nologging as
select newTable.customerNumber, listagg(newTable.bought,',') within group (order 
by bought) as boughtdesc
from newTable
group by customerNumber

Можно ли объединить эти два раздела в один раздел? так что у меня нет 2 таблицы?

1 Ответ

0 голосов
/ 29 июня 2018

Как-то так, я полагаю:

create table formattingSection nologging as
select 
  customerNumber, 
  listagg(bought, ',') within group (order by bought) as boughtdesc
from 
  -- instead of "newTable" itself, use SELECT statement from "CREATE TABLE newTable"
  (select distinct x as customerNumber,
                   y as items bought,
                   z as prices
   from 
     customerNum,
     numItems,
     itemPrice
   where (z not in ('2.00', '3.00', 'NA'))
     -- and ... --> you should join those tables, somehow. Otherwise, Cartesian product 
     -- will be created, and that's probably not what you want. 
  )
group by customerNumber;

Помимо комментариев, которые я написал в коде, столбец "z", по-видимому, представляет "цену", и кажется, что вы сохраняете его в столбце VARCHAR2. Я бы посоветовал вам не делать этого; используйте NUMBER тип данных. Зачем? Потому что ничто не мешает вам вводить цены как "x.% I" или "2 = & 3". Если, например, решение было принято из-за «NA», нет проблем - вы просто оставили бы его пустым , его значение было бы NULL. Для целей отображения вам нужно DECODE такое значение: decode(z, null, 'NA', z).

...