Total and Percentage for entire report when using sections in Cognos - cognos-bi

I'm using Cognos Analytics 11.0.12
I have a crosstab report with the following data:
SELECT 'A' GROUPNAME, 'A_1' ITEMNAME, 5 MYVAL FROM DUAL
UNION ALL SELECT 'A' GROUPNAME, 'A_1' ITEMNAME, 6 MYVAL FROM DUAL
UNION ALL SELECT 'A' GROUPNAME, 'A_1' ITEMNAME, 7 MYVAL FROM DUAL
UNION ALL SELECT 'A' GROUPNAME, 'A_2' ITEMNAME, 8 MYVAL FROM DUAL
UNION ALL SELECT 'A' GROUPNAME, 'A_2' ITEMNAME, 9 MYVAL FROM DUAL
UNION ALL SELECT 'B' GROUPNAME, 'B_1' ITEMNAME, 2 MYVAL FROM DUAL
UNION ALL SELECT 'B' GROUPNAME, 'B_1' ITEMNAME, 3 MYVAL FROM DUAL
UNION ALL SELECT 'B' GROUPNAME, 'B_2' ITEMNAME, 4 MYVAL FROM DUAL
UNION ALL SELECT 'B' GROUPNAME, 'B_2' ITEMNAME, 6 MYVAL FROM DUAL
UNION ALL SELECT 'B' GROUPNAME, 'B_2' ITEMNAME, 9 MYVAL FROM DUAL
And the resulting crosstab looks like this:
Grand Total uses the formula:
total([MYVAL] for report)
Percentage of Grand Total uses the formula:
percentage([MYVAL] for report)
I would like to section the crosstab by GROUPNAME, but when I do that, the section total is used to calculate Grand Total and Percentage of Grand Total rather than the total of the entire report.
Is there any way to avoid this behavior when sectioning?

Related

How to count distinct flags in the sql

BELOW IS SNIPPET OF MY DATA
Here is the sample creation code of testing.
CREATE TABLE MYGROUP ( Category,PERSON,Flag ) AS
SELECT 'Cat1','A','1' FROM DUAL
UNION ALL SELECT 'Cat1','A','0' FROM DUAL
UNION ALL SELECT 'Cat1','A','1' FROM DUAL
UNION ALL SELECT 'Cat1','B','1' FROM DUAL
UNION ALL SELECT 'Cat1','B','0' FROM DUAL
UNION ALL SELECT 'Cat2','A','0' FROM DUAL
UNION ALL SELECT 'Cat2','A','0' FROM DUAL
UNION ALL SELECT 'Cat2','A','0' FROM DUAL
UNION ALL SELECT 'Cat2','B','1' FROM DUAL
UNION ALL SELECT 'Cat2','B','1' FROM DUAL
UNION ALL SELECT 'Cat2','B','0' FROM DUAL
UNION ALL SELECT 'Cat3','X','0' FROM DUAL
UNION ALL SELECT 'Cat3','Y','0' FROM DUAL;
Desired Output:
Category - Count of Distinct Persons with Flag = 1
Cat1 - 2
Cat2 - 1
Cat3 - 0
I need to get my code in Big query to get distinct counts of persons. It shouldnt double count.
You can use conditional aggregation
SELECT
Category,
COUNT(DISTINCT CASE WHEN Flag = 1 THEN PERSON END)
FROM MYGROUP
GROUP BY Category;

How to group columns based on specific text and sum them using ORACLE database

I am a newbie in oracle. Trying to group columns based on a specific text and sum other columns.
let's say I have below tables
I want to group by all people with the last name. Like below
I have no clue how should I proceed.
Note: There could be multiple names but I just want to filter for Sharma, Decosta and Liver.
Updating the question.
Sorry for updating the question. but in a real scenario search string is not always at last.
Let me give you another example.
Output.
The queries offered so far does seem to work to get what you need. But the below does:
With inputs as
(
select 'Rahul Kumar Sharma' as Name, 10 as No_of_beer_bottles from dual union all
select 'Rohith Kumar Sharma' as Name, 5 as No_of_beer_bottles from dual union all
select 'Sharma' as Name, 20 as No_of_beer_bottles from dual union all
select 'Rahul Kumar Varma' as Name, 10 as No_of_beer_bottles from dual
)
select SUBSTR(name,Instr(name,' ',-1)+1) as Name, sum(No_of_beer_bottles) as No_of_beer_bottles
from inputs
group by SUBSTR(name,Instr(name,' ',-1)+1);
Output:
You can use regexp_substr():
select regexp_substr(name, '[^ ]+$') as last_name, sum(num_beers)
from t
group by regexp_substr(name, '[^ ]+$');
If you use have a table with all the words you are searching for (or define them in a Common Table Expression like I did below), you can join on that search term then sum the numeric column.
Query 1
WITH
search_words (word)
AS
(SELECT 'Sharma' FROM DUAL
UNION ALL
SELECT 'Decosta' FROM DUAL
UNION ALL
SELECT 'Liver' FROM DUAL),
d (name, num_beers)
AS
(SELECT 'Ravi Sharma', 1 FROM DUAL
UNION ALL
SELECT 'Sam Decosta', 4 FROM DUAL
UNION ALL
SELECT 'Jhony Liver', 5 FROM DUAL
UNION ALL
SELECT 'Ravi Sharma', 2 FROM DUAL
UNION ALL
SELECT 'Rohul Sharma', 3 FROM DUAL
UNION ALL
SELECT 'Jhon Decosta', 3 FROM DUAL
UNION ALL
SELECT 'V Decosta', 3 FROM DUAL)
SELECT sw.word, SUM (d.num_beers) AS total_beer
FROM search_words sw, d
WHERE INSTR (UPPER (d.name), UPPER (sw.word)) > 0
GROUP BY sw.word;
Result 1
WORD TOTAL_BEER
__________ _____________
Decosta 10
Sharma 6
Liver 5
Query 2
WITH
search_words (word)
AS
(SELECT 'Pen' FROM DUAL
UNION ALL
SELECT 'Dog' FROM DUAL),
d (name, quantity)
AS
(SELECT 'Ravi have red pens', 2 FROM DUAL
UNION ALL
SELECT 'Sam''s grifriend have black dogs', 4 FROM DUAL
UNION ALL
SELECT 'Jhony has blue pen', 1 FROM DUAL
UNION ALL
SELECT 'Ravi has white dog', 1 FROM DUAL
UNION ALL
SELECT 'Rahul has small dogs', 3 FROM DUAL
UNION ALL
SELECT 'There are pens inside Jhon''s car', 3 FROM DUAL
UNION ALL
SELECT 'My dog and me are flying.', 1 FROM DUAL)
SELECT sw.word, SUM (d.quantity) AS total_quantity
FROM search_words sw, d
WHERE INSTR (UPPER (d.name), UPPER (sw.word)) > 0
GROUP BY sw.word;
Result 2
WORD TOTAL_QUANTITY
_______ _________________
Dog 9
Pen 6

How to count consecutive duplicates in a table?

I have below question:
Want to find the consecutive duplicates
SLNO NAME PG
1 A1 NO
2 A2 YES
3 A3 NO
4 A4 YES
6 A5 YES
7 A6 YES
8 A7 YES
9 A8 YES
10 A9 YES
11 A10 NO
12 A11 YES
13 A12 NO
14 A14 NO
We will consider the value of PG column and I need the output as 6 which is the count of maximum consecutive duplicates.
It can be done with Tabibitosan method. Run this, to understand it:
with a as(
select 1 slno, 'A' pg from dual union all
select 2 slno, 'A' pg from dual union all
select 3 slno, 'B' pg from dual union all
select 4 slno, 'A' pg from dual union all
select 5 slno, 'A' pg from dual union all
select 6 slno, 'A' pg from dual
)
select slno, pg, newgrp, sum(newgrp) over (order by slno) grp
from(
select slno,
pg,
case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp
from a
);
Newgrp means a new group is found.
Result:
SLNO PG NEWGRP GRP
1 A 1 1
2 A 0 1
3 B 1 2
4 A 1 3
5 A 0 3
6 A 0 3
Now, just use a group by with count, to find the group with maximum number of occurrences:
with a as(
select 1 slno, 'A' pg from dual union all
select 2 slno, 'A' pg from dual union all
select 3 slno, 'B' pg from dual union all
select 4 slno, 'A' pg from dual union all
select 5 slno, 'A' pg from dual union all
select 6 slno, 'A' pg from dual
),
b as(
select slno, pg, newgrp, sum(newgrp) over (order by slno) grp
from(
select slno, pg, case when pg <> nvl(lag(pg) over (order by slno),1) then 1 else 0 end newgrp
from a
)
)
select max(cnt)
from (
select grp, count(*) cnt
from b
group by grp
);
with test as (
select 1 slno,'A1' name ,'NO' pg from dual union all
select 2,'A2','YES' from dual union all
select 3,'A3','NO' from dual union all
select 4,'A4','YES' from dual union all
select 6,'A5','YES' from dual union all
select 7,'A6','YES' from dual union all
select 8,'A7','YES' from dual union all
select 9,'A8','YES' from dual union all
select 10,'A9','YES' from dual union all
select 11,'A10','NO' from dual union all
select 12,'A11','YES' from dual union all
select 13,'A12','NO' from dual union all
select 14,'A14','NO' from dual),
consecutive as (select row_number() over(order by slno) rr, x.*
from test x)
select x.* from Consecutive x
left join Consecutive y on x.rr = y.rr+1 and x.pg = y.pg
where y.rr is not null
order by x.slno
And you can control output with condition in where.
where y.rr is not null query returns duplicates
where y.rr is null query returns "distinct" values.
Just for completeness, here's the actual Tabibitosan method:
with sample_data as (select 1 slno, 'A1' name, 'NO' pg from dual union all
select 2 slno, 'A2' name, 'YES' pg from dual union all
select 3 slno, 'A3' name, 'NO' pg from dual union all
select 4 slno, 'A4' name, 'YES' pg from dual union all
select 6 slno, 'A5' name, 'YES' pg from dual union all
select 7 slno, 'A6' name, 'YES' pg from dual union all
select 8 slno, 'A7' name, 'YES' pg from dual union all
select 9 slno, 'A8' name, 'YES' pg from dual union all
select 10 slno, 'A9' name, 'YES' pg from dual union all
select 11 slno, 'A10' name, 'NO' pg from dual union all
select 12 slno, 'A11' name, 'YES' pg from dual union all
select 13 slno, 'A12' name, 'NO' pg from dual union all
select 14 slno, 'A14' name, 'NO' pg from dual)
-- end of mimicking a table called "sample_data" containing your data; see SQL below:
select max(cnt) max_pg_in_queue
from (select count(*) cnt
from (select slno,
name,
pg,
row_number() over (order by slno)
- row_number() over (partition by pg
order by slno) grp
from sample_data)
where pg = 'YES'
group by grp);
MAX_PG_IN_QUEUE
---------------
6
SELECT MAX(consecutives) -- Block 1
FROM (
SELECT t1.pg, t1.slno, COUNT(*) AS consecutives -- Block 2
FROM test t1 INNER JOIN test t2 ON t1.pg = t2.pg
WHERE t1.slno <= t2.slno
AND NOT EXISTS (
SELECT * -- Block 3
FROM test t3
WHERE t3.slno > t1.slno
AND t3.slno < t2.slno
AND t3.pg != t1.pg
)
GROUP BY t1.pg, t1.slno
);
The query calculates the result in following way:
Extract all couples of records that don't have a record with different value of PG in between (blocks 2 and 3)
Group them by PG value and starting SLNO value -> this counts the consecutive values for any [PG, (starting) SLNO] couple (block 2);
Extract Maximum value from query 2 (block 1)
Note that the query may be simplified if the slno field in table contains consecutive values, but this seems not your case (in your example record with SLNO = 5 is missing)
Only requiring a single aggregation query and no joins (the rest of the calculation can be done with ROW_NUMBER, LAG and LAST_VALUE):
SELECT MAX( num_before_in_queue ) AS max_sequential_in_queue
FROM (
SELECT rn - LAST_VALUE( has_changed ) IGNORE NULL OVER ( ORDER BY ROWNUM ) + 1
AS num_before_in_queue
FROM (
SELECT pg,
ROW_NUMBER() OVER ( ORDER BY slno ) AS rn,
CASE pg WHEN LAG( pg ) OVER ( ORDER BY slno )
THEN NULL
ELSE ROW_NUMBER() OVER ( ORDER BY sl_no )
END AS change
FROM table_name
)
WHERE pg = 'Y'
);
Try to use row_number()
select
SLNO,
Name,
PG,
row_number() over (partition by PG order by PG) as 'Consecutive'
from
<table>
order by
SLNO,
NAME,
PG
This is should work with minor tweaking.
--EDIT--
Sorry, partiton by PG.
The partitioning tells the row_number when to start a new sequence.

How do I add a moving window to rank() using Oracle SQL

I need to rank values over a moving time window. I got some direction from a blogpost by Tony Hasler at
https://tonyhasler.wordpress.com/2012/10/24/model-clause-use-cases/#comment-5116 but the solution of adding a windowing clause to non-windowing functions like median does not work with the rank() or percent_rank() functions which are analytic non-windowing functions.
Working example with median() function:
with a as (
select 'a' sector, trunc(sysdate) dt, 64 v from dual union all
select 'a' sector, trunc(sysdate)-1 dt, 2 from dual union all
select 'a' sector, trunc(sysdate)-2 dt, 4 from dual union all
select 'a' sector, trunc(sysdate)-3 dt, 128 from dual union all
select 'a' sector, trunc(sysdate)-4 dt, 8 from dual union all
select 'a' sector, trunc(sysdate)-5 dt, 16 from dual union all
select 'a' sector, trunc(sysdate)-6 dt, 32 from dual union all
select 'a' sector, trunc(sysdate)-7 dt, 256 from dual union all
select 'a' sector, trunc(sysdate)-8 dt, 1 v from dual union all
select 'a' sector, trunc(sysdate)-9 dt, 512 from dual union all
select 'b' sector, trunc(sysdate) dt, 3 from dual union all
select 'b' sector, trunc(sysdate)-1 dt, 27 from dual union all
select 'b' sector, trunc(sysdate)-2 dt, 9 from dual union all
select 'b' sector, trunc(sysdate)-3 dt, 81 from dual
)
select * from a
model
partition by (sector)
dimension by (dt)
measures (v, 0 mov_rank)
rules
(
mov_rank[ANY] = median(v)[dt between CV()-3 and CV()]
)
order by sector, dt
;
The example does not work if we replace median with rank() as in:
with a as (
select 'a' sector, trunc(sysdate) dt, 64 v from dual union all
select 'a' sector, trunc(sysdate)-1 dt, 2 from dual union all
select 'a' sector, trunc(sysdate)-2 dt, 4 from dual union all
select 'a' sector, trunc(sysdate)-3 dt, 128 from dual union all
select 'a' sector, trunc(sysdate)-4 dt, 8 from dual union all
select 'a' sector, trunc(sysdate)-5 dt, 16 from dual union all
select 'a' sector, trunc(sysdate)-6 dt, 32 from dual union all
select 'a' sector, trunc(sysdate)-7 dt, 256 from dual union all
select 'a' sector, trunc(sysdate)-8 dt, 1 v from dual union all
select 'a' sector, trunc(sysdate)-9 dt, 512 from dual union all
select 'b' sector, trunc(sysdate) dt, 3 from dual union all
select 'b' sector, trunc(sysdate)-1 dt, 27 from dual union all
select 'b' sector, trunc(sysdate)-2 dt, 9 from dual union all
select 'b' sector, trunc(sysdate)-3 dt, 81 from dual
)
select * from a
model
partition by (sector)
dimension by (dt)
measures (v, 0 mov_rank)
rules
(
mov_rank[ANY] = rank() over (order by v)[dt between CV()-3 and CV()]
)
order by sector, dt
;
I would appreciate any help.
Thanks.
This might be a little "old-fashioned", but you might be able to achieve an equivalent result using a self-join instead of analytics or model, as in something like:
with a as (
select 'a' sector, trunc(sysdate) dt, 64 v from dual union all
select 'a' sector, trunc(sysdate)-1 dt, 2 from dual union all
select 'a' sector, trunc(sysdate)-2 dt, 4 from dual union all
select 'a' sector, trunc(sysdate)-3 dt, 128 from dual union all
select 'a' sector, trunc(sysdate)-4 dt, 8 from dual union all
select 'a' sector, trunc(sysdate)-5 dt, 16 from dual union all
select 'a' sector, trunc(sysdate)-6 dt, 32 from dual union all
select 'a' sector, trunc(sysdate)-7 dt, 256 from dual union all
select 'a' sector, trunc(sysdate)-8 dt, 1 v from dual union all
select 'a' sector, trunc(sysdate)-9 dt, 512 from dual union all
select 'b' sector, trunc(sysdate) dt, 3 from dual union all
select 'b' sector, trunc(sysdate)-1 dt, 27 from dual union all
select 'b' sector, trunc(sysdate)-2 dt, 9 from dual union all
select 'b' sector, trunc(sysdate)-3 dt, 81 from dual
)
select
a.sector,
a.dt,
a.v,
count(case when self.v < a.v then self.v end) + 1 mov_rank
from
a,
a self
where
self.sector = a.sector
and
self.dt between a.dt - 3 and a.dt + 3
group by
a.sector,
a.dt,
a.v
order by
a.sector,
a.dt,
a.v;

Oracle SUM returns false summary with identicals values returing from an SELECT UNION

I am facing a problem with SUM statement.
This query returns MY_ID = 1 and QTY = 7
select my_id, sum(qty) qty
from
(
select 1 my_id ,2 qty from dual
union
select 1 my_id, 5 qty from dual
)
group by my_id;
But this one returns MY_ID = 1 and QTY = 5 instead of QTY = 10.
select my_id, sum(qty) qty
from
(
select 1 my_id ,5 qty from dual
union
select 1 my_id, 5 qty from dual
)
group by my_id;
How can I summary the two quantity in case of the two values are the same?
Use union all:
select my_id, sum(qty) qty
from
(
select 1 my_id ,5 qty from dual
union all
select 1 my_id, 5 qty from dual
)
group by my_id;
Try using union all:
The below works:
select my_id, sum(qty) qty
from
(
select 1 my_id ,5 qty from dual
union all
select 1 my_id, 5 qty from dual
)
group by my_id;
this is because 5 union 5 is always 5. if you do union all it includes everything irrespective of it being the same!
In the second query, the two rows in the union are identical.
There are two forms of UNION: UNION ALL and UNION DISTINCT. Which one is the default varies, but it looks like you're getting a UNION DISTINCT, which since the two (1, 5) rows are the same is only returning one of them. Change it to:
select my_id, sum(qty) qty
from
(
select 1 my_id ,5 qty from dual
union ALL
select 1 my_id, 5 qty from dual
)
group by my_id;
That should give you what you want: (1, 10).
EDIT: Briefly I had union DISTINCT in the query which was wrong! Now corrected....