Zend query sort by multiplication of columns - database

I am trying to query some rows from a MySQL database with Zend Framework 1. I need all columns, but want to sort them by a multiplication of columns:
$select = $this
->select()
->where('start_date < ' . $currentTime)
->where('end_date >' . $currentTime)
->order('columnA * columnB DESC');
This obviously isn't working.
With the Zend documentation, I'm getting to this:
$select = $this->select()
->from(array('p' => 'products'),
array('product_id',
'order_column' =>
new Zend_Db_Expr('columnA * columnB'))
)
->order('order_column DESC);
However, this only returns the product_id and new order_column, but I need all columns.
How to get there? How to return all columns of the selected rows, ordered by columnA * columnB?

Too quick. I found the solution by trying:
$select = $this
->select()
->where('start_date < ' . $currentTime)
->where('end_date >' . $currentTime)
->order(new Zend_Db_Expr('columnA * columnB DESC'));
This gives the desired result.

Related

POWER BI - DAX - Measure filter

I have a dax measure . This measure have 1 data . This is "GOOGLE";"YOUTUBE";"AMAZON"
I want to use this 1 line string result in FILTER.
CALCULATE(SUM(_TABLE);_TABLE.COMPANIESNAME; FILTER(_TABLE.COMPANIESNAME IN { mymeasure } ))
Does anyone can help me solve this problem ?
Thank you for help
There are probably way better ways to do what you want. You are treating Power BI like a relational database when you should be using it like a Star Schema. But without more info, I'm just going to answer the question.
Here's my sample table:
// Table
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsxNrMrPU9JRMlSK1YlWcs/PT89JBXKNwNzI/NKQ0iQQ3xjMd0tMTk3Kz88GCpgoxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Company = _t, Count = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Count", Int64.Type}})
in
#"Changed Type"
I don't have your DAX measure or its name, so I'm using this:
CompanyList = """Google"";""YouTube"";""Amazon"""
Just to prove it's the same as your measure, here it is in the report:
From this post I created a DAX formula that will parse your DAX value into a table with one row for each company name. Add this as a DAX table from Modeling > New Table. I named mine "Filtered table".
Filtered table = VAR CommaSeparatedList = [CompanyList]
VAR BarSeparatedList =
SUBSTITUTE ( CommaSeparatedList, ";", "|" )
VAR Length =
PATHLENGTH ( BarSeparatedList )
VAR Result =
SELECTCOLUMNS (
GENERATESERIES ( 1, Length ),
"Company", SUBSTITUTE( PATHITEM ( BarSeparatedList, [Value] ), """", "")
)
RETURN
Result
Here's what the table looks like:
Add a relationship between the two tables like this (Modeling > Manage relationships > New...):
Then add a DAX column to the filtered table by selecting the table and then Modeling > New Column
Count = CALCULATE(SUM('Table'[Count]))
You can total it up with this DAX measure:
Filtered total = SUM('Filtered table'[Count])
Change the CompanyList measure, and result will update:

Unknown column 'value' in 'where clause'

I have reviewed every post and tried a few recommendations but nothing is working. My code has been fine -- but Gravity Forms did an update and messed up the database tables the data was pulling from so I edited that but the data is still not pulling properly and I am getting the error: Unknown column 'value' in 'where clause' when I turn on debug.
Can anyone see why I am getting this error? The page should be retrieving a list of team members based on the value they have entered in a graavity form field.....all was fine until the update.
Here is the code that is causing the issues - specifically the $get_team_ids and $user_email lines seem to be the offenders.
function showstep1(){
$concate='';
$postid = get_the_ID();
$post_7 = get_post($postid);
if( is_mr() ) {
//print_r($post_7->post_title);
}
//echo $postid;
global $wpdb;
$table_name = $wpdb->prefix . "walking_steps";
$table_name1 = $wpdb->prefix . "users";
$table_name2 = $wpdb->prefix . "gf_entry_meta";
$get_team_ids = $wpdb->get_results("SELECT * FROM $table_name2 where value = '" .$post_7->post_title. "'");
if( is_mr() ) {
//print_r($get_team_ids);
//print_r( $getuserids );
}
$users = array();
if(!empty($get_team_ids)){
foreach ($get_team_ids as $idlead) {
$user_email = $wpdb->get_var("SELECT value FROM wp_gf_entry_meta WHERE form_id=4 AND lead_id=$idlead->lead_id AND field_number=3");
if( empty( $user_email ) ) {
$user_email = $wpdb->get_var("SELECT value FROM wp_gf_entry_meta WHERE form_id=1 AND lead_id=$idlead->lead_id AND field_number=2");
}
if( $user_email ) $users[] = $user_email;
//$concate .= $idlead->lead_id.',';
}
$concate = substr($concate, 0,-1);
//echo $concate;
}
Unknown column 'value' in 'WHERE clause' would usually indicate that the column does not exist in the table.
You say that this is also happening for the other line 'user_email' which also is trying to SELECT the column 'value' again, if this causing you a problem, the column name is not correct it would seem.
Is it the correct header for the column? Seems like an ambiguous name. Try renaming the column 'value' to something less generic or change the where clause to
"WHERE table_name2.value = "
If this is not the column header, please replace 'value' with the correct column header
EDIT: Correct Code below as per comments
Replace all instances of 'value' with 'meta_value'

Print SQL query of ORM query builder in cakephp3

How to print ORM query
$query = $articles->find('all')->contain(['Comments']);
For example print =>
SELECT * FROM comments WHERE article_id IN (comments);
Wrapping your ORM query result with the debug function will show the SQL and bound params:
debug($query);
You can also similarly look at the query results with the debug function.See CakePHP 3: retrieving data and result sets — Debugging Queries and ResultSets
what about $query->sql()?
$qb = $this->Person->find()->select(["id", "text" => "concat(Name,' ',Family)"])
->where(['id >' => 0])
->where($query ? ["OR" => $filters] : null)
->limit(10);
dd($qb->sql());
and result:
.../src/Controller/ClientController.php (line 86)
'SELECT Person.id AS `Person__id`, concat(Name,' ',Family) AS `text` FROM person Person WHERE (id > :c0 AND (Family like '%sam%' OR Name like '%sam%' OR Family like '%sam%' OR Name like '%sam%')) LIMIT 10'
I prefer this:
public function __debugInfo()
{
return [
'query' => $this->_query,
'items' => $this->toArray(),
];
}
// Print the query
debug($query->__debugInfo()['sql']);
// Prints this
SELECT * FROM comments WHERE article_id IN (comments);

CakePHP count query gives different result when run in phpmyadmin

I am running the following query on my database:-
SELECT COUNT(*) AS COUNT, `Doctor`.`device_type` FROM `doctors` AS `Doctor` WHERE 1 = 1 GROUP BY `Doctor`.`device_type`
and it gives the result:-
count device_type
47 Android
23 iPhone
Whereas when running this query as a CakePHP query it gives the result as '2':-
$this->Doctor->find('count',array('group'=>'Doctor.device_type'));
Can anyone please suggest why this is happening?
The CakePHP result is correct as it is returning a count of the number of results returned by your query. In your case you have 2 rows: 'Android' and 'iPhone'.
find('count') always returns an integer, not an array. What Cake is doing is basically this:-
$data = $this->Doctor->find('all', array('group' => 'Doctor.device_type'));
$count = count($data); // This is what find('count') will return.
You need to do something like the following instead:-
$data = $this->Doctor->find('all', array(
'fields' => array(
'Doctor.device_type',
'COUNT(Doctor.*) AS count'
)
'group' => 'Doctor.device_type'
));

CakePHP : Search date from form helper

I use form helpers to show drop down select date. My problem is, i can't compare date from ms sql with form data.
My View :
<?php echo $this->Form->input('collect_date', array('label' => 'Collect Date','type'=>'date','dateFormat'=> 'DMY','minYear' => date('Y'),'maxYear' => date('Y')+1));?>
My Controller
$status =array(0,2,4);
$find_date = $this->data['Transaction']['collect_date'];
$field = array('Transaction.status','Catalogue.title', 'Catalogue.author', 'Catalogue.isbn', 'Location.rack');
$condition = array('OR' => array('AND' =>array('Transaction.return_date <'=> $find_date,'Transaction.status '=> '3'),array('Transaction.status'=> $status)));
$data = $this->Catalogue->Transaction->find('count',array('fields' => $field,'conditions'=>$condition,'order'=>array('Transaction.id desc') ));
$this->set('Found', $data);
And the sql dump
SELECT COUNT(*) AS [count] FROM [transactions] AS [Transaction]
LEFT JOIN [catalogues] AS [Catalogue] ON ([Transaction].[catalogue_id] = [Catalogue].[id])
LEFT JOIN [users] AS [User] ON ([Transaction].[user_id] = [User].[id])
WHERE (((([Transaction].[return_date] < ('01', '09', 2013))
AND ([Transaction].[status] = 3))) OR ([Transaction].[status] IN (0, 2, 4)))
As you can see the date format ('01', '09', 2013). But when try to convert use this
'Transaction.return_date <'=> date('Y-m-d', strtotime($find_date))
it show error:
Warning (2): strtotime() expects parameter 1 to be string, array given [APP\Controller\CataloguesController.php, line 66]
and the sql show:
[Transaction].[return_date] < '1970-01-01'
You could use:
$date = DateTime::createFromFormat('d/m/y', implode('/', $find_date));
'Transaction.return_date <'=> $date->format('Y-m-d');
Edit:
I think the way its meant to be 'flattened' is with
$this->Model->deconstruct('find_date', $find_date);
However, last time i tried to use this, i couldn't get it to work properly, so i went with the above.
http://api21.cakephp.org/class/model#method-Modeldeconstruct
Try converting return date using UNIX_TIMESTAMP in sql query and then comparing it with
strtotime($find_date).

Resources