2016-11-03 4 views
2

У меня есть ниже запрос:Добавить строку для отображения суммы в последнем столбце

SELECT 
    cc.chain_desc as chain_desc 
    ,cc.chain_id as chain_id 
    ,COUNT(distinct t.trans_id) as TranCount 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00' 
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 
GROUP BY cc.chain_desc, cc.chain_id 
ORDER BY cc.chain_desc, cc.chain_id 

Когда я выполнить запрос, я получаю результат ниже:

enter image description here

Но, необходимо добавить строку «Всего», в которой будет отображаться сумма всех транзакций в последнем столбце, как показано ниже:

enter image description here

Какие изменения должны внести в запрос для отображения приведенного выше результата?.

Я попытался с помощью накопительного пакета, как показано ниже, но я не получить ожидаемый результат:

SELECT 
    cc.chain_desc as chain_desc 
    ,cc.chain_id as chain_id 
    ,COUNT(distinct t.trans_id) as TranCount 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00' 
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 
GROUP BY cc.chain_desc, cc.chain_id with Rollup 
ORDER BY cc.chain_desc, cc.chain_id 

ответ

3

Простым способом является использованием UNION с избранным для полного

SELECT 
    cc.chain_desc as chain_desc 
    ,cc.chain_id as chain_id 
    ,COUNT(distinct t.trans_id) as TranCount 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00' 
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 
GROUP BY cc.chain_desc, cc.chain_id 
ORDER BY cc.chain_desc, cc.chain_id 

UNION 

SELECT 
    'TOTAL' 
    null 
    ,COUNT(distinct t.trans_id) 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00' 
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 
1

это работало для меня:

SELECT 
    cc.chain_desc as chain_desc 
    ,cc.chain_id as chain_id 
    ,COUNT(distinct t.trans_id) as TranCount 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00' 
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 
GROUP BY cc.chain_desc, cc.chain_id 


UNION 

SELECT 
    'TOTAL', 
    0 
    ,COUNT(distinct t.trans_id) 
FROM TRANSACTION AS t 
LEFT OUTER JOIN location AS l 
ON t.location_id = l.location_id 
LEFT OUTER JOIN trans_line AS tl 
ON t.trans_id = tl.trans_id 
LEFT OUTER JOIN contract as c 
ON t.contract_id = c.contract_id 
LEFT OUTER JOIN chain_desc as cc 
ON l.chain_id = cc.chain_id 
WHERE 
t.loc_country = 'U' 
AND c.issuer_id IN (156966,166203) 
AND t.trans_date >= '2016-10-01 00:00' 
and t.trans_date < '2016-10-31 00:00' 
AND tl.cat NOT IN ('DEF','DEFD','DEFC') 

 Смежные вопросы

  • Нет связанных вопросов^_^