Table:
PARENT_ID ID YR_MONTH REWARD
1 1 11 201601 3
2 1 11 201605 9
3 1 11 201609 12
4 1 12 201601 6
5 1 12 201605 9
6 1 12 201609 9
7 2 21 201601 15
8 2 21 201605 9
9 2 21 201609 12
10 2 22 201601 9
10 2 22 201605 9
10 2 23 201609 9
I need to create a new column based on the reward column. Where Reward is 9 put 1 else 0 based on the condition.
Expected results
output table -
PARENT_ID ID YR_MONTH REWARD REWARD_STATUS
1 1 11 201601 3 0
2 1 11 201605 9 1
3 1 11 201609 12 0
4 1 12 201601 6 0
5 1 12 201605 9 1
6 1 12 201609 9 0
7 2 21 201601 15 0
8 2 21 201605 9 0
9 2 21 201609 12 0
10 2 22 201601 9 1
10 2 22 201605 9 0
10 2 23 201609 9 1
for particular parent_id, id check if there is no higher reward than 9 in any of previous yr_month if yes then 0 else 1.
I am interpreting this as "if the previous year_month is higher than 9 then 1 else 0."
I think you want lag():
select t.*,
(case when lag(reward) over (partition by parent_id order by year_month) > 9
then 1 else 0
end) as reward_status
from t;
with t (PARENT_ID, ID, YR_MONTH, REWARD) as (
select 1, 11, 201601, 3 from dual union all
select 1, 11, 201605, 9 from dual union all
select 1, 11, 201609, 12 from dual union all
select 1, 12, 201601, 6 from dual union all
select 1, 12, 201605, 9 from dual union all
select 1, 12, 201609, 9 from dual union all
select 2, 21, 201601, 15 from dual union all
select 2, 21, 201605, 9 from dual union all
select 2, 21, 201609, 12 from dual union all
select 2, 22, 201601, 9 from dual union all
select 2, 22, 201605, 9 from dual union all
select 2, 23, 201609, 9 from dual
)
select t1.*, case when reward = 9 and grp = 1 then 1 else 0 end reward_status from (
select t.*, sum(case when reward < 9 then 0 else 1 end) over (partition by parent_id, id order by yr_month) grp
from t
) t1
order by parent_id, id, yr_month;
First, group the rows based on your condition. Here, I have used the SUM() analytic function for that purpose. Then in the outer query, simply match if 9 is the first to occur in a group.
Related
SOURCE
Rowid_object
Rowid_object_matched
1
2
1
3
3
2
2
4
4
6
6
5
7
8
9
8
Target
Rowid_object
Rowid_object_matched
1
1
2
1
3
1
4
1
5
1
6
1
7
7
8
7
9
7
Here, we have Source like data and we want Target like result.
There are two groups in source which are in transitive match.
Need to identify these kind of record.
In Oracle, you can use:
SELECT rowid_object,
MIN(root) AS rowid_object_matched
FROM (
SELECT CONNECT_BY_ROOT(rowid_object) AS root,
rowid_object,
rowid_object_matched
FROM source
CONNECT BY NOCYCLE
PRIOR rowid_object IN (rowid_object, rowid_object_matched)
OR PRIOR rowid_object_matched IN (rowid_object, rowid_object_matched)
)
UNPIVOT (
rowid_object FOR key IN (rowid_object, rowid_object_matched)
)
GROUP BY rowid_object;
Which, for the sample data:
CREATE TABLE SOURCE (Rowid_object, Rowid_object_matched) AS
SELECT 1, 2 FROM DUAL UNION ALL
SELECT 1, 3 FROM DUAL UNION ALL
SELECT 3, 2 FROM DUAL UNION ALL
SELECT 2, 4 FROM DUAL UNION ALL
SELECT 4, 6 FROM DUAL UNION ALL
SELECT 6, 5 FROM DUAL UNION ALL
SELECT 7, 8 FROM DUAL UNION ALL
SELECT 9, 8 FROM DUAL;
Outputs:
ROWID_OBJECT
ROWID_OBJECT_MATCHED
1
1
2
1
3
1
4
1
6
1
5
1
7
7
8
7
9
7
fiddle
Table TRANSACTION:
TRANS_VALUE
USER ID
TRANS_TYPE_ID
10
1
2
5
2
1
15
1
1
20
2
2
10
1
2
5
1
2
15
3
1
20
3
1
I need to get to this:
USER
SUM(TRANS_TYPE_1)
SUM(TRANS_TYPE_2)
1
15
25
2
5
20
3
35
NULL
Can someone help me?
I tried this but sadness
SELECT
user_id AS "USER
SUM(trans_value)
FROM
TRANSACTION
WHERE
trans_value = 1
GROUP BY
user_id
ORDER BY 1;
I need to get to this
USER
SUM(TRANS_TYPE_1)
SUM(TRANS_TYPE_2)
1
15
25
2
5
20
3
35
NULL
Use conditional aggregation:
SELECT user_id,
SUM(CASE trans_type_id WHEN 1 THEN trans_value END) AS sum_trans_type_1,
SUM(CASE trans_type_id WHEN 2 THEN trans_value END) AS sum_trans_type_2
FROM transaction
GROUP BY user_id
or PIVOT:
SELECT *
FROM transaction
PIVOT (
SUM(trans_value)
FOR trans_type_id IN (
1 AS sum_trans_type_1,
2 AS sum_trans_type_2
)
)
Which, for the sample data:
CREATE TABLE transaction (TRANS_VALUE, USER_ID, TRANS_TYPE_ID) AS
SELECT 10, 1, 2 FROM DUAL UNION ALL
SELECT 5, 2, 1 FROM DUAL UNION ALL
SELECT 15, 1, 1 FROM DUAL UNION ALL
SELECT 20, 2, 2 FROM DUAL UNION ALL
SELECT 10, 1, 2 FROM DUAL UNION ALL
SELECT 5, 1, 2 FROM DUAL UNION ALL
SELECT 15, 3, 1 FROM DUAL UNION ALL
SELECT 20, 3, 1 FROM DUAL;
Both output:
USER_ID
SUM_TRANS_TYPE_1
SUM_TRANS_TYPE_2
1
15
25
2
5
20
3
35
null
fiddle
I have a table with multiple pos and I need to find the Purchase Id where it has only wallet per group and nothing else in the group.
For eg,here PID - 4 and 5 has only wallet , rest has other's as well. So wallet_flag should be 1 in the output.
I tried to use window's function but could not achieve the result. Can you please suggest.
select PID
,POS
, SUM(CASE WHEN POS='bwallet' THEN 1 ELSE 0 END ) OVER(PARTITION BY PID) as FLAG
from PAYMENTS
where "status" = 'SUCCESS'
OUTPUT:
Here's one option:
Sample data:
SQL> with test (pid, pos, amount) as
2 (select 1, 'wallet', 10 from dual union all
3 select 1, 'BT' , 10 from dual union all
4 select 1, 'Cash' , 10 from dual union all
5 select 2, 'BT' , 50 from dual union all
6 select 3, 'Cash' , 24 from dual union all
7 select 3, 'BT' , 12 from dual union all
8 select 4, 'wallet', 100 from dual union all
9 select 5, 'wallet', 20 from dual union all
10 select 5, 'wallet', 100 from dual
11 ),
Query begins here; cnt will be 0 if there's only "wallet" per PID:
12 temp as
13 (select pid,
14 sum(case when pos = 'wallet' then 0 else 1 end) cnt
15 from test
16 group by pid
17 )
18 select a.pid, a.pos, a.amount,
19 case when b.cnt = 0 then 1 else 0 end wallet_flag
20 from test a join temp b on a.pid = b.pid
21 order by a.pid;
PID POS AMOUNT WALLET_FLAG
---------- ------ ---------- -----------
1 wallet 10 0
1 BT 10 0
1 Cash 10 0
2 BT 50 0
3 Cash 24 0
3 BT 12 0
4 wallet 100 1
5 wallet 20 1
5 wallet 100 1
9 rows selected.
SQL>
SELECT
your_table.*,
MIN(
CASE pos
WHEN 'wallet' THEN 1
ELSE 0
END
)
OVER (
PARTITION BY pid
)
AS wallet_flag
from
your_table
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=e05a7863b9f4d912dcdf5ced5ec1c1b2
How to count an average of attendance based on concert_id?
So far what i have done is like this
select concert_id, event_id, count(customer_id) attendance,
case when concert_id = 1
then (select count(customer_id)/count(concert_id)
from booking where concert_id=1)
end as avg_attendance_each_concert
from booking
group by event_id, concert_id
order by event_id;
result
CONCERT_ID EVENT_ID ATTENDANCE AVG_ATTENDANCE_EACH_CONCERT
---------- ---------- ---------- ---------------------------
1 1 1 3
1 2 2 3
2 3 2
2 4 1
3 5 2
3 6 2
4 8 2
4 9 2
5 11 4
5 12 1
5 13 1
How to make AVG_ATTENDANCE_EACH_CONCERT become like this?
CONCERT_ID EVENT_ID ATTENDANCE AVG_ATTENDANCE_EACH_CONCERT
---------- ---------- ---------- ---------------------------
1 1 1 1.5 --> 3 attendance / 2 same concert_id
1 2 2 1.5
2 3 2 1.5 --> 3 attendance / 2 same concert_id
2 4 1 1.5
3 5 2 2 --> 4 attendance / 2 same concert_id
3 6 2 2
4 8 2 2 --> 4 attendance / 2 same concert_id
4 9 2 2
5 11 4 2 --> 6 attendance / 3 same concert_id
5 12 1 2
5 13 1 2
Because I would like to show which event have below average attendance
How about AVG in its analytic form?
(By the way, your example for CONCERT_ID = 5 is wrong; 6 / 3 = 2, not 3).
SQL> with booking (concert_id, event_id, customer_id) as
2 (select 1, 1, 10 from dual union
3 select 1, 2, 10 from dual union
4 select 1, 2, 20 from dual union
5 --
6 select 3, 5, 10 from dual union
7 select 3, 5, 20 from dual union
8 select 3, 6, 30 from dual union
9 select 3, 6, 40 from dual union
10 --
11 select 5, 11, 10 from dual union
12 select 5, 11, 20 from dual union
13 select 5, 11, 30 from dual union
14 select 5, 11, 40 from dual union
15 select 5, 12, 50 from dual union
16 select 5, 13, 60 from dual
17 )
18 select concert_id, event_id, count(customer_id) attendance,
19 avg(count(*)) over (partition by concert_id) avg_attendance_each_concert
20 from booking
21 group by concert_id, event_id
22 order by event_id;
CONCERT_ID EVENT_ID ATTENDANCE AVG_ATTENDANCE_EACH_CONCERT
---------- ---------- ---------- ---------------------------
1 1 1 1,5
1 2 2 1,5
3 5 2 2
3 6 2 2
5 11 4 2
5 12 1 2
5 13 1 2
7 rows selected.
SQL>
i have the following table with different prices in every week and need a numbering like in the last column. consecutive rows with same prices should have the same number like in weeks 11/12 or 18/19. but on the other side weeks 2 and 16 have the same prices but are not consecutive so they should get a different number.
w | price | r1 | need
===========================
1 167,93 1 1
2 180 1 2
3 164,72 1 3
4 147,42 1 4
5 133,46 1 5
6 145,43 1 6
7 147 1 7
8 147,57 1 8
9 150,95 1 9
10 158,14 1 10
11 170 1 11
12 170 2 11
13 166,59 1 12
14 161,06 1 13
15 162,88 1 14
16 180 2 15
17 183,15 1 16
18 195 1 17
19 195 2 17
i have already experimented with the analytics functions (row_number, rank, dens_rank), but didn't found a solution for this problem so far.
(oracle sql 10,11)
does anyone have a hint? thanks.
Simulating your table first:
SQL> create table mytable (w,price,r1)
2 as
3 select 1 , 167.93, 1 from dual union all
4 select 2 , 180 , 1 from dual union all
5 select 3 , 164.72, 1 from dual union all
6 select 4 , 147.42, 1 from dual union all
7 select 5 , 133.46, 1 from dual union all
8 select 6 , 145.43, 1 from dual union all
9 select 7 , 147 , 1 from dual union all
10 select 8 , 147.57, 1 from dual union all
11 select 9 , 150.95, 1 from dual union all
12 select 10, 158.14, 1 from dual union all
13 select 11, 170 , 1 from dual union all
14 select 12, 170 , 2 from dual union all
15 select 13, 166.59, 1 from dual union all
16 select 14, 161.06, 1 from dual union all
17 select 15, 162.88, 1 from dual union all
18 select 16, 180 , 2 from dual union all
19 select 17, 183.15, 1 from dual union all
20 select 18, 195 , 1 from dual union all
21 select 19, 195 , 2 from dual
22 /
Table created.
Your need column is calculated in two parts: first compute a delta column which denotes whether the previous price-column differs from the current rows price column. If you have that delta column, the second part is easy by computing the sum of those deltas.
SQL> with x as
2 ( select w
3 , price
4 , r1
5 , case lag(price,1,-1) over (order by w)
6 when price then 0
7 else 1
8 end delta
9 from mytable
10 )
11 select w
12 , price
13 , r1
14 , sum(delta) over (order by w) need
15 from x
16 /
W PRICE R1 NEED
---------- ---------- ---------- ----------
1 167.93 1 1
2 180 1 2
3 164.72 1 3
4 147.42 1 4
5 133.46 1 5
6 145.43 1 6
7 147 1 7
8 147.57 1 8
9 150.95 1 9
10 158.14 1 10
11 170 1 11
12 170 2 11
13 166.59 1 12
14 161.06 1 13
15 162.88 1 14
16 180 2 15
17 183.15 1 16
18 195 1 17
19 195 2 17
19 rows selected.
You can nest your analytic functions using inline views, so you first group the consecutive weeks with same prices and then dense_rank using those groups:
select w
, price
, r1
, dense_rank() over (
order by first_w_same_price
) drank
from (
select w
, price
, r1
, last_value(w_start_same_price) ignore nulls over (
order by w
rows between unbounded preceding and current row
) first_w_same_price
from (
select w
, price
, r1
, case lag(price) over (order by w)
when price then null
else w
end w_start_same_price
from your_table
)
)
order by w
The innermost inline view with LAG function lets the starting week of every consecutive group get it's own week number, but every consecutive week with same price gets null (weeks 12 and 19 in your data.)
The middle inline view with LAST_VALUE function then use the IGNORE NULLS feature to give the consecutive weeks the same value as the first week within each group. So week 11 and 12 both gets 11 in first_w_same_price and week 18 and 19 both gets 18 in first_w_same_price.
And finally the outer query use DENSE_RANK to give the desired result.
For each row you should count previous rows where (w-1) row price isn't the same as (w) price:
select T1.*,
(SELECT count(*)
FROM T T2
JOIN T T3 ON T2.w-1=T3.w
WHERE T2.Price<>T3.Price
AND T2.W<=T1.W)+1 rn
from t T1
SQLFiddle demo
Try this:
with tt as (
select t.*, decode(lag(price) over(order by w) - price, 0, 1, 0) diff
from t
)
select w
, price
, r1
, row_number() over (order by w) - sum(diff) over(order by w rows between UNBOUNDED PRECEDING and current row) need
from tt
SELECT w, price, r1,
ROW_NUMBER () OVER (PARTITION BY price ORDER BY price) row_column
FROM TABLE