favorite
So I am currently building a system where the user inputs 'interest', 'monthly_deposit' and 'start_date'.
I then want to query the current value of 'FV'.
I need some help writing the query.
I was trying this out, but im getting lost. I think im very wrong, I just need some guidance here
SELECT ((I/C + 1) AND (N*C) + R) * EXP(SUM(COALESCE(LOG)(1)/(I/C)
My values
P = 0 (always)
I = 'interest'
C = 12 (always)
N = 'start_date'
R = 'monthly_deposit'
Try this query
SELECT (P*(POWER((1+(i DIV c)),(n * c)))) + ((R * (POWER((1+(i DIV c)),(n * c)) - 1)) DIV (i DIV c)) as fav
Related
I am trying to calculate the percentage of all of the numeric columns,from greeting_msg to ask_alt_number. I am able to get the correct percentage when there is only one record, but I cannot get the correct result as I add more records.
Code example
Query:
$sumGreeting = $dbcon->prepare(`SELECT SUM(greeting_msg + provide_agent_name + clear_and_aud + confirm_caller_name + confirm_caller_tel + ask_alt_number) AS SumGrt FROM table`);
$sumGreeting->execute();
Code:
$initRows = 0;
while ($row = $sumGreeting->fetch(PDO::FETCH_ASSOC))
{
$initRows = $row[`SumGrt`];
$result = ($initRows/12)*100;
echo $result.` %;
}
Any help will do.
Thanks in advance
You need a group by clause to get calculated sum per coach , agent_name ..., Right now you are using an aggregate function sum() in absence of group by so your whole table is treated as single group and aggregation is applied on all records, you need to group them by specifying your grouping columns in select and group by clause
SELECT coach, agent_name, reference, campaign_name
SUM(greeting_msg + provide_agent_name + clear_and_aud + confirm_caller_name + confirm_caller_tel + ask_alt_number) AS SumGrt
FROM table
GROUP BY coach, agent_name, reference, campaign_name
Also put your query inside single quotes in prepare('query') not in backticks
I try to get the records from opportunity that created manually, so it means it’s not created via Converted from Leads or other module.
I’m trying to do is to get the opportunity record that is not converted from leads.
Below, you will see my query using left join the leads to opportunity using
opportunity id from leads table and the opportunity id from Opportunity table.
But whenever I try to run this query it doesn’t show the records that created manually in the opportunity,
I just want to get the records that are not converted and created manually in the opportunities. May I know why it is not showing? Thanks guys.
$strQuery = " SELECT
*
FROM
leads l
LEFT JOIN
opportunities O
ON
l.opportunity_id = O.id
WHERE
l.deleted = '0'
AND
O.deleted = '0'
AND
l.converted = '0'
AND
DATE_FORMAT(O.date_created, '%y-%m-%d') = CURDATE()";
//
$hQuery = $db->query($strQuery);
//
while ( $arRow = $db->fetchByAssoc($hQuery) ){
// My logic
}
I just want to get the records that are not converted and created manually in the opportunities
You may need to reverse the table relationships, e.g.
SELECT *
FROM opportunities O
LEFT JOIN leads l ON O.id = l.opportunity_id
WHERE l.opportunity_id IS NULL
AND O.deleted = '0'
AND DATE_FORMAT(O.date_created, '%y-%m-%d') = CURDATE()
here there is no assocated "lead", just an "opportunity".
I have a Part Management system I've created in PHP with MySQL. What I'm trying to create is something that will generate the next Part Number for me. All part numbers start with a 3 letter prefix (which is determined by the product family/category) followed by their number.
For example 'ABC001'
What I have below is something that I'd like to use to determine what the next number is having already 'ABC001', 'ABC002' & 'ABC003' so I would like it to recognize what the next number is by querying until the query comes back false because that product number doesn't exist yet.
$abc_query = "SELECT * FROM products WHERE id LIKE 'ABC%'";
$abc_result = $mysqli2->query($abc_query);
while($row = $abc_result->fetch_assoc()) {
$rowid = $row["id"];
$pnumber = substr($rowid, 3, 3);
echo $pnumber. '<br/>';
$int = (int)$pnumber;
$abc_query2 = "SELECT * FROM products WHERE id 'ABC" . sprintf('%03s', $int);
for ($abc_query2 = true; $abc_query2 = false; $int++){
echo $int;
}$abc_nextnumber = $int +1;
}
$abc_newnumber = 'ABC' . sprintf('%03s', $abc_nextnumber);
echo $abc_newnumber;
The result I get is
001
002
003
005
ABC006
However the result should be..
001
002
003
ABC004
code update I've updated the code but it doesn't seem to stop at ABC004 if I have an 005. It will go to 006.
You should have the db do this instead of your app:
select t.id_prfx, max(t.id_num) as latest_num from
(select substring(id, 1, 3) as id_prfx,
cast(substring(id,4) as integer) as id_num) t
group by id_prfx
This will give you a result table where you get the highest part number for each prefix.
If you really really only want prefixes of 'ABC' then:
select max(cast(substring(id,4) as integer)) as max_num from table
where id LIKE 'ABC%'
Could you try this query?
SELECT MAX(SUBSTR(id, 4)) as last_id FROM products WHERE SUBSTR(id, 1, 3)='ABC'
EDİT:
products TABLE
==============
ABC001
ABC002
ABC003
ABC005
==============
We want to find 4 in products table.
SELECT SUBSTR(t1.id, 4) + 1 as POSSIBLE_MIN_ID
FROM products t1
WHERE NOT EXISTS (
SELECT *
FROM products t2
WHERE SUBSTR(id, 1, 3)='ABC' AND SUBSTR(t2.id, 4) = SUBSTR(t1.id, 4) + 1
) LIMIT 1
RESULT:
POSSIBLE_MIN_ID : 4
If anyone knows how I can have it add automatic zeros to the into the query (as it will be different amount of 0s once it gets to 'ABC011') instead of typing them in that would also be very helpful.
Here's how to automatically handle the prepended zeroes.
$sql3 = "SELECT * FROM products WHERE id 'ABC" . sprintf('%03s', $int);
I'm trying to display and sort an array by an average created using data from a database. I'm retrieving three variables from the database and creating an average from these values. This value is then placed inside a new array to be sorted along with the rest of the database data.
Am I right in thinking that having the SQL query inside the loop isn't a great idea? (Performance issue?)
Is there any alternative that's available? I've attached the code below:
^ database connection/query string to retrieve all data...
$result = $stmt_business_list->fetchAll(PDO::FETCH_ASSOC);
$items = array();
foreach($result as $row){
$single_business_id = $row['id'];
$name = $row['name'];
//Query to get ALL the service, value and quality ratings for certain business
$test_query = "SELECT * FROM rating WHERE business_id = $single_business_id";
$test_query_stmt = $dbh->prepare($test_query);
$test_query_stmt->execute();
$test_row = $test_query_stmt->fetchAll(PDO::FETCH_ASSOC);
$total_value = $total_quality = $total_service = 0;
foreach($test_row as $review)
{
$total_value += $review['value'];
$total_quality += $review['quality'];
$total_service += $review['service'];
}
$bayesian_value = (($set_site_average_review_count * $set_site_average_review_score) + $total_value) / ($set_site_average_review_count + $business_review_count);
$bayesian_quality = (($set_site_average_review_count * $set_site_average_review_score) + $total_quality) / ($set_site_average_review_count + $business_review_count);
$bayesian_service = (($set_site_average_review_count * $set_site_average_review_score) + $total_service) / ($set_site_average_review_count + $business_review_count);
$average_bayesian_rating = ($bayesian_value + $bayesian_quality + $bayesian_service) / 3;
$average_bayesian_rating = $average_bayesian_rating;
array_push($items, array(
"id"=>"$single_business_id",
"name"=>"$name",
"value"=>"$total_value",
"quality"=>"$total_quality",
"service"=>"$total_service",
"average"=>"$average_bayesian_rating"));
echo
'Name: '.$name.'<br>
Value: '.$total_value.'<br>
Quality: '.$total_quality.'<br>
Service: '.$total_service.'<br>
Average: '.$average_bayesian_rating.'<br><br>';
}
}
The page will be split up by a separate pagination script and will only display 6 objects at a time, but over time this may change so I do have an eye on performance as much as I can.
SQL aggregate queries are made for this kind of thing.
Use this query to summarize the results
SELECT b.name, b.id,
SUM(value) total_value,
SUM(quality) total_quality,
SUM(service) total_service,
COUNT(*) review_count,
avg_reviews_per_biz
FROM business b
JOIN ratings r ON b.id = r.business_id
JOIN (
SELECT COUNT(DISTINCT business_id) / COUNT(*) avg_reviews_per_biz
FROM ratings
) a ON 1=1
GROUP BY b.name, b.id, avg_review_per_biz
This will give you one row per business showing the summed ratings and the number of ratings. This result set will have the following columns
name business name
id business id
total_value sum of value ratings for that business
total_quality sum of quality ditto
total_service sum of service ditto
review_count number of reviews for business "id"
avg_reviews_per_biz avg number of reviews per business
The last column has the same value for all rows of your query.
You can then loop over these row one business at a time doing your statistical computations.
I can't tell from your question where you're getting variables like $set_site_average_review_count, so I can't help with those computations.
You'll find that SQL aggregate querying is very powerful indeed.
I would like to do a multiple select in mysql but have not had any joy. I have tried following examples on here and around the web but they do not seam to fit what I am trying to do.
My select statement is as follows
SELECT a.* FROM Calendar a
WHERE a.CalendarId = 256 AND a.Private = 0
UNION
SELECT b.* FROM Calendar b
WHERE b.CalendarId = 256 AND b.Private = 1 AND b.PrivateId = 11
To explain what I want above, I would like to return all Calendar rows that have a Private value of 0.
I would also like to filter this by selecting only those that have Private = 1 that have the PrivateId = 11 (in this example).
Thanks
Try :
SELECT *
FROM Calendar
WHERE CalendarId = 256
AND (Private = 0 OR (Private = 1 AND PrivateId = 11))