Distribute value in sql 2000 - stored-procedures

#tempR table
iPriority_num GCAMOUNT
221143 2000.00
221255 1500.00
#tempV table
iPriority_num sPlu_cd iDetail_num siTem_cd ITEMAMOUNT
221143 0660 1 ACN00660 6000.00
221255 0112 1 CON00112 500.00
221255 0134 2 HAI00134 750.00
221255 0851 3 FPR50074 249.00
221255 0008 4 FNU30200 300.00
221255 0856 5 MICO0001 297.00
This is my script
SELECT t.iPriority_num as [TransactionID]
,t.iDetail_num as [Order ID],t.sPlu_cd AS [ItemCode]
,t.ITEMAMOUNT
,d.GCAMOUNT as [GC Payment]
,CASE WHEN d.GCAMOUNT < t.ITEMAMOUNT
THEN (d.GCAMOUNT / CAST((SELECT COUNT(*) as cnt FROM #tempV t2 where t.iPriority_num = t2.iPriority_num) AS decimal(10,2)))
ELSE
d.GCAMOUNT − t.ITEMAMOUNT
END AS DistributedAmt
FROM #tempV t
INNER JOIN #tempR dON d.iPriority_num = t.iPriority_num
The result
TransactionID OrderID ItemCode ITEMAMOUNT GC Payment DistributedAmt
221143 1 0660 6000.00 2000.00 2000.00
221255 1 0112 500.00 1500.00 1000.00
221255 2 0134 750.00 1500.00 750.00
221255 3 0851 249.00 1500.00 1251.00
221255 4 0008 300.00 1500.00 1200.00
221255 5 0856 297.00 1500.00 1203.00
Desired Ouput
TransactionID OrderID ItemCode ITEMAMOUNT GC Payment DistributedAmt
221143 1 0660 6000.00 2000.00 2000.00
221255 1 0112 500.00 1500.00 500.00
221255 2 0134 750.00 1500.00 750.00
221255 3 0851 249.00 1500.00 249.00
221255 4 0008 300.00 1500.00 1.00
221255 5 0856 297.00 1500.00 0.00
All I want is to distribute amount of each item if Item Amount is less than the GC Payment

Related

ActiveRecord::Fixture::FixtureError: table has no columns named "false"

I am getting the error: ActiveRecord::Fixture::FixtureError: table "creatures" has no columns named "false". I have no column named false in this model.
What is going on?
Here is my fixture:
3 one:
4 name: MyString
5 no: 1
6 type1: 1
7 type2: 1
8 total: 1
9 hp: 1
10 attack: 1
11 defense: 1
12 special_attack: 1
13 special_defense: 1
14 speed: 1
15 generation: 1
16 legendary: false
17
Putting the no in single quotes solved the problem.
If I put a debugger call just before the error is raised:
[475, 484] in /usr/local/bundle/gems/activerecord-7.0.4.2/lib/active_record/connection_adapters/abstract/database_statements.rb
475: fixture = fixture.stringify_keys
476:
477: unknown_columns = fixture.keys - columns.keys
478: if unknown_columns.any?
479: debugger
=> 480: raise Fixture::FixtureError, %(table "#{table_name}" has no columns named #{unknown_columns.map(&:inspect).join(', ')}.)
481: end
482:
483: columns.map do |name, column|
484: if fixture.key?(name)
(byebug) fixtures
It looks like the no got interpreted as a false:
[{"name"=>"MyString", false=>1, "type1"=>1, "type2"=>1, "total"=>1, "hp"=>1, "attack"=>1, "defense"=>1, "special_attack"=>1, "special_defense"=>1, "speed"=>1, "genneration"=>1, "legendary"=>false, "created_at"=>2023-02-05 18:53:31.63881045 UTC, "updated_at"=>2023-02-05 18:53:31.63881045 UTC, "id"=>980190962}, {"name"=>"MyString", false=>2, "type1"=>1, "type2"=>1, "total"=>1, "hp"=>1, "attack"=>1, "defense"=>1, "special_attack"=>1, "special_defense"=>1, "speed"=>1, "genneration"=>1, "legendary"=>false, "created_at"=>2023-02-05 18:53:31.63881045 UTC, "updated_at"=>2023-02-05 18:53:31.63881045 UTC, "id"=>298486374}]
Putting the no in single quotes solved the problem:
3 one:
4 name: MyString
5 'no': 1
6 type1: 1
7 type2: 1
8 total: 1
9 hp: 1
10 attack: 1
11 defense: 1
12 special_attack: 1
13 special_defense: 1
14 speed: 1
15 generation: 1
16 legendary: false
17
18 two:
19 name: MyString
20 'no': 2
21 type1: 1
22 type2: 1
23 total: 1
24 hp: 1
25 attack: 1
26 defense: 1
27 special_attack: 1
28 special_defense: 1
29 speed: 1
30 generation: 1
31 legendary: false

Print and save to excel issues

I have the following script and I have problems on >printing and
saving. Any ideas or help welcome.
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [3513, 3514, 3517],
'B':['lname1', 'lname2', 'lname3'],
'C':['fname1', 'fname2', 'fname3'],
},index=np.arange(3,dtype=int))
def vamos(df):
for x in df.index:
s = (df.loc[x,'A'])
digits = list(map(int, str(s)))
Sum = (sum(digits))
df = df.assign(column_2=(Sum))
df['column_3'] = 20 - Sum
print(df)
df.to_excel("book_Sum.xlsx")
if __name__ == '__main__':
vamos(df)
This is what I get with print(df):
A B C column_2 column_3
0 3513 lname1 fname1 12 8
1 3514 lname2 fname2 12 8
2 3517 lname3 fname3 12 8
A B C column_2 column_3
0 3513 lname1 fname1 13 7
1 3514 lname2 fname2 13 7
2 3517 lname3 fname3 13 7
A B C column_2 column_3
0 3513 lname1 fname1 16 4
1 3514 lname2 fname2 16 4
2 3517 lname3 fname3 16 4
And this when I save to excel. df.to_excel("book_Sum.xlsx")
A B C column_2 column_3
0 3513 lname1 fname1 16 4
1 3514 lname2 fname2 16 4
2 3517 lname3 fname3 16 4

How to concat or Listagg two values in a same column to join it with the values in other column

I have two tables :
TABLE_A
pos_id res_id bb_id bsk_name
1122 10000 1444 type_1
1122 10000 5678 type_2
1122 10001 1444 type_1
1122 10001 5678 type_2
1122 10002 1467 type_1
1122 10002 5678 type_2
1122 10003 1467 type_1
1122 10003 5678 type_2
table_b
pos_id row_id bb_id bsk_name
1122 1 1444 type_1
1122 1 5678 type_2
1122 2 1467 type_1
1122 2 5678 type_2
I wanted to join the table_a and table_b to get the row_id for each res_id.
the res_id 10000 and 10001 has to have the row_id 1 and res_id 10002 and 10003 has to have the row_id 2. but since there is no unique column to join these two tables i get duplicated values for the bb_id 5678 since they are the same in both row_id's.
So Is there a way like to listagg the bb_id's with the erg_id in table_a and row_id in table_b to join these two tables ?
You can use CAST(COLLECT(...) AS ...) to aggregate the rows into a user-defined collection and then compare the collections.
First, create a nested-table-type collection:
CREATE TYPE int_list AS TABLE OF INT;
Then you can use:
SELECT a.pos_id,
a.res_id,
a.bb_id,
a.bsk_name,
b.row_id,
b.bsk_name
FROM (
SELECT a.*,
CAST(
COLLECT(bb_id) OVER (PARTITION BY pos_id, res_id)
AS int_list
) AS all_bb_ids
FROM table_a a
) a
INNER JOIN (
SELECT b.*,
CAST(
COLLECT(bb_id) OVER (PARTITION BY pos_id, row_id)
AS int_list
) AS all_bb_ids
FROM table_b b
) b
ON ( a.pos_id = b.pos_id
AND a.all_bb_ids = b.all_bb_ids
AND a.bb_id = b.bb_id)
Which, for your sample data:
CREATE TABLE TABLE_A ( pos_id, res_id, bb_id, bsk_name ) AS
SELECT 1122, 10000, 1444, 'type_1' FROM DUAL UNION ALL
SELECT 1122, 10000, 5678, 'type_2' FROM DUAL UNION ALL
SELECT 1122, 10001, 1444, 'type_1' FROM DUAL UNION ALL
SELECT 1122, 10001, 5678, 'type_2' FROM DUAL UNION ALL
SELECT 1122, 10002, 1467, 'type_1' FROM DUAL UNION ALL
SELECT 1122, 10002, 5678, 'type_2' FROM DUAL UNION ALL
SELECT 1122, 10003, 1467, 'type_1' FROM DUAL UNION ALL
SELECT 1122, 10003, 5678, 'type_2' FROM DUAL;
CREATE TABLE table_b (pos_id, row_id, bb_id, bsk_name) AS
SELECT 1122, 1, 1444, 'type_1' FROM DUAL UNION ALL
SELECT 1122, 1, 5678, 'type_2' FROM DUAL UNION ALL
SELECT 1122, 2, 1467, 'type_1' FROM DUAL UNION ALL
SELECT 1122, 2, 5678, 'type_2' FROM DUAL;
Outputs:
POS_ID
RES_ID
BB_ID
BSK_NAME
ROW_ID
BSK_NAME
1122
10000
1444
type_1
1
type_1
1122
10000
5678
type_2
1
type_2
1122
10001
1444
type_1
1
type_1
1122
10001
5678
type_2
1
type_2
1122
10002
1467
type_1
2
type_1
1122
10002
5678
type_2
2
type_2
1122
10003
1467
type_1
2
type_1
1122
10003
5678
type_2
2
type_2
db<>fiddle here
Update
You could also use LISTAGG to aggregate the data; however, if the aggregated string will exceed 4000 bytes then the aggregation will fail (using CAST(COLLECT(...) ...) does not have this limitation but does require you create a collection data type):
SELECT a.pos_id,
a.res_id,
a.bb_id,
a.bsk_name,
b.row_id,
b.bsk_name
FROM (
SELECT a.*,
LISTAGG(bb_id, ',') WITHIN GROUP (ORDER BY bb_id)
OVER (PARTITION BY pos_id, res_id) AS all_bb_ids
FROM table_a a
) a
INNER JOIN (
SELECT b.*,
LISTAGG(bb_id, ',') WITHIN GROUP (ORDER BY bb_id)
OVER (PARTITION BY pos_id, row_id) AS all_bb_ids
FROM table_b b
) b
ON ( a.pos_id = b.pos_id
AND a.all_bb_ids = b.all_bb_ids
AND a.bb_id = b.bb_id)
db<>fiddle here

Fixing joining two datasets with same variables

I want to join two datasets.
Datasets had same columns/ variables.
Dataset 1 (n11)
caseid v000 v005 age v021 v022 v023 v024 resi region v102 education pregnant v445 v501 v717 wealth stra occupation
1 101 15 2 NP6 342191 5 101 10 1 1 2 1 2 1 0 2190 1 4 4 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
2 101 19 2 NP6 342191 7 101 10 1 1 2 1 2 0 0 2300 1 4 3 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
3 101 19 4 NP6 342191 2 101 10 1 1 2 1 2 1 0 2139 1 4 3 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
4 101 21 4 NP6 342191 5 101 10 1 1 2 1 2 0 0 1855 1 4 2 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
5 101 45 3 NP6 342191 5 101 10 1 1 2 1 2 0 0 2133 3 4 3 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
6 101 47 2 NP6 342191 1 101 10 1 1 2 1 2 2 0 2022 1 4 4 NPIR61FL$ssubreg=1, NPIR61FL$v025=2 2
Dataset 2 (n16)
caseid v000 v005 age v021 v022 v023 v024 resi region v102 education pregnant v445 v501 v717 wealth stra occupation
1 101 2 2 NP5 295061 6 101 1 1 1 2 1 2 0 0 2534 1 4 2 NPIR51FL$v024=1, NPIR51FL$v025=2 2
2 101 2 3 NP5 295061 1 101 1 1 1 2 1 2 2 0 2061 0 4 2 NPIR51FL$v024=1, NPIR51FL$v025=2 2
3 101 3 3 NP5 295061 1 101 1 1 1 2 1 2 2 0 2157 1 4 1 NPIR51FL$v024=1, NPIR51FL$v025=2 2
4 101 4 1 NP5 295061 4 101 1 1 1 2 1 2 0 0 2370 1 4 1 NPIR51FL$v024=1, NPIR51FL$v025=2 2
5 101 6 6 NP5 295061 2 101 1 1 1 2 1 2 3 0 2254 0 4 2 NPIR51FL$v024=1, NPIR51FL$v025=2 2
6 101 7 2 NP5 295061 2 101 1 1 1 2 1 2 2 0 2364 0 4 1 NPIR51FL$v024=1, NPIR51FL$v025=2 2
I used rbind function.
code was d2 <-rbind(n11, n16)
I found the following error. How can I fix this?
Error: Can't convert from <labelled<double>> to <labelled<double>> due to loss of precision.
* Locations: 5723, 5724, 5725, 5726, 5727, 5728, 5729, 5730, 5731, 5732, 5733, 5734, 5735, 5736, 5...
Values are labelled in `` but not in ``.
Run `rlang::last_error()` to see where the error occurred.
I have found the solution now.
We can use full_join function of dplyr package to solve this.
d2<- full_join(n11, n16)

What does the "*A" and "*U" means in the "sctp assocs" display?

cat /proc/net/sctp/assocs
ASSOC-ID ASSOC SOCK STY SST ST HBKT TX_QUEUE RX_QUEUE UID INODE LPORT RPORT LADDRS <-> RADDRS HBINT INS OUTS MAXRT T1X T2X RTXC wmema wmemq sndbuf rcvbuf
13 ffff8800b93a9000 ffff8800ac752300 2 1 3 9176 0 0 0 20735 3905 48538 *192.168.44.228 <-> *A172.16.236.92 7500 10 10 2 0 0 23 1 0 2000000 2000000
0 ffff88042aea3000 ffff880422e88000 0 10 1 40840 0 0 0 9542 3868 3868 *10.127.58.66 <-> *U10.127.115.194 7500 17 17 10 0 0 0 1 0 8388608 8388608

Resources