How to do grand total on level break footer total in Report Design Aid - oracle10g

I want to calculate a grand total of those total I get on level break footer group.
Example:
Item A1 - 10
Item A2 - 20
Item A3 - 30
Total = 60
Item B1 - 10
Item B2 - 20
Total = 30
Grand Total 90
What should I do?

You can use rollup. Hope that helps.
EDIT: Did not recognized that the question is related to JDEdwards so this is how it would be done in Oralce SQL.
with
items as (
select 'A' as grp, '1' as item, 10 as val from dual
union
select 'A' as grp, '2' as item, 20 as val from dual
union
select 'A' as grp, '3' as item, 30 as val from dual
union
select 'B' as grp, '1' as item, 10 as val from dual
union
select 'B' as grp, '2' as item, 20 as val from dual
)
select case
when grouping(grp) = 1 then 'Grand Total ' || grp || sum(val)
when grouping(item) = 1 then 'Total ' || grp || ' = ' || sum(val)
else 'Item ' || grp || item || ' - ' || max(val)
end
from items
group by rollup (grp, item)

You should set which fields of your Business View you will sequence. For instance, Branch/Plant(MCU) / Date Transaction(TRDJ) / Item (LITM)
Define which of these fields you want a Level Break.
Then create a Level Break Footer Group Section based on the field you defined on the previous step and add an aggregation function to sum the amount fields you want.

I assume you have more than 2 sort columns.
Let us assume them to be MCU(Business Unit),DOCO(Order No),LITM(Item Number)
You will get your subtotal by putting a level-break footer on DOCO and using the aggregate function (sumof) on BC LITM (The item needs to be displayed in the parent section)
To get Grand Total, you can do 2 things:
a: Make a global variable and store the subtotal in it and keep adding it to itself. (Display it at the end as Grand Total)
b: Add a level-break footer (on MCU) inside the existing level-break footer and perform sumof on BC LITM
I hope this solves your problem :)

Related

GROUP BY - How to create 3 group for the column?

Say I have a table of products, fields are id, number_of_product, price
Let's price is min = 100, max = 1000*
How to create 3 groups for this column (PostgreSQL) - 100-400, 400-600, 600-1000*
*PS - it would be nice to know how to split into 3 equal parts.
SELECT COUNT(id),
COUNT(number_of_product),
!!!! price - ?!
FROM Scheme.Table
GROUP BY PRICE
You can try next query:
with p as (
select
*,
min(price) over() min_price,
(max(price) over() - min(price) over()) / 3 step
from products
) select
id, product, price,
case
when price < min_price + step then 'low_price'
when price < min_price + 2 * step then 'mid_price'
else 'high'
end as category
from p
order by price;
PostgreSQL fiddle
To do this quickly, you can use a case statement to set the groups.
CASE WHEN price BETWEEN 100 AND 400 THEN 1 WHEN price BETWEEN 400 AND 600 THEN 2 WHEN price BETWEEN 600 AND 1000 THEN 3 ELSE 0 END
You would group on this.
For splitting into equal parts, you would use the NTILE window function to group.
NTILE(3) OVER (
ORDER BY price]
)

Select statement based on category

I have a table like this
Code
A123
B3123
C93485
D345
E29845
The first letter of rows in code column are classified into the following:
Char Category
A-B A
C B
D-E C
I would like to display the output table like this
Category Total Percentage
A 2 0.4%
B 1 0.2%
C 2 0.4%
Total 5 1.0%
I'm not sure how to start. Any hints or help is much appreciated
Here is one option:
SELECT
CASE WHEN SUBSTR(Code, 1, 1) IN ('A', 'B') THEN 'A'
WHEN SUBSTR(Code, 1, 1) = 'C' THEN 'B'
ELSE 'C' END AS Category,
COUNT(*) AS Total,
200.0 * COUNT(*) / SUM(COUNT(*)) OVER () AS Percentage
FROM yourTable
GROUP BY
ROLLUP(CASE WHEN SUBSTR(Code, 1, 1) IN ('A', 'B') THEN 'A'
WHEN SUBSTR(Code, 1, 1) = 'C' THEN 'B'
ELSE 'C' END);
Demo
This approach uses a CASE expression on the first letter of each code to assign a category. Then, we aggregate by category and find the totals, as well as the percentages. Note that ROLLUP is used to generate a total record at the bottom of the result set. As a side effect of this, we multiply by 200%, because the summary row already contains the entire table count, which then gets counted twice.

Put remainder of division into next line

Let's say I have the following data that represents taxes:
SELECT trunc(i*i, 3) tax
FROM generate_series(1.17, 5) i;
tax
--------
1.368
4.708
10.048
17.388
(4 rows)
Is there any nice way in PostgreSQL to put mill remainder into next line and if current line is the last it must have all leftovers.
So, I need to make it the following:
tax
--------
1.360
4.710
10.050
17.392
(4 rows)
It could be a query or SQL / PL/pgSQL function.
Next row and last row make sense only when the sort order is defined. I assume that the sort order is defined by tax asc.
The first subquery adds row numbers to the data, while the second one calculates the number of rows. The next part is a recursion based on increasing row numbers:
with recursive data as (
select trunc(i*i, 3) tax, row_number() over (order by i) as rn
from generate_series(1.17, 5) i
),
count as (
select count(*)
from data
),
result as (
select
tax, rn,
floor(tax* 100)/100 as new_tax,
tax- floor(tax* 100)/100 as remainder
from data
where rn = 1
union all
select
d.tax, d.rn,
case d.rn
when count then d.tax+ r.remainder
else floor((d.tax+ r.remainder)* 100)/100 end as new_tax,
d.tax+ r.remainder- floor((d.tax+ r.remainder)* 100)/100 as remainder
from data d
join result r on d.rn = r.rn+ 1
cross join count
)
select new_tax as tax
from result
order by rn;
Live demo in rextester.

Sql Server - Running Totals Based on Conditions

I have been banging my head trying to come up with the correct logic (SQL Server 2012) needed to achieve something I would imagine would be fairly routine but I have been unable to find any examples of this anywhere. Basically, I have 3 columns in a table: product, flag, value. It is possible for a product to be listed multiple times within the table but only once with a unique flag (i.e. product1 can have flag1 or flag2 with different/identical but there will never be 2 records with product1 and flag1 and different/identical values).
The flag represents a pre-defined value (1,2,3,4) and the intention behind this field is to be able to assign a unique mathematical equation based on the value of the flag. The end result would yield a single product, the unique flag, and a new cumulative total based on the mathematical equation output. For instance, let's say product1 was listed 4 times with flag values of flag1, flag2, flag3, flag4 (see below):
Product-----Flag-----Value
Product1----Flag1----1.00
Product1----Flag2----3.00
Product1----Flag3----5.00
Product1----Flag4----7.00
Product-----Flag-----Value
Product1----Flag1----1.00 (flag1 value)
Product1----Flag2----4.00 (flag1+flag2 value)
Product1----Flag3----6.00 (flag1+flag3 value)
Product1----Flag4----10.00 (flag2+flag4 value)
Flag1 is defined as add flag1 only. Flag2 is defined as add flag1 and flag2. Flag 3 is defined as add flag1 and flag 3. Flag 4 is defined as add flag2 and flag4. the new output would be product1 listed four times with flag values of flag1, flag2, flag3, flag4 but new values as flag1, flag1_flag2, flag1+flag3, flag2+flag4.
I have tried to apply the logic via a case statement but I can't figure out how to traverse all the products for each condition and I have tried to go with a running totals solution but I am not sure how to incorporate the flag condition into it so it only performs a running total for when those conditions are true. Any assistance and/or article to help get me going down the right path would be greatly appreciated.
While I'm not sure I fully understand your question I think this might be what you want. For this to work it assumes flag1 is always present when flags 1 through 3 are and that flag2 is present when flag4 is.
;with cte as (
select
product,
max(case when flag = 'Flag1' then Value end) as f1Value,
max(case when flag = 'Flag2' then Value end) as f2Value,
max(case when flag = 'Flag3' then Value end) as f3Value,
max(case when flag = 'Flag4' then Value end) as f4Value
from flags group by Product
)
select
flags.Product,
flags.Flag,
flags.Value as "Org. value",
case flag
when 'Flag1' then f1Value
when 'Flag2' then f1Value + f2Value
when 'Flag3' then f1Value + f3Value
when 'Flag4' then f2Value + f4Value
else flags.Value -- take the present value when flag is not Flag1-4
end as "New value"
from flags
inner join cte on flags.Product = cte.Product
Take a look at this Sample SQL Fiddle to see it in action.
You can join a table to itself, and pick the conditions appropriately:
SELECT p1.product,p1.Flag,p1.Value + COALESCE(p2.Value,0)
FROM
Products p1
left join
Products p2
on
p1.Product = p2.Product and
p2.Flag = CASE p1.Flag
--1 doesn't need a previous value
WHEN 2 THEN 1
WHEN 3 THEN 1
WHEN 4 THEN 2
END
I assumed and tried on Range values.
CREATE TABLE #tmp (Product VARCHAR(10), flag VARCHAR(10),value numeric(13,2))
GO
INSERT INTO #tmp
SELECT 'Product1' , 'Flag1',1
UNION
SELECT 'Product1' , 'Flag2',3
UNION
SELECT 'Product1' , 'Flag3',5
UNION
SELECT 'Product1' , 'Flag4',7
GO
;WITH cte
AS
(
SELECT row_number () OVER(
ORDER BY flag) 'row',*
FROM #tmp
)
SELECT *,value 'RT'
FROM cte
WHERE row = 1
UNION
SELECT * ,(
SELECT cte.value
FROM cte
WHERE row = 1
) + value 'RT'
FROM cte
WHERE row BETWEEN 2
AND 3
UNION
SELECT * ,(
SELECT cte.value
FROM cte
WHERE row =2
) + value 'RT'
FROM cte
WHERE row >3
GO
DROP TABLE #tmp

Summing From Consecutive Rows

Assume we have a table and we want to do a sum of the Expend column so that the summation only adds up values of the same Week_Name.
SN Week_Name Exp Sum
-- --------- --- ---
1 Week 1 10 0
2 Week 1 20 0
3 Week 1 30 60
4 Week 2 40 0
5 Week 2 50 90
6 Week 3 10 0
I will assume we will need to `Order By' Week_Name, then compare the previous Week_Name(previous row) with the current row Week_name(Current row).
If both are the same, put zero in the SUM column.
If not the same, add all expenditure, where Week_Name = Week_Name(Previous row) and place in the Sum column. The final output should look like the table above.
Any help on how to achieve this in T-SQL is highly appreciated.
Okay, I was eventually able to resolve this issue, praise Jesus! If you want the exact table I gave above, you can use GilM's response below, it is perfect. If you want your table to have running Cumulatives, i.e. Rows 3 shoud have 60, Row 5, should have 150, Row 6 160 etc. Then, you can use my code below:
USE CAPdb
IF OBJECT_ID ('dbo.[tablebp]') IS NOT NULL
DROP TABLE [tablebp]
GO
CREATE TABLE [tablebp] (
tablebpcCol1 int PRIMARY KEY
,tabledatekey datetime
,tableweekname varchar(50)
,expenditure1 numeric
,expenditure_Cummulative numeric
)
INSERT INTO [tablebp](tablebpcCol1,tabledatekey,tableweekname,expenditure1,expenditure_Cummulative)
SELECT b.s_tablekey,d.PK_Date,d.Week_Name,
SUM(b.s_expenditure1) AS s_expenditure1,
SUM(b.s_expenditure1) + COALESCE((SELECT SUM(s_expenditure1)
FROM source_table bs JOIN dbo.Time dd ON bs.[DATE Key] = dd.[PK_Date]
WHERE dd.PK_Date < d.PK_Date),0)
FROM source_table b
INNER JOIN dbo.Time d ON b.[Date key] = d.PK_Date
GROUP BY d.[PK_Date],d.Week_Name,b.s_tablekey,b.s_expenditure1
ORDER BY d.[PK_Date]
;WITH CTE AS (
SELECT tableweekname
,Max(expenditure_Cummulative) AS Week_expenditure_Cummulative
,MAX(tablebpcCol1) AS MaxSN
FROM [tablebp]
GROUP BY tableweekname
)
SELECT [tablebp].*
,CASE WHEN [tablebp].tablebpcCol1 = CTE.MaxSN THEN Week_expenditure_Cummulative
ELSE 0 END AS [RunWeeklySum]
FROM [tablebp]
JOIN CTE on CTE.tableweekname = [tablebp].tableweekname
I'm not sure why your SN=6 line is 0 rather than 10. Do you really not want the sum for the last Week? If having the last week total is okay, then you might want something like:
;WITH CTE AS (
SELECT Week_Name,SUM([Expend.]) as SumExpend
,MAX(SN) AS MaxSN
FROM T
GROUP BY Week_Name
)
SELECT T.*,CASE WHEN T.SN = CTE.MaxSN THEN SumExpend
ELSE 0 END AS [Sum]
FROM T
JOIN CTE on CTE.Week_Name = T.Week_Name
Based on the requst in the comment wanting a running total in SUM you could try this:
;WITH CTE AS (
SELECT Week_Name, MAX(SN) AS MaxSN
FROM T
GROUP BY Week_Name
)
SELECT T.SN, T.Week_Name,T.Exp,
CASE WHEN T.SN = CTE.MaxSN THEN
(SELECT SUM(EXP) FROM T T2
WHERE T2.SN <= T.SN) ELSE 0 END AS [SUM]
FROM T
JOIN CTE ON CTE.Week_Name = T.Week_Name
ORDER BY SN