powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Oracle [игнор отключен] [закрыт для гостей] / UNION PL/SQL
7 сообщений из 7, страница 1 из 1
UNION PL/SQL
    #39745101
1dva3
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Добрый день!
У меня проблема с объединением двух запросов. Первый запрос считает открытые/закрытые установки по услугам связи А, второй запрос считает открытые/закрытые установки по услугам связи Б.
Мне необходимо вывести результат одним запросом.
пример.

--Атырау СТС

select trim(r.name) "РЕГИОН",
sum(case when od.end_sysdate is not null then 1 end)"Закрытые СТС" , --ЭТО УСЛУГА А
sum(case when od.end_sysdate is null then 1 end) "Открытые СТС"
from db.order_device od, db.report_date rd, db.device_group dg, db.connect_type ct, db.town t,
db.region r, db.crm_server cs,
(select 383074 id from dual union
select 144786 from dual) tab
where tab.id=od.abonent_id
and od.new_connect_type_id=ct.id
and od.begin_sysdate between rd.from_date and rd.to_date
and rd.id=11
and dg.id=od.device_group_id
and dg.id= decode(cs.id, 3, 24, 13, 33, 4, 37, 11, 24, -1)
and od.town_id=t.id
and t.region_id=r.id
and od.action_id in (9)
group by r.name
order by r.name
------------------------
--Атырау ГТС
union
select trim(r.name) "РЕГИОН",
sum(case when od.end_sysdate is not null then 1 end)Закрытые ГТС, --ЭТО Услуга Б
sum(case when od.end_sysdate is null then 1 end) "Открытые ГТС"
from db.order_device od, db.report_date rd, db.device_group dg, db.connect_type ct, db.town t,
db.region r, db.crm_server cs,
(select 131690 id from dual union
select 343792 from dual) tab
where tab.id=od.abonent_id
and od.new_connect_type_id=ct.id
and od.begin_sysdate between rd.from_date and rd.to_date
and rd.id=11
and dg.id=od.device_group_id
and dg.id= decode(cs.id, 3, 24, 13, 33, 4, 37, 11, 24, -1)
and od.town_id=t.id
and t.region_id=r.id
and od.action_id in (9)
group by r.name
order by r.name
...
Рейтинг: 0 / 0
UNION PL/SQL
    #39745108
Фотография Щукина Анна
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
1dva3Добрый день!
У меня проблема с объединением двух запросов. Первый запрос считает открытые/закрытые установки по услугам связи А, второй запрос считает открытые/закрытые установки по услугам связи Б.
Мне необходимо вывести результат одним запросом.
пример.

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
--Атырау СТС

select trim(r.name) "РЕГИОН",
        sum(case when od.end_sysdate is not null then 1 end)"Закрытые СТС" , --ЭТО УСЛУГА А
        sum(case when od.end_sysdate is null then 1 end) "Открытые СТС"
from db.order_device od, db.report_date rd, db.device_group dg, db.connect_type ct, db.town t,
db.region r, db.crm_server cs,
 (select	383074	id from dual union
select	144786	from dual) tab
  where tab.id=od.abonent_id
        and od.new_connect_type_id=ct.id
        and od.begin_sysdate between rd.from_date and rd.to_date
        and rd.id=11
        and dg.id=od.device_group_id
        and dg.id= decode(cs.id, 3, 24, 13, 33, 4, 37, 11, 24, -1)
        and od.town_id=t.id
        and t.region_id=r.id
        and od.action_id in (9)
        group by r.name
        order by r.name
------------------------
--Атырау ГТС
union
select trim(r.name) "РЕГИОН",
        sum(case when od.end_sysdate is not null then 1 end)Закрытые ГТС, --ЭТО Услуга Б
        sum(case when od.end_sysdate is null then 1 end) "Открытые ГТС"
from db.order_device od, db.report_date rd, db.device_group dg, db.connect_type ct, db.town t,
db.region r, db.crm_server cs,
 (select	131690	id from dual union
select	343792	from dual) tab
  where tab.id=od.abonent_id
       and od.new_connect_type_id=ct.id
        and od.begin_sysdate between rd.from_date and rd.to_date
        and rd.id=11
        and dg.id=od.device_group_id
        and dg.id= decode(cs.id, 3, 24, 13, 33, 4, 37, 11, 24, -1)
        and od.town_id=t.id
        and t.region_id=r.id
        and od.action_id in (9)
        group by r.name
        order by r.name


как минимум - лишний ORDER BY между секциями UNION-запроса (в скрипте - выделено цветом)...
...
Рейтинг: 0 / 0
UNION PL/SQL
    #39745110
Фотография Щукина Анна
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Щукина Анна1dva3Добрый день!
У меня проблема с объединением двух запросов. Первый запрос считает открытые/закрытые установки по услугам связи А, второй запрос считает открытые/закрытые установки по услугам связи Б.
Мне необходимо вывести результат одним запросом.
пример.

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
--Атырау СТС

select trim(r.name) "РЕГИОН",
        sum(case when od.end_sysdate is not null then 1 end)"Закрытые СТС" , --ЭТО УСЛУГА А
        sum(case when od.end_sysdate is null then 1 end) "Открытые СТС"
from db.order_device od, db.report_date rd, db.device_group dg, db.connect_type ct, db.town t,
db.region r, db.crm_server cs,
 (select	383074	id from dual union
select	144786	from dual) tab
  where tab.id=od.abonent_id
        and od.new_connect_type_id=ct.id
        and od.begin_sysdate between rd.from_date and rd.to_date
        and rd.id=11
        and dg.id=od.device_group_id
        and dg.id= decode(cs.id, 3, 24, 13, 33, 4, 37, 11, 24, -1)
        and od.town_id=t.id
        and t.region_id=r.id
        and od.action_id in (9)
        group by r.name
        order by r.name
------------------------
--Атырау ГТС
union
select trim(r.name) "РЕГИОН",
        sum(case when od.end_sysdate is not null then 1 end)Закрытые ГТС, --ЭТО Услуга Б
        sum(case when od.end_sysdate is null then 1 end) "Открытые ГТС"
from db.order_device od, db.report_date rd, db.device_group dg, db.connect_type ct, db.town t,
db.region r, db.crm_server cs,
 (select	131690	id from dual union
select	343792	from dual) tab
  where tab.id=od.abonent_id
       and od.new_connect_type_id=ct.id
        and od.begin_sysdate between rd.from_date and rd.to_date
        and rd.id=11
        and dg.id=od.device_group_id
        and dg.id= decode(cs.id, 3, 24, 13, 33, 4, 37, 11, 24, -1)
        and od.town_id=t.id
        and t.region_id=r.id
        and od.action_id in (9)
        group by r.name
        order by r.name


как минимум - лишний ORDER BY между секциями UNION-запроса (в скрипте - выделено цветом)...ну и в финальном ORDER BY - некорректная ссылка на имя столбца. Нужно использовать назначенный алиас для колонки, либо - номер колонки в итоговой выборке:
order by "РЕГИОН"
ИЛИ
order by 1
...
Рейтинг: 0 / 0
UNION PL/SQL
    #39745112
Фотография Щукина Анна
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
1dva3,

в целом - надобность в UNION - сильно под вопросом. Как минимум - заменить на UNION ALL. Как максимум - переписать все в один запрос, так как список таблиц и условия фильтрации (за исключением встроенного представления TAB) - идентичен.
...
Рейтинг: 0 / 0
UNION PL/SQL
    #39745114
1dva3
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Щукина Анна, спасибо большое
но он не выдает 2 запрос.
в конечном результате должно быть 5 столбцов.
Регион, ЗакрытыеСТС, ОткрытыеСТС, ЗакрытыеГТС,ОткрытыеГТС


во вложении результат

Модератор: Тема перенесена из форума "MySQL".
...
Рейтинг: 0 / 0
UNION PL/SQL
    #39745124
1dva3
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Щукина Анна,

отработался запрос))) спасибо
но мне нужно чтоб он по столбцам разделил. Скорее всего мне LEFT JOIN нужен(((
вы мне не поможете пжл(((
...
Рейтинг: 0 / 0
UNION PL/SQL
    #39745193
MaximaXXL
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
1dva3,

Скорее всего Вам надо переписать запрос на:

Код: plsql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
select trim(r.name) "РЕГИОН",
        sum(case when od.abonent_id in (131690, 343792) and od.end_sysdate is not null then 1 end)Закрытые ГТС, --ЭТО Услуга Б
        sum(case when od.abonent_id in (131690, 343792) and od.end_sysdate is null then 1 end) "Открытые ГТС",
        sum(case when od.abonent_id in (383074, 144786) and od.end_sysdate is not null then 1 end)"Закрытые СТС" , --ЭТО УСЛУГА А
        sum(case when od.abonent_id in (383074, 144786) and od.end_sysdate is null then 1 end) "Открытые СТС"
from db.order_device od, db.report_date rd, db.device_group dg, db.connect_type ct, db.town t,
db.region r, db.crm_server cs
  where od.abonent_id in (131690, 343792/*ГТС ЭТО Услуга Б*/, 383074, 144786/*СТС ЭТО УСЛУГА А*/)
       and od.new_connect_type_id=ct.id
        and od.begin_sysdate between rd.from_date and rd.to_date
        and rd.id=11
        and dg.id=od.device_group_id
        and dg.id= decode(cs.id, 3, 24, 13, 33, 4, 37, 11, 24, -1)
        and od.town_id=t.id
        and t.region_id=r.id
        and od.action_id in (9)
        group by r.name
        order by r.name
...
Рейтинг: 0 / 0
7 сообщений из 7, страница 1 из 1
Форумы / Oracle [игнор отключен] [закрыт для гостей] / UNION PL/SQL
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]