ParentFieldValue and COUNT MVL in calculated field: Siebel - siebel

I want to count child records and using COUNT ("MVL") function in a calculated field. My search spec is like below:
[Hearing Date] = ParentFieldValue("Schedule Date") AND [GHQ Session Status] = LookupValue("GHQ_SESSION_STATUS", "Scheduled")
My calculated field always gives 0 (zero). I looked into logs and found a straight query:
SELECT
T10.CONFLICT_ID,
T10.LAST_UPD,
T10.CREATED,
T10.LAST_UPD_BY,
T10.CREATED_BY,
T10.MODIFICATION_NUM,
T10.ROW_ID,
T9.X_SRL_NUMBER,
T10.OCCUR_DT,
T10.X_PAR_JUD_ID,
T10.X_AUTO_CREATED_FLG,
T7.NAME,
T1.FST_NAME,
T1.JOB_TITLE,
T1.LAST_NAME,
T5.POSTN_ID,
T6.PR_POSTN_ID,
T2.ATTRIB_03,
T10.X_REVIEW_FLG,
T10.X_MP_POSTN_ID,
T4.X_PANEL_ID,
T10.X_CASE_ID,
T10.X_PANEL_ID,
T10.X_PANEL_SCHED_ID,
T9.X_REF_NUMBER,
T9.SERIAL_NUM,
T10.X_STATUS_CD,
T10.X_STATUS_CD,
:1
FROM
SIEBEL.S_CONTACT T1,
SIEBEL.S_PROJ_XM T2,
SIEBEL.S_OFFENSE T3,
SIEBEL.S_INCIDENT T4,
SIEBEL.S_PROJ_POSTN T5,
SIEBEL.S_PROJ T6,
SIEBEL.S_PROJ T7,
SIEBEL.S_POSTN T8,
SIEBEL.S_CASE T9,
SIEBEL.S_INCIDENT T10
WHERE
T10.X_PANEL_SCHED_ID = T2.ROW_ID (+) AND
T10.X_PANEL_ID = T6.ROW_ID (+) AND
T10.X_PANEL_ID = T5.PROJ_ID (+) AND T5.X_PANEL_ROLE (+) = 'Head of Bench' AND
T4.X_PANEL_ID = T7.ROW_ID (+) AND
T3.INCIDENT_ID = T4.ROW_ID (+) AND
T10.X_PAR_JUD_ID = T3.ROW_ID (+) AND
T8.PR_EMP_ID = T1.PAR_ROW_ID (+) AND
T10.X_MP_POSTN_ID = T8.PAR_ROW_ID (+) AND
T10.X_CASE_ID = T9.ROW_ID (+) AND
((T10.X_STATUS_CD IN ( :2 )) AND
(T10.X_TYPE = 'GHQ Hearing Session')) AND
(T10.X_PANEL_ID = :3)
ORDER BY
T10.X_PANEL_ID DESC, T10.CREATED DESC
ObjMgrSqlLog Detail 4 0000000257bd2d90:0 2016-08-24 12:02:44 Bind variable 1: ,,,SADMIN,0000000257bd2d90:0,,GHQ Hearing BC,LHC Roster Roster Detail View
ObjMgrSqlLog Detail 4 0000000257bd2d90:0 2016-08-24 12:02:44 Bind variable 2: Scheduled
ObjMgrSqlLog Detail 4 0000000257bd2d90:0 2016-08-24 12:02:44 Bind variable 3: 1-12IWRL
ObjMgrSqlLog Debug 5 0000000257bd2d90:0 2016-08-24 12:02:44 User search spec:
ObjMgrSqlLog Debug 5 0000000257bd2d90:0 2016-08-24 12:02:44 Named search [Link Search Spec Named Search]: [Hearing Date] = ParentFieldValue("Schedule Date") AND [GHQ Session Status] = LookupValue("GHQ_SESSION_STATUS", "Scheduled")
ObjMgrSqlLog Debug 5 0000000257bd2d90:0 2016-08-24 12:02:44 Named search [System Search]: [Type]='GHQ Hearing Session'
ObjMgrSqlLog Debug 5 0000000257bd2d90:0 2016-08-24 12:02:44 User sort spec: Created (DESCENDING)
ObjMgrSqlLog Debug 5 0000000257bd2d90:0 2016-08-24 12:02:44 System sort spec: Panel Id
But when I remove [Hearing Date] = ParentFieldValue("Schedule Date") part in search spec. Then I get count and below is the generated query:
SELECT
COUNT (*),
:1
FROM
SIEBEL.S_INCIDENT T1
WHERE
((T1.X_STATUS_CD IN ( :2 )) AND
(T1.X_TYPE = 'GHQ Hearing Session')) AND
(T1.X_PANEL_ID = :3)
ObjMgrSqlLog Detail 4 0000000257bd1758:0 2016-08-24 12:52:25 Bind variable 1: ,,,SADMIN,0000000257bd1758:0,,GHQ Hearing BC,LHC Roster Roster Detail View
ObjMgrSqlLog Detail 4 0000000257bd1758:0 2016-08-24 12:52:25 Bind variable 2: Scheduled
ObjMgrSqlLog Detail 4 0000000257bd1758:0 2016-08-24 12:52:25 Bind variable 3: 1-12IWRL
ObjMgrSqlLog Debug 5 0000000257bd1758:0 2016-08-24 12:52:25 User search spec:
ObjMgrSqlLog Debug 5 0000000257bd1758:0 2016-08-24 12:52:25 Named search [Link Search Spec Named Search]: [GHQ Session Status] = LookupValue("GHQ_SESSION_STATUS", "Scheduled")
ObjMgrSqlLog Debug 5 0000000257bd1758:0 2016-08-24 12:52:25 Named search [System Search]: [Type] = "GHQ Hearing Session"
ObjMgrSqlLog Debug 5 0000000257bd1758:0 2016-08-24 12:52:25 User sort spec:
ObjMgrSqlLog Debug 5 0000000257bd1758:0 2016-08-24 12:52:25 System sort spec:
ObjMgrSqlLog Debug 5 0000000257bd1758:0 2016-08-24 12:52:25
Link specification property is set for all the fields being used in search spec and also on the calculated field. I am not sure what I am doing wrong here or it is the expected behavior.

I suspect the problem is in
ObjMgrSqlLog Debug 5 0000000257bd2d90:0 2016-08-24 12:02:44 System sort spec: Panel Id
I am assuming this is an Association Sort spec on the link. This causes the system to activate the field Panel Id, which adds further tables and conditions into the SQL, restricting data.
Suggestion:Try removing that sort spec and try the original calc field again.

I suspect the culprit is:
AND T5.X_PANEL_ROLE (+) = 'Head of Bench'
Can you check the results of the query after commenting it out?

Link Specification property on BC field did the job.

Related

BaseQuery.paginate() return inconsistent results

When I manually execute this query in MySQL, the number of results I get is 28:
SELECT *
FROM position_reporting_structures prs
INNER JOIN positions AS pos ON pos.id = prs.reports_to
INNER JOIN jobs ON jobs.id = pos.job_id
INNER JOIN position_fulfillments AS pf ON pf.position_id = pos.id
INNER JOIN parties ON parties.id = pf.party_id
WHERE jobs.title LIKE '%QA/QC%' OR parties.name LIKE '%QA/QC%';
This is the interpreted SQLAlchemy value of the query above:
query = (
PositionReportingStructure.query
.join(Position,
Position.id == PositionReportingStructure.reports_to)
.join(Job,
Job.id == Position.job_id)
.join(PositionFulfillment,
PositionFulfillment.position_id == Position.id)
.join(Party,
Party.id == PositionFulfillment.party_id)
.filter(db.or_(
Job.title.like('%QA/QC%'),
Party.name.like('%QA/QC%')))
)
I have a pagination decorator that wraps a function whose returning value is a BaseQuery. Inside the decorator, these are the code snippets that I've used:
query = f(*args, **kwargs)
page = 1 // just a dummy value
per_page = 5 // just a dummy value
paginate = query.paginate(page=page, per_page=per_page)
I'm expecting that it will return more or less 6 pages and 28 items in total, but it's not. The outcome is 1 page, 4 items per page, 4 total items and no previous/next pages which is incorrect. For further investigation, I have tried changing the value of page variable to compare each one:
page = 1 : 1 page, 4 items, 4 total items
page = 2 : 6 pages, 3 items, 28 total items
page = 3 : 6 pages, 2 items, 28 total items
page = 4 : 6 pages, 3 items, 28 total items
page = 5 : 6 pages, 5 items, 28 total items
page = 6 : 6 pages, 3 items, 28 total items
As you may notice, per page items is too inconsistent. Anybody who can explain what causes this contingency? I am currently using Flask-SQLAlchemy-2.0.
I already knew the root cause of this problem. My query returns some duplicate rows which I think is not acceptable to SQLAlchemy so in return automatically gives the distinct ones (even without using GROUP BY or DISTINCT).
As the context of my problem stated, I passed a count of 5 items in the per_page parameter of the paginate function, at the first page, it would only return 4 items since it groups the duplicate ones, thus, satisfies the paginate function's conditional statement (the one that has an emphasis):
**if page == 1 and len(items) < per_page:
total = len(items)**
else:
total = self.order_by(None).count()

Filtering on annotations with max date in Django

I have 3 models in Django-project:
class Hardware(models.Model):
inventory_number = models.IntegerField(unique=True,)
class Subdivision(models.Model):
name = models.CharField(max_length=50,)
class Relocation(models.Model):
hardware = models.ForeignKey('Hardware',)
subdivision = models.ForeignKey('Subdivision',)
relocation_date = models.DateField(verbose_name='Relocation Date', default=date.today())
Table 'Hardware_Relocation' with values for example:
id hardware subdivision relocation_date
1 1 1 01.01.2009
2 1 2 01.01.2010
3 1 1 01.01.2011
4 1 3 01.01.2012
5 1 3 01.01.2013
6 1 3 01.01.2014
7 1 3 01.01.2015 # Now hardware 1 located in subdivision 3 because relocation_date is max
I would like to write a filter to find hardwares in subdivisions on today.
I'm trying to write a filter:
subdivision = Subdivision.objects.get(pk=1)
hardware_list = Hardware.objects.annotate(relocation__relocation_date=Max('relocation__relocation_date')).filter(relocation__subdivision = subdivision)
Now hardware_list contains hardware 1, but it is wrong (because now hardware 1 in subdivision 3).
hardware_list must be None in this example.
The following code works wrong (hardware_list contains hardware 1, for subdivision 1).
limit_date = datetime.datetime.now()
q1 = Hardware.objects.filter(relocation__subdivision=subdivision, relocation__relocation_date__lte=limit_date)
q2 = q1.exclude(~Q(relocation__relocation_date__gt=F('relocation__relocation_date')), ~Q(relocation__subdivision=subdivision))
hardware_list = q2.distinct()
Maybe better use SQL?
This might work...
from django.db.models import F, Q
Hardware.objects
.filter(relocation__subdivision=target_subdivision, relocation__relocation_date__lte=limit_date)
.exclude(~Q(relocation__subdivision=target_subdivision), relocation__relocation_date__gt=F('relocation__relocation_date'))
.distinct()
The idea is, give me all hardware that have been relocated to target division before limit date, which DON'T have been relocated to other divisions after that.

Linear Programming: How to implement with multiple constraints?

I’m trying to solve a linear programing model and need some help. I’m not a programming expert, but I conceptually can draw up the problem and am hoping for some help implementing it.
I’m looking into an asset allocation problem for an investment portfolio from a theoretical perspective, but for simplicity of this post I’m going to use generic terms.
I have a list of 500+ choices that all have an assigned cost and value add. My goal is to maximize the sum of the value add, given a constraint on how much I can spend. These 500 choices are divided into 5 categories and there are restrictions on how many choices I can have from each category.
Category 1 = 1
Category 2 = 1
Category 3 = 2 or 3
Category 4 = 1 or 2
Category 5 = 2
Category 3 + Category 4 = 4
I figure I’ll need to use a binary X variable attached to each choice and 1 means I’m picking that choice and 0 doesn’t so in the end there should be 8 variables that have 1 and the rest have a 0 value that leads to the maximum value add given the constraints on cost each choice has.
I ultimately hope to be able to run and say for example “what is the nth highest value” so instead of getting the maximum value add I can get the second highest value add and so on.
Is this possible and what software/language would be best to do it? Thanks for your help!
Just to simplify writing everything down, let's assume you had 15 assets, with value added v_1, v_2, ..., v_15 and costs c_1, c_2, ..., c_15. Let's assume assets 1, 2, and 3 are in category 1, assets 4, 5, and 6 are in category 2, assets 7, 8, and 9 are in category 3, assets 10, 11, and 12 are in category 4, and assets 13, 14, and 15 are in category 5. Finally, let's assume a budget B.
We would create binary variables x_1, x_2, ..., x_15 to indicate whether we bought each asset. Now, the objective function of our integer program is:
max v_1*x_1 + v_2*x_2 + ... + v_15*x_15
Our budget constraint is:
c_1*x_1 + c_2*x_2 + ... + c_15*x_15 <= B
Exactly one choice from category 1:
x_1 + x_2 + x_3 = 1
Exactly one choice from category 2:
x_4 + x_5 + x_6 = 1
Either 2 or 3 choices from category 3:
x_7 + x_8 + x_9 >= 2
x_7 + x_8 + x_9 <= 3
Either 1 or 2 choices from category 4:
x_10 + x_11 + x_12 >= 1
x_10 + x_11 + x_12 <= 2
Exactly 2 choices from category 5:
x_13 + x_14 + x_15 = 2
Exactly 4 choices from categories 3 and 4 combined:
x_7 + x_8 + x_9 + x_10 + x_11 + x_12 = 4
Finally, you would specify all variables to be binary.
Note that the only adjustment you would need to your problem is to change the variables in each of these constraints to be the variables associated with each of your five categories.
All that remains would be to implement the model. There are a myriad of linear programming packages in all major languages; check out this survey for details. Since Stack Overflow is not a software recommendation site and you haven't really given any details about your situation (e.g. free vs. non-free solvers or the programming language you're using), I will refrain from suggesting a particular package.

cake php webservice to display recursive data

I have table with self_join
menus
id | menu_id | title | content
1 0 main1
2 0 main2
3 1 sub_1a content
4 1 sub_1b
5 4 sub_4a
6 5 sub_5a
7 6 sub_6a content
-------------------------------
I have 2 main menus with menu_id = 0
main1(id=1) has 2 sub menus with id 3 and 4
sub_1b(id=4) has 1 sub menu with id 5
sub_4a(id=5) has 1 sub menu with id 6
sub_5a(id=6) has 1 sub menu with id 7
this can be upto 5 levels
I have to display json recursive array
I have tried custom function in controller
public function menuz($id,$arr)
{
$arr[]=$id;
$id=$id-1;
if($id>0)
{
menuz($id,$arr);
}
}
I have called above function from index action
but it is giving error
what will be best aproach
If you're using cakephp then attach Tree behavior to your model (you'll have to add a couple of columns to your table, too)
then you can do
$data = $this->YourModel->find('threaded');
(see the manual abour it) to obtain a nested array that you can easily transform in json

Mongoid 4 .or not working in complex query

I know Mongoid 4 is still in beta and maybe I've found a bug, but I'm having a hard time understanding why the first query works and the second one returns nothing:
Product.or({sender_uid: params[:user_id]}, {receiver_uid: params[:user_id]})
Product.where({sender_uid: params[:user_id]}).or({receiver_uid: params[:user_id]})
It sort of making it hard to compose any complex queries, so any pointers would be appreciated.
See the following example:
Product 1: sender_uid = 1, receiver_uid = 2
Product 2: sender_uid = 2, receiver_uid = 1
Product 3: sender_uid = 1, receiver_uid = 2
params[:user_id] = 1
In the first query what you are getting is ALL the products where the sender_uid OR the receiver_uid is equal to 1. That is Product 1, 2 and 3.
In the second query you are querying all products where the sender_uid is 1. That is Product 1 and Product 3 and then (on that criteria), the products with receiver_id = 1. Neither the Product 1, not the Product 2 have a receiver with uid 1. So, that's why you're getting nothing. What you are doing in the second query is something like:
Product.where(sender_uid: params[:user_id]).where(receiver_uid: params[:user_id])
UPDATE:
Answering to a comment:
Product.or({ product_id: 1 }, { product_id: 2, sender_uid: 2 })
As you can see, the or method receive to Hashes of conditions. Each one is like a where query.