Dear Respectful Experts,
We have a table having Column TOTAL_MONTHS e.g. if
As shown below table need to Update using Update Statement.
TOTAL_MONTHS = 15 then Update the Column named "Months<=12" = 12 and Update the Column named "Months>12" = 3
another example in below table is
TOTAL_MONTHS =8 then Update Column named "Months<=12" = 8 and Update Column named "Months>12" = 0
Title
Amount
Total_months
Months<=12
Months>12
10101288
28000
15
12
3
10101289
40000
13
12
1
10101290
2000000
10
10
0
10101291
50000
14
12
2
10101239
6000
11
11
0
10101240
50000
8
8
0
10121003
690
12
12
0
CREATE TABLE "TEST3"
( TITLE VARCHAR2(100 BYTE),
AMOUNT NUMBER,
Total_Months NUMBER,
Months<=12 NUMBER,
Months>12 NUMBER
)
REM INSERTING into TEST3
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101288',28000,15,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101289',40000,13,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101290',2000000,10,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101291',50000,14,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101239',6000,11,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10101240',50000,8,null,null);
Insert into TEST3 (TITLE,AMOUNT,"Total_Months","Months<=12","Months>12") values ('10121003',690,12,null,null);
Somebody can help us please.
Thanks,
Regards,
Out Put Result as below
Title
Amount
Total_months
Months<=12
Months>12
10101288
28000
15
12
3
10101289
40000
13
12
1
10101290
2000000
10
10
0
10101291
50000
14
12
2
10101239
6000
11
11
0
10101240
50000
8
8
0
10121003
690
12
12
0
Use LEAST and GREATEST:
update test3
set "Months<=12" = LEAST(total_months, 12),
"Months>12" = GREATEST(total_months - 12, 0);
The same can be done with CASE expressions of course.
Demo: https://dbfiddle.uk/wB89f9eN
I don't consider it a good idea to store these calculated values in common table columns, by the way. Don't store data redundantly in a database. What would it mean, if some day you find a row containing total_months = 15, "Months<=12" = 12, "Months>12" = 10? Which value would be correct, which not? If you want to see the results like if they were table columns for convenience, use generated columns or a view instead.
If you want to calculate the months as full-years and part-years or as up-to-one year and more-than-one-year then you can calculate those values and do not need to have separate columns in the database that run the risk of becoming out of sync.
If you did want to represent the values in the table then you can use virtual columns:
CREATE TABLE "TEST3"(
TITLE VARCHAR2(100 BYTE),
AMOUNT NUMBER,
Total_Months NUMBER,
Full_Year_Months NUMBER
GENERATED ALWAYS AS (Total_months - MOD(total_months, 12)),
Part_Year_Months NUMBER
GENERATED ALWAYS AS (MOD(total_months, 12)),
"Month>12" NUMBER
GENERATED ALWAYS AS (GREATEST(total_months-12,0)),
"Month<=12" NUMBER
GENERATED ALWAYS AS (LEAST(total_months,12))
);
Which, for the sample data:
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101288', 28000,15);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101289', 40000,13);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101290',2000000,10);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101291', 50000,14);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101239', 6000,11);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10101240', 50000, 8);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('10121003', 690,12);
Insert into TEST3 (TITLE,AMOUNT,Total_Months) values ('12345678',1234567,45);
Then the table contains:
TITLE
AMOUNT
TOTAL_MONTHS
FULL_YEAR_MONTHS
PART_YEAR_MONTHS
Month>12
Month<=12
10101288
28000
15
12
3
3
12
10101289
40000
13
12
1
1
12
10101290
2000000
10
0
10
0
10
10101291
50000
14
12
2
2
12
10101239
6000
11
0
11
0
11
10101240
50000
8
0
8
0
8
10121003
690
12
12
0
0
12
12345678
1234567
45
36
9
33
12
fiddle
I have a table that contains one or more records for each item. Each item can contain multiple sub-items (boards) and so the Itemid is often replicated with each record showing the division category (a number) that the Item/sub-item combo resides in:
ItemId Board# Division
142585109 0 6
142585114 0 3
142585116 0 1
142585120 0 4
142585197 0 5
142585197 2 4
142585197 3 3
142585197 5 6
142585197 8 1
142585294 0 4
142585317 0 1
I want to update the table and aggregate all of the division values (as a comma separated string) in a new field in this table, something like:
ItemId Board# AggDivisions
142585109 0 6
142585114 0 3
142585116 0 1
142585120 0 4
142585197 0 1,3,4,5,6
142585294 0 4
142585317 0 1
I used a ListAgg query to do the aggregation which works correctly but when I tried to incorporate this into an update query, I end up with multiple duplicates in the aggregated field for each record.
Here is my update attempt:
update itemtable dd
set aggregateddivisions = (SELECT Listagg(division, ',') within GROUP (ORDER BY division)
FROM itemtable ev
WHERE ev.itemid = dd.itemid
)
where exists (select 1
from itemtable ev
where ev.itemid = dd.itemid
);
How can I update the table with the aggregated list of values from the same table without ending up with duplicates?
I want to merge two columns(Sender and Receiver) and get the Transaction Type count then merge another table with using Sender_Receiver primary id.
Sender Receiver Type Amount Date
773787639 777611388 1 300 2/1/2019
773631898 776806843 4 450 8/20/2019
773761571 777019819 6 369 2/11/2019
774295511 777084440 34 1000 1/22/2019
774263079 776816905 45 678 6/27/2019
774386894 777202863 12 2678 2/10/2019
773671537 777545555 14 38934 9/29/2019
774288117 777035194 18 21 4/22/2019
774242382 777132939 21 1275 9/30/2019
774144715 777049859 30 6309 7/4/2019
773911674 776938987 10 3528 5/1/2019
773397863 777548054 15 35892 7/6/2019
776816905 772345091 6 1234 7/7/2019
777035194 775623065 4 453454 7/20/2019
Second Table
Mobile_number Age
773787639 34
773787632 23
774288117 65
I am try to get like this kind of table
Sender/Receiver Type_1 Type_4 Type_12...... Type_45 Age
773787639 3 2 0 0 23
773631898 1 0 1 2 56
773397863 2 2 0 0 65
772345091 1 1 0 3 32
Ok, I have seen your old question and you just need inner join in sub-query as following:
SELECT
SenderReceiver,
COUNT(CASE WHEN Type = 1 THEN 1 END) AS Type_1,
COUNT(CASE WHEN Type = 2 THEN 1 END) AS Type_2,
COUNT(CASE WHEN Type = 3 THEN 1 END) AS Type_3,
...
COUNT(CASE WHEN Type = 45 THEN 1 END) AS Type_45,
Age -- changes here
FROM
( SELECT sr.SenderReceiver, sr.Type, st.Age from -- changes here
(SELECT Sender AS SenderReceiver, Type FROM yourTable
UNION ALL
SELECT Receiver, Type FROM yourTable) sr
join <second_table> st on st.Mobile_number = sr.SenderReceiver -- changes here
) t
GROUP BY
SenderReceiver,
Age; -- changes here
Changes done in your previous query are marked with comments -- changes here.
Please replace the name of the <second_table> with the original name of the table.
Cheers!!
I am a beginner for Spotfire. I have a problem about the difference calculation for the some column value. A sample table could be like this:
id timestamp state
1 7/1/2016 12:00:01 AM 1
2 7/1/2016 12:00:03 AM 0
3 7/1/2016 12:00:04 AM 1
4 7/1/2016 12:00:06 AM 0
5 7/1/2016 12:00:09 AM 1
6 7/1/2016 12:00:10 AM 0
7 7/1/2016 12:00:12 AM 1
I want to calculate the time difference for the timestamp when the state is 1,
the final table I want to have is:
id timestamp state time_diffence
3 7/1/2016 12:00:04 AM 1 3
5 7/1/2016 12:00:09 AM 1 5
7 7/1/2016 12:00:12 AM 1 3
it seems that I should identify an expression for the calculation, but I have not idea for the calculation just for one parameter :(. somebody could help me ?
still one more small question: what if the timestamp column value is just number value, how can i calculate the difference, is there any related function like DateDiff() here? for example:
id times state
1 12 1
2 7 0
3 10 1
4 11 0
5 6 1
6 9 0
7 7 1
the result could be :
id times state diffence
3 10 1 -2
5 6 1 -4
7 7 1 1
after running the code: i have the error as below:
for the row if it has the same time stamp as the last previous row, the difference will keep same as before, but actually the difference for the rows which have same time stamp would be as 0
thanks for your help :)
Assuming your data is sorted in ascending order by [timestamp] before you import it, you can partition using the Previous function with Over where the [state]=1.
Insert a calculated column with this expression:
If([state]=1,DateDiff("ss",Min([timestamp]) OVER (Previous([timestamp])),[timestamp]))
You will see it populated in your table like the below:
Then if you ONLY want to see the rows that have the difference you mentioned, on your table you can...
Right Click > Properties > Data > Limit data using expression >
And insert the expression: [time_difference] > 1
This will result in this table:
Problem is in left outer join, when there are no rows in right side table then it does not display null values, it displays previous values....
Like this....
1 st Table contains
PGMTX_CODE PGMTX_MARKS PGMTX_TOTQSTN
-------------------------------------------
EE 1 5
EE 2 5
EE 3 0
EE 4 0
2 nd Table contains
PGMTX_CODE PGMTX_MARKS PGMTX_ACTUSEDQST
-------------------------------------------
EE 1 5
So I want result like...
PGMTX_MARKS PGMTX_TOTQSTN PGMTX_ACTUSEDQST
--------------------------------------------------
1 5 5
2 5 blank
3 0 blank
4 0 blank
I use query like this...
SELECT m.PGMTX_MARKS,
m.PGMTX_TOTQSTN,
tlm.PGMTX_ACTUSEDQST,
from PAPERGEN_MTL_OEX m
left OUTER JOIN PAPERGEN_TLMTL_OEX tlm
ON m.PGMTX_CODE=tlm.PGMTX_CODE
where m.PGMTX_CODE='EE'
order by m.PGMTX_MARKS
But I got result like
PGMTX_MARKS PGMTX_TOTQSTN PGMTX_ACTUSEDQST
--------------------------------------------------
1 5 5
2 5 5
3 0 5
4 0 5
Your join condition is wrong, should be
ON m.PGMTX_CODE=tlm.PGMTX_CODE AND m.PGMTX_MARKS = tlm.PGMTX_MARKS