I've search this group and Google for a solution without any luck.
I can read (SELECT) data, but cannot change this database at all. To
get the data I run the following find:
$this->find('all',
array(
'fields' => array(
'ServiceData.stat_date',
'SUBSTR(ServiceData.stat_parameter,1,4) AS
"ServiceData.schema"',
'SUBSTR(ServiceData.stat_parameter,19,2) AS
"ServiceData.state"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service1\',
ServiceData.stat_value,0)) AS "ServiceData.service1"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service2\',
ServiceData.stat_value,0)) AS "ServiceData.service2"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service3\',
ServiceData.stat_value,0)) AS "ServiceData.service3"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service4\',
ServiceData.stat_value,0)) AS "ServiceData.service4"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service5\',
ServiceData.stat_value,0)) AS "ServiceData.service5"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service6\',
ServiceData.stat_value,0)) AS "ServiceData.service6"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service7\',
ServiceData.stat_value,0)) AS "ServiceData.service7"',
'SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), \'service8\',
ServiceData.stat_value,0)) AS "ServiceData.service8"'
),
'conditions' => array(
'SUBSTR(ServiceData.stat_parameter, 5, 13)' =>
'customer_lookup',
'SUBSTR(ServiceData.stat_parameter, 1, 4)' => $active_schema,
'ServiceData.stat_server LIKE' => 'PRODUCTION%',
'ServiceData.stat#' => 999,
'ServiceData.stat_date' => $load_date,
),
'group' => array(
'ServiceData.stat_date',
'SUBSTR(ServiceData.stat_parameter,1,4)',
'SUBSTR(ServiceData.stat_parameter,19,2)'
),
'order' => array('SUBSTR(ServiceData.stat_parameter,19,2)')
)
);
This is the resulting query:
SELECT
ServiceData.stat_date
, SUBSTR(ServiceData.stat_parameter,1,4) AS "ServiceData.schema"
, SUBSTR(ServiceData.stat_parameter,19,2) AS "ServiceData.state"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service1',
ServiceData.stat_value,0)) AS "ServiceData.service1"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service2',
ServiceData.stat_value,0)) AS "ServiceData.service2"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service3',
ServiceData.stat_value,0)) AS "ServiceData.service3"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service4',
ServiceData.stat_value,0)) AS "ServiceData.service4"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service5',
ServiceData.stat_value,0)) AS "ServiceData.service5"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service6',
ServiceData.stat_value,0)) AS "ServiceData.service6"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service7',
ServiceData.stat_value,0)) AS "ServiceData.service7"
, SUM(DECODE(SUBSTR(ServiceData.stat_parameter,22), 'service8',
ServiceData.stat_value,0)) AS "ServiceData.service8"
FROM
SERVICE_DATA ServiceData
WHERE
SUBSTR(ServiceData.stat_parameter, 5, 13) = 'customer_lookup'
AND SUBSTR(ServiceData.stat_parameter, 1, 4) = 'SCHEMA_A'
AND ServiceData.stat_server LIKE 'PRODUCTION%'
AND ServiceData.stat_number = 999
AND ServiceData.stat_date = TO_DATE('2010-02-17 09:00:02', '-MM-
DD HH24:MI:SS')
GROUP BY
ServiceData.stat_date
, SUBSTR(ServiceData.stat_parameter,1,4)
, SUBSTR(ServiceData.stat_parameter,19,2)
ORDER BY
SUBSTR(ServiceData.stat_parameter,19,2) ASC
This results in data returning in this form:
array(43) {
[0]=>
array(3) {
["ServiceData"]=>
array(4) {
["stat_date"]=>
string(19) "2010-02-17 09:00:02"
["schema"]=>
string(4) "SCHEMA_A"
["state"]=>
string(2) "()"
["statvalue"]=>
string(3) "116"
}
["SUM(DECODE(SUBSTR(ServiceData"]=>
array(1) {
["stat_parameter,22)"]=>
string(3) "107"
}
[0]=>
array(3) {
["'service1'"]=>
string(2) "40"
["'service2'"]=>
string(3) "116"
["'service3'"]=>
string(2) "17"
}
}
}
Notice the odd array keys "SUM(DECODE(SUBSTR(ServiceData" and
"'service1'" (this is the decode value in single ticks). Also the
value to service1 is the value that belongs in service2, service2 has
the value for service5, and service3 has the value for service8???
If I remove the SUM(DECODE(SUBSTR( fields, then everything works as
expected:
array(43) {
[0]=>
array(1) {
["ServiceData"]=>
array(3) {
["stat_date"]=>
string(19) "2010-02-17 09:00:02"
["schema"]=>
string(4) "SCHEMA_A"
["state"]=>
string(2) "()"
}
}
}
I have tried the afterFind solution posted by TekNoid, but it did not
work.
http://teknoid.wordpress.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/
Any hints or ideas on how I can overcome the issues