Oracle Sequence mismatch - oracle

I have five different schemas that use the same sequence in the code from the Main Schema.
The code is schema by schema and never overlaps.
By the end of all the schema programs, the sequence should never overlap as it runs individually.
But it is getting duplicate for one schema:
select min(FUTURE_RETAIL_ID), max(FUTURE_RETAIL_ID) From dmfuser_ame.MIG_RPM_FUTURE_RETAIL;
282 950 868
288 967 925
select min(FUTURE_RETAIL_ID), max(FUTURE_RETAIL_ID) From dmfuser_bts.MIG_RPM_FUTURE_RETAIL;
288 967 926
289 773 677
This duplicated:
select min(FUTURE_RETAIL_ID), max(FUTURE_RETAIL_ID) From dmfuser_mot.MIG_RPM_FUTURE_RETAIL;
**289 773 678
311 239 253**
select min(FUTURE_RETAIL_ID), max(FUTURE_RETAIL_ID) From dmfuser_riv.MIG_RPM_FUTURE_RETAIL;
289 773 678
290 761 583
select min(FUTURE_RETAIL_ID), max(FUTURE_RETAIL_ID) From dmfuser_wes.MIG_RPM_FUTURE_RETAIL;
290 761 584
290 826 644
What could be the possible error with this?
Edit:
Here is how the table is populated for each schema: Insert via a PLSQL code
select /*+ PARALLEL(il,8) */
rpm_future_retail_seq.NEXTVAL future_retail_id,
im.item,
im.dept,
im.class,
im.subclass,
rzl.loc_type zone_node_type,
rzl.location,
TRUNC(im.create_datetime) action_date,
il.selling_unit_retail selling_retail,
loc.currency_code selling_retail_currency,
il.selling_uom,
il.multi_units,
il.multi_unit_retail,
loc.currency_code multi_unit_retail_currency,
il.multi_selling_uom,
il.selling_unit_retail clear_retail,
loc.currency_code clear_retail_currency,
il.selling_uom clear_uom,
il.selling_unit_retail simple_promo_retail,
loc.currency_code simple_promo_retail_currency,
il.selling_uom simple_promo_uom,
0 on_simple_promo_ind,
0 on_complex_promo_ind,
0 lock_version,
im.item_parent,
im.diff_1 diff_id,
rzl.zone_id,
'IL' max_hier_level,
'IL' cur_hier_level
from mig_item_master im,
(select wh as location,
currency_code,
0 as type
from mig_wh) loc,
mig_item_loc il,
rpm_zone_location rzl --required to use RMS migrated table as its the only one with the WH
where il.item = im.item
and il.loc = loc.location
and rzl.location = loc.location
and item_level = tran_level
and rzl.loc_type = 2
and sellable_ind = 'Y'
and il.loc_type = 'W'
and not exists (select 1
from mig_rpm_future_retail rfr
where rfr.item = il.item
and rfr.location = il.loc);

Related

After Sort in Python how to create new Index field

After I sort my data, I am trying to create a new index field.
Here is my code:
mydata_1=[[38,125,56],[98,23,150],[11,46,15],[23,87,81]]
cols= ['Col_A','Col_B','Col_C']
mydata_2= pd.DataFrame(mydata_1, columns=cols)
mydata_3= mydata_2.sort('Col_A')
So with my code above the output looks like this:
Col_A Col_B Col_C
2 11 46 15
3 23 87 81
0 38 125 56
1 98 23 150
I need to create a new column called "Col_D" as seen below:
Col_A Col_B Col_C Col_D
2 11 46 15 1
3 23 87 81 2
0 38 125 56 3
1 98 23 150 4
Also, I am going to be using this on many data sets. Is there a way to do this without specifying a range?
Here you go
import pandas as pd
mydata_1=[[38,125,56],[98,23,150],[11,46,15],[23,87,81]]
cols= ['Col_A','Col_B','Col_C']
mydata_2= pd.DataFrame(mydata_1, columns=cols)
mydata_3= mydata_2.sort('Col_A')
mydata_3["Col_D"] = range(len(mydata_3.index))
print(mydata_3)

Open a refcursor for a PL/SQL statement?

I'm creating a report for a business using Oracle and SSRS. The report requires me to aggregate contiguous ranges of serial numbers, which can consist of alphanumerics.
For example, say I have the following serials:
OPS114
OPS115
OPS116
OPS117
OPS145
OPS146
OPS160
890RPT
896RPT
897RPT
The report should have a single aggregate row for each contiguous range, with the count of each range, like so:
OPS114 - OPS117 (4)
OPS145 - OPS146 (2)
OPS160 - OPS160 (1)
890RPT - 890RPT (1)
896RPT - 897RPT (2)
I've pulled the data I need, and I'm bulk-collecting it into a table variable. Now, I need to aggregate the rows - this wouldn't be bad if I only needed to manipulate the data, but I need to have this available as a query for the refcursor. Can I open a refcursor for a PL/SQL FOR loop, or am I barking up the wrong tree? I've attempted to Google this, but the "cursor for loop" is not what I'm looking for. The alternative is to try to aggregate the results in SSRS using VB. (So either way, it won't be a good time.) I'm not sure if I have access to create a SQL table type for this, so this is the alternative I've sought.
If anyone has any experience with this, it would be greatly appreciated!
You could do this from a single SQL statement but you need to define the data better. Your column stores strings but you are using them as numbers to find out the range. And it seems the number part could either be before or after the string part.
If you are able to write some logic that separates out the numbers like this (and maybe keep the string part in another column)-
114
115
116
117
145
146
160
890
896
897
Then it reduces to a simple gaps and islands problem.
Step 1 - Select rownum along with this column (this would be a continuous sequence starting from 1)
Step 2 - Subtract rownum from this numeric data column.
Step 3 - Group by that result
Step 4 - Get min(numeric value) and max(numeric value) and count(numeric value) from the group which will be your result when combined as string.
Numeric_part Rownum Difference
------------ ------ ------------
114 1 113
115 2 113
116 3 113
117 4 113
145 5 140
146 6 140
160 7 153
890 8 882
896 9 887
897 10 887
Grouping this by Difference column, you get -
Difference Min(num) Max(num) count(num) Result
---------- --------- ---------- ---------- -----------------
113 114 117 4 114 - 117 (4)
140 145 146 2 145 - 146 (2)
153 160 160 1 160 - 160 (1)
882 890 890 1 890 - 890 (1)
887 896 897 2 896 - 897 (2)
That SQL statement can be used in PLSQL to return a cursor as in this link that #MickMnemonic has in the comments.
Basically -
OPEN cursor_variable FOR SELECT something FROM something...;
I spoke to a coworker about this and he had an idea that I've been able to implement.
I was able to create a pipelined function that handled my data selection and transforms for me; this allowed me to aggregate my rows as I needed to and only pass rows after a contiguous range was complete.
I am now calling that function from a procedure using the SELECT ... FROM TABLE(MYFUNCTION()) syntax. This allows me to get everything into a refcursor without much issue.
While this may not be performant (looping over a cursor, and manually aggregating), this is for a monthly report, and so I won't attempt to optimize until it's necessary (as we have other work to do).

Need a macro or a way to create list from Excel table

I am working on distance related table and I would like to add it to mysql database but need it to be in a different format than the table itself.
Here is the table
MANH BONX BROK QUNZ STIS Loc1 Loc2 Loc3
ABINGTONCT 360 360 390 380 400 380 390 435
ADDISONCT 285 285 310 300 325 300 310 355
Need it to be in this format
MANH ABINGTONCT 360
MANH ADDISONCT 285
BONX ABINGTONCT 360
BONX ADDISONCT 285
BROK ABINGTONCT 390
BROK ADDISONCT 310

COUNT returning NULL, should return 0

I have a simple query for the table:
Person id Organization id employee_nam age busines_group_id
123 Zuyo 10 John 30 81
2457 Zuyo 10 Geet 69 81
56 Ghiya 12 paul 20 81
frei 13 81
SELECT
COUNT(DISTINCT ped.person_id)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
SELECT
NVL(COUNT(DISTINCT ped.person_id), 0)
FROM
per_emp_detail ped
WHERE
ped.business_group_id = 81
AND
ped.id = NVL(p_org_id, ped.organization_id);
p_org_id is the parameter which I am passing which can be 10, 12, or 13.
COUNT returns 2 for id 10. 1 for id 12. but is returning NULL for id 13.
I want 0 to be returned in this case.
NVL and CASE are also not working.
try MAX:
nvl(max(count(DISTINCT ped.person_id)),0)

Hive: Joining tables with different scenarios

I have a question on joining tables in a different scenario. Please find the sample tables below.
Capacity of expected table row 3-5 should be repeated as table 2 does not have those fields.
could anyone please help to get expected table?
Table 1:
No ProjectID Capacity
1 514 4
2 418 10
3 418 30
4 401 40
5 502 41
Table2:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
Expected Table:
NO ProjectID Capacity1 Capacity2
1 514 4 10
2 418 10 20
3 418 30 30
4 401 40 40
5 502 41 41
1.Do left outer join
2.For the values not matching take them from table 1 with if condition.
select t1.no,t1.projectid,t1.capacity1,if(t2.capacity2 is null,t1.capacity,t2.capacity)
from table1 t1 left outer join table2 t2 on t1.no=t2.no
I think above query meets your requirement let me know if need any more help.

Resources