oracle grouping everytime the sum amount is below 15 - sql

I've following data:
SELECT 1 note, 1000 amt FROM dual union all
SELECT 2 note, 2000 amt FROM dual union all
SELECT 3 note, 8000 amt FROM dual union all
SELECT 4 note, 3000 amt FROM dual union all
SELECT 5 note, 1500 amt FROM dual union all
SELECT 6 note, 1600 amt FROM dual union all
SELECT 7 note, 20000 amt FROM dual union all
SELECT 8 note, 20000 amt FROM dual union all
SELECT 9 note, 2100 amt FROM dual union all
SELECT 10 note, 4500 amt FROM dual union all
SELECT 11 note, 1000 amt FROM dual union all
SELECT 12 note, 16000 amt FROM dual
and I need sum the amount, but for every sum <= 15000, they will be grouped together. If the amount is > 15000, they will be on their own group like this:
NOTE
AMT
group
1
1000
1
11
1000
1
5
1500
1
6
1600
1
2
2000
1
9
2100
1
4
3000
1
10
4500
2
3
8000
2
12
16000
3
7
20000
4
8
20000
5
I need the solution in oracle sql, is it possible? I'm using oracle 11g

One method is a recursive subquery:
with tt(note, amt, seqnum) as (
select t.note, t.amt, row_number() over (order by amt) as seqnum
from t
),
cte(note, amt, seqnum, grp, running_amt) as (
select note, amt, seqnum, 1, amt
from tt
where seqnum = 1
union all
select tt.note, tt.amt, tt.seqnum,
(case when tt.amt + cte.running_amt > 15000 then cte.grp + 1 else cte.grp end),
(case when tt.amt + cte.running_amt > 15000 then tt.amt else tt.amt + cte.running_amt end)
from cte join
tt
on tt.seqnum = cte.seqnum + 1
)
select *
from cte;
Here is a db<>fiddle.
Note: This is ordering by amt -- as in your sample data. You can as easily order by note (which also makes sense) just by adjusting seqnum in tt.

You can use the SQL for Pattern Matching:
WITH t AS (
SELECT 1 note, 1000 amt FROM DUAL UNION ALL
SELECT 2 note, 2000 amt FROM DUAL UNION ALL
SELECT 3 note, 8000 amt FROM DUAL UNION ALL
SELECT 4 note, 3000 amt FROM DUAL UNION ALL
SELECT 5 note, 1500 amt FROM DUAL UNION ALL
SELECT 6 note, 1600 amt FROM DUAL UNION ALL
SELECT 7 note, 20000 amt FROM DUAL UNION ALL
SELECT 8 note, 20000 amt FROM DUAL UNION ALL
SELECT 9 note, 2100 amt FROM DUAL UNION ALL
SELECT 10 note, 4500 amt FROM DUAL UNION ALL
SELECT 11 note, 1000 amt FROM DUAL UNION ALL
SELECT 12 note, 16000 amt FROM DUAL)
SELECT note, amt, amt_group, sum_amt
FROM t
MATCH_RECOGNIZE (
ORDER BY amt
MEASURES
MATCH_NUMBER() AS amt_group,
NVL(SUM(amt), amt) AS sum_amt
ALL ROWS PER MATCH
PATTERN (s*)
DEFINE
s AS SUM(amt) <= 15000
) mr
ORDER BY amt
NOTE
AMT
AMT_GROUP
SUM_AMT
1
1000
1
1000
11
1000
1
2000
5
1500
1
3500
6
1600
1
5100
2
2000
1
7100
9
2100
1
9200
4
3000
1
12200
10
4500
2
4500
3
8000
2
12500
12
16000
3
16000
7
20000
4
20000
8
20000
5
20000

Related

Oracle SQL Query to get the RAG

RAG PCT
------ ---
GREEN 100
AMBER 50
ORANGE 20
RED 0
I need an oracle query to result like (Suggest to use Inner Join or Outer Join)
if the given PCT >100 then Green
if the given PCT >=50 and PCT < 100 then AMBER
if the given PCT >=20 and PCT < 0 then ORANGE
else RED
You can implement the logic using CASE, like :
SELECT pct, CASE
WHEN pct >= 100 THEN 'GREEN'
WHEN pct >= 50 THEN 'AMBER'
WHEN pct >= 20 then 'ORANGE'
ELSE 'RED'
END
FROM mytable
CASE stops on the first matching condition (hence no need to write WHEN pct >= 50 AND pct < 100 for example, since pct >= 100 is already caught by the previous condition.
If you are using a separate table to store the lower bound of each interval (like
myranges), as shown in your example, and you are looking to JOIN it with a table that cotains actual data (like mydata), then it is a little more tricky : you would need to ensure that you are joining with the relevant range record :
SELECT d.*, r.*
FROM mydata d
INNER JOIN myranges r
ON d.value >= r.pct
AND (
LEAD (r.pct) OVER (ORDER BY pct) IS NULL
OR d.value < LEAD (r.pct) OVER (ORDER BY pct)
)
What you're asking for doesn't entirely make sense - you've said "Use a join" but have provided nothing to join to, but never mind. The following strictly implements your spec:
WITH cteData AS (SELECT 'GREEN' AS RAG, 100 AS PCT FROM DUAL UNION ALL
SELECT 'AMBER', 50 FROM DUAL UNION ALL
SELECT 'ORANGE', 20 FROM DUAL UNION ALL
SELECT 'RED', 0 FROM DUAL)
SELECT RAG, PCT, CASE
WHEN PCT > 100 THEN 'GREEN'
WHEN PCT >= 50 AND PCT < 100 THEN 'AMBER'
WHEN PCT >= 20 AND PCT < 0 THEN 'ORANGE'
ELSE 'RED'
END AS COLOR
FROM cteData;
When executed, the above produces:
RAG PCT COLOR
GREEN 100 RED
AMBER 50 AMBER
ORANGE 20 RED
RED 0 RED
And may Codd have mercy upon your soul.
If I understand you correctly, I think this might be something like what you're after:
WITH rag_data AS (SELECT 'GREEN' AS rag, 100 AS PCT FROM DUAL UNION ALL
SELECT 'AMBER' AS rag, 50 AS PCT FROM DUAL UNION ALL
SELECT 'ORANGE' AS rag, 20 AS PCT FROM DUAL UNION ALL
SELECT 'RED' AS rag, 0 AS PCT FROM DUAL),
sample_data AS (SELECT -1 NUM FROM dual UNION ALL
SELECT 0 NUM FROM dual UNION ALL
SELECT 1 NUM FROM dual UNION ALL
SELECT 19 NUM FROM dual UNION ALL
SELECT 20 NUM FROM dual UNION ALL
SELECT 21 NUM FROM dual UNION ALL
SELECT 49 NUM FROM dual UNION ALL
SELECT 50 NUM FROM dual UNION ALL
SELECT 51 NUM FROM dual UNION ALL
SELECT 99 NUM FROM dual UNION ALL
SELECT 100 NUM FROM dual UNION ALL
SELECT 101 NUM FROM dual)
SELECT NUM,
rag,
pct,
rn
FROM (SELECT sd.num,
rd.rag,
rd.pct,
row_number() OVER (PARTITION BY sd.num ORDER BY rd.pct DESC) rn
FROM sample_data sd
INNER JOIN rag_data rd ON sd.num >= rd.pct)
WHERE rn = 1;
NUM RAG PCT RN
---------- ------ ---------- ----------
0 RED 0 1
1 RED 0 1
19 RED 0 1
20 ORANGE 20 1
21 ORANGE 20 1
49 ORANGE 20 1
50 AMBER 50 1
51 AMBER 50 1
99 AMBER 50 1
100 GREEN 100 1
101 GREEN 100 1

Analytic Function: ROW_NUMBER( )

I have a table "Invoice"
id integer Primary key
customer_id Integer
total Number (*,2)
The query is to display all customer_id, total and running serial number to each customer with alias name as 'SNO'. And the records should be displayed in ascending order based on the customer_id and then by SNO.
Hints:
Analytic Function: ROW_NUMBER( )
Analytic Clause: query_partition_clause and order_by_clause.
I wrote the below query:
Select customer_id,
total,
ROW_NUMBER( ) OVER (PARTITION BY customer_id ORDER BY customer_id ASC) AS "SNO"
from invoice;
But the result is failing. What is that I am missing. Also what is meant "the records should be displayed in ascending order based on the customer_id and then by SNO".
The result I am getting is as below:
CUSTOMER_ID TOTAL SNO
1 70000 1
2 250000 1
2 560000 2
3 200000 1
3 45000 2
4 475000 1
5 50000 1
5 10000 2
6 600000 1
6 90000 2
Expected result is :
CUSTOMER_ID TOTAL SNO
1 70000 1
2 250000 1
2 560000 2
3 45000 1
3 200000 2
4 475000 1
5 10000 1
5 50000 2
6 600000 1
6 90000 2
TOTAL Column data is not matching.
You're close, you probably need to order the row_number by id (assuming it's ascending based on time)
Select customer_id,
total,
ROW_NUMBER( ) OVER (PARTITION BY customer_id ORDER BY id ASC) AS "SNO"
from invoice
order by customer_id, "SNO" -- should be the default anyway (but there's no guarantee)
I have not found any order by clause in your query, another issue in which order you want to generate SNO ? by using id or total that will impact on your ordering
with cte as
(
select 1 cid, 70000 total from dual
union all
select 2, 250000 from dual
union all
select 2, 560000 from dual
union all
select 3, 200000 from dual
union all
select 3, 45000 from dual
union all
select 4, 475000 from dual
union all
select 5, 50000 from dual
union all
select 5, 10000 from dual
union all
select 6, 600000 from dual
union all
select 6, 90000 from dual
)Select cid,total,ROW_NUMBER( ) OVER (PARTITION BY cid ORDER BY total ) AS "SNO" from cte order by cid,SNO

SQL oracle group list number

Please help me: group list number
A new group starts when the values descend. You can find the groups where they start using lag(). Then do a cumulative sum:
select t.*,
1 + sum(case when prev_col2 < col2 then 0 else 1 end) over (order by col1) as grp
from (select t.*,
lag(col2) over (order by col1) as prev_col2
from t
) t;
In Oracle 12.1 and above, this is a simple application of the match_recognize clause:
with
inputs ( column1, column2 ) as (
select 1, 1000 from dual union all
select 2, 2000 from dual union all
select 3, 3000 from dual union all
select 4, 6000 from dual union all
select 5, 7500 from dual union all
select 6, 0 from dual union all
select 7, 500 from dual union all
select 8, 600 from dual union all
select 9, 900 from dual union all
select 10, 2300 from dual union all
select 11, 4700 from dual union all
select 12, 40 from dual union all
select 13, 1000 from dual union all
select 14, 2000 from dual union all
select 15, 4000 from dual
)
-- End of simulated inputs (not part of the solution).
-- SQL query begins BELOW THIS LINE. Use actual table and column names.
select column1, column2, column3
from inputs
match_recognize(
order by column1
measures match_number() as column3
all rows per match
pattern ( a b* )
define b as column2 >= prev(column2)
)
order by column1 -- If needed.
;
OUTPUT:
COLUMN1 COLUMN2 COLUMN3
---------- ---------- ----------
1 1000 1
2 2000 1
3 3000 1
4 6000 1
5 7500 1
6 0 2
7 500 2
8 600 2
9 900 2
10 2300 2
11 4700 2
12 40 3
13 1000 3
14 2000 3
15 4000 3
You can use window function to mark the point where column_2 restarts and use cumulative sum to get the desired result
Select column_1,
Column_2,
Sum(flag) over (order by column_1) as column_3
From (
Select t.*,
Case when column_2 < lag(column_2,1,0) over (order by column_1) then 1 else 0 end as flag
From your_table t
) t;

How to sum two different fields from two tables with one field is common

I have two tables Sales and Charges.
Tables having data as:
'Sales' 'Charges'
SID F_AMT SID C_AMT
1 100 1 10
1 100 1 10
1 100 1 20
1 200 2 20
2 200 2 10
2 300 3 20
4 300 3 30
4 300 3 10
4 300 5 20
4 200 5 10
I want the output as below:
SID Total_Fees Total_charges
1 500 40
2 500 30
3 0 60
4 1100 0
5 0 30
Assuming you want to do it for the whole tables this is the simplest approach:
Select Sid
, Sum(f_amt) as total_fees
, Sum(c_amt) as total_charges
From ( select sid, f_amt, 0 as c_amt
From sales
Union all
select sid, 0 as f_amt, c_amt
From charges
)
Group by sid
Use full join and nvl():
select sid, nvl(sum(f_amt), 0) fees, nvl(sum(c_amt), 0) charges
from sales s
full join charges c using (sid)
group by sid
order by sid
Demo:
with sales(sid, f_amt) as (
select 1, 100 from dual union all select 1, 100 from dual union all
select 1, 100 from dual union all select 1, 200 from dual union all
select 2, 200 from dual union all select 2, 300 from dual union all
select 4, 300 from dual union all select 4, 300 from dual union all
select 4, 300 from dual union all select 4, 200 from dual ),
charges (sid, c_amt) as (
select 1, 10 from dual union all select 1, 10 from dual union all
select 1, 20 from dual union all select 2, 20 from dual union all
select 2, 10 from dual union all select 3, 20 from dual union all
select 3, 30 from dual union all select 3, 10 from dual union all
select 5, 20 from dual union all select 5, 10 from dual )
select sid, nvl(sum(f_amt), 0) fees, nvl(sum(c_amt), 0) charges
from sales s
full join charges c using (sid)
group by sid
order by sid
Output:
SID FEES CHARGES
------ ---------- ----------
1 1500 160
2 1000 60
3 0 60
4 1100 0
5 0 30
You could use conditional aggregation:
SELECT SID,
COALESCE(SUM(CASE WHEN t=1 THEN AMT END),0) AS Total_Fees,
COALESCE(SUM(CASE WHEN t=2 THEN AMT END),0) AS Total_Charges
FROM (SELECT SID, F_AMT AS AMT, 1 AS t
FROM Sales
UNION ALL
SELECT SID, C_AMT AS AMT, 2 AS t
FROM Charges) sub
GROUP BY SID
ORDER BY SID;
DB Fiddle Demo

Oracle SQL query previous row result

I am just stuck now, now getting the logic to solve this query. Find the below tables and the output. There is Table A and table B which matches two column ID and DATE. If Date got matched then it should multiply the qty with percent else it should pick previous percent.
Table A Table B
ID Date Percent ID Date Qty
A 01/01/17 0.5 A 01/01/17 10
A 04/01/17 1 A 02/01/17 20
A 06/01/17 2 A 03/01/17 30
B 02/01/17 5 A 05/01/17 40
B 05/01/17 10 A 06/01/17 50
A 07/01/17 60
A 08/01/17 40
B 01/01/17 10
B 02/01/17 50
============================================
column column column comment comment comment
ID Date Qty Previous percent if row not matched
A 01/01/17 0.5 * 10 0.5 got new percent
A 02/01/17 0.5 * 20 0.5
A 03/01/17 0.5 * 30 0.5
A 04/01/17 1* 0 1 got new percent but no qty found
A 05/01/17 1 * 40 1
A 06/01/17 2 * 50 2 got new percent
B 01/01/17 10 * 0 0 no percent found
B 02/02/17 5 * 10 5 got new percent
B 5/1/17 10 * 0 10 got new percent
I interpreted the problem to mean "add a few more columns to Table B" - to show the "most current" percentage, showing its date from Table A, and the "gross quantity" from Table B and the "net quantity" by multiplying by the proper percentage.
If you also need one row for every row in Table A, simply delete the WHERE clause from the outermost query (towards the bottom of the query).
with table_a ( id, dt, pct ) as (
select 'A', to_date('01/01/17', 'mm/dd/rr'), 0.5 from dual union all
select 'A', to_date('04/01/17', 'mm/dd/rr'), 1 from dual union all
select 'A', to_date('06/01/17', 'mm/dd/rr'), 2 from dual union all
select 'B', to_date('02/01/17', 'mm/dd/rr'), 5 from dual union all
select 'B', to_date('05/01/17', 'mm/dd/rr'), 10 from dual
), table_b ( id, dt, qty ) as (
select 'A', to_date('01/01/17', 'mm/dd/rr'), 10 from dual union all
select 'A', to_date('02/01/17', 'mm/dd/rr'), 20 from dual union all
select 'A', to_date('03/01/17', 'mm/dd/rr'), 30 from dual union all
select 'A', to_date('05/01/17', 'mm/dd/rr'), 40 from dual union all
select 'A', to_date('06/01/17', 'mm/dd/rr'), 50 from dual union all
select 'A', to_date('07/01/17', 'mm/dd/rr'), 60 from dual union all
select 'A', to_date('08/01/17', 'mm/dd/rr'), 40 from dual union all
select 'B', to_date('01/01/17', 'mm/dd/rr'), 10 from dual union all
select 'B', to_date('02/01/17', 'mm/dd/rr'), 50 from dual
)
-- end of test data (not part of the SQL query); query begins BELOW THIS LINE
select id, qty_date, pct_date, qty as gross_qty, pct, qty * pct as net_qty
from ( select id, dt as qty_date,
last_value(case flag when 0 then dt end ignore nulls)
over (partition by id order by dt) as pct_date,
last_value(pct ignore nulls)
over (partition by id order by dt, flag) as pct,
qty, flag
from ( select id, dt, pct, null as qty, 0 as flag
from table_a
union all
select id, dt, null as pct, qty, 1 as flag
from table_b
)
)
where flag = 1
order by id, qty_date -- if needed
;
Output:
ID QTY_DATE PCT_DATE GROSS_QTY PCT NET_QTY
-- ---------- ---------- ---------- ---------- ----------
A 2017-01-01 2017-01-01 10 .5 5
A 2017-02-01 2017-01-01 20 .5 10
A 2017-03-01 2017-01-01 30 .5 15
A 2017-05-01 2017-04-01 40 1 40
A 2017-06-01 2017-06-01 50 2 100
A 2017-07-01 2017-06-01 60 2 120
A 2017-08-01 2017-06-01 40 2 80
B 2017-01-01 10
B 2017-02-01 2017-02-01 50 5 250
9 rows selected.