I am creating graph for display month wise count from Mysql database.
Execute this following query:
$cash_query = $this->db->query("select COUNT(*) as count_monthwise, MONTH(FROM_UNIXTIME(dt_added)) as month from `order` where user_id='1' and status != '9' and payment_type = '1' GROUP BY month");
$cash_result = $cash_query->result();
Output:
Array
(
[0] => stdClass Object
(
[count_monthwise] => 1
[month] => 8
)
[1] => stdClass Object
(
[count_monthwise] => 2
[month] => 9
)
)
In above output, there are display "count_monthwise" means count and month "8" means "8th month - August".
But i want to display output with all months, if find count are 0 in any months, then display [count_monthwise] => 0.
I want to display Exact output like:
Array
(
[0] => stdClass Object
(
[count_monthwise] => 1
[month] => 1
)
[1] => stdClass Object
(
[count_monthwise] => 1
[month] => 2
)
.
.
.
.
.
.
.
[10] => stdClass Object
(
[count_monthwise] => 0
[month] => 11
)
[11] => stdClass Object
(
[count_monthwise] => 0
[month] => 12
)
)
I have used using foreach loop something like this, but this is not working.
Loop
foreach($cash_result as $cash => $cash_value){
for($i=0;$i<=11;$i++){
if($i == $cash){
}
}
}
That could be the one way around:
SELECT
COALESCE(yourQuery.count_monthwise,0) AS monthwise_count,
allMonths.month
(SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12 ) AS allMonths
LEFT JOIN
(
SELECT
COUNT(*) as count_monthwise,
MONTH(FROM_UNIXTIME(dt_added)) as month
from `order`
where user_id='1' and status != '9' and payment_type = '1'
GROUP BY month
) AS yourQuery
ON allMonths.month = yourQuery.month
use below way after getting the result from database don't do complex query just do some code twist
$cash_result = $cash_query->result_array(); // make it to array
$month = range(1,12); // month array
$new_array = array();
$counta = count($month);
for($i=0; $i<$counta ; $i++){
foreach($arr as $key=>$row){
if(($month[$i] == $row['month'])){
$new_array[$i] = $arr[$key];
break;
}else{
$new_array[$i] = array('count_monthwise' => 0,'month' => $month[$i]);
}
}
}
echo '<pre>'; print_r($new_array);
I have two table: imports and orders: I have attached these respectively.
I want the followings:
1. sum amount of same product_id in imports table
2. sum of pieces of same product_id in orders table as status wise.
my query is:
SELECT `Import`.*,
SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id`
and result for this query:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 50
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 2
[category_id] => 2
[product_id] => 2
[amount] => 15
[cost] => 3000
[comment] =>
[created] => 2015-06-22 18:10:36
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
My expected result is:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 65
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
How can I do this? I tried in different way such as:
SELECT `Import`.*, SUM(`Import`.`amount`) as total_import,
SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id`
But no luck :'(
I am guessing that you want information about each product. If so, you can do this in a few ways. Here is a union all method:
SELECT product_id, sum(amount) as total_import,
sum( case when status = 'sold' THEN pieces else 0 end) as total_sell,
sum( case when status = 'No contact' THEN pieces else 0 end) as no_contact,
sum( case when status = 'confirmed' THEN pieces else 0 end) as confirmed,
sum( case when status = 'canceled' THEN pieces else 0 end) as canceled
from ((select i.product_id, amount, NULL as status, NULL as pieces
from `amrajegeachi`.`imports` i
) union all
(select o.product_id, NULL, o.status, o.pieces
from `orders` o
)
) io
group by product_id;
SELECT `Import`.*, SUM(`Import`.`amount`) as total_import,
SUM( case when orders.status = "sold" THEN orders.pieces else 0 end) as total_sell,
SUM( case when orders.status = "No contact" THEN orders.pieces else 0 end) as no_contact,
SUM( case when orders.status = "confirmed" THEN orders.pieces else 0 end) as confirmed,
SUM( case when orders.status = "canceled" THEN orders.pieces else 0 end) as canceled
FROM `amrajegeachi`.`imports` AS `Import`
LEFT JOIN `orders`
ON `Import`.`product_id` = `orders`.`product_id`
WHERE 1 = 1
GROUP BY `Import`.`id
I have the following mysql query:
SELECT p.id, p.title, p.description, a.id, a.answer, q.id, q.question
FROM x_trybe_poll_relation pr
LEFT JOIN x_polls p
ON pr.poll_id = p.id
LEFT JOIN x_polls_question_relation qr
ON p.id = qr.poll_id
LEFT JOIN x_polls_questions q
ON qr.question_id = q.id
LEFT JOIN x_polls_answers a
ON q.id = a.question_id
WHERE pr.trybe_id = '%s'
Which returns:
[polls] => Array
(
[0] => Array
(
[id] => 79de13af1978ab14f56b4305956b2d93cd42e0fb
[title] => Poll 1
[description] => Poll 1 Description
[answer] => Danny Welbeck
[question] => Who is the best player in the Arsenal squad?
)
[1] => Array
(
[id] => 79de13af1978ab14f56b4305956b2d93cd42e0fb
[title] => Poll 1
[description] => Poll 1 Description
[answer] => Alexis Sanchez
[question] => Who is the best player in the Arsenal squad?
)
)
How do I adjust the mysql query to get the following:
[polls] => Array
(
[0] => Array
(
[id] => 79de13af1978ab14f56b4305956b2d93cd42e0fb
[title] => Poll 1
[description] => Poll 1 Description
[answers] => Array (
[0] => Array (
[id] => abc
[answer] => Danny Welbeck
)
[1] => Array (
[id] => def
[answer] => Alexis Sanchez
)
)
[question] => Who is the best player in the Arsenal squad?
)
)
I want to get the poll question, and get the array of pre-generated answers with one query.
Is this possible, or should I just run two queries?
Thanks
Looking to get a total for each ID from the sum of the replacement fields.
SELECT
insurance_carrier as ID, SUM(REPLACE(REPLACE(REPLACE(es_reserve,'$',''),',',''),'-','')) AS es_reserve,
SUM(REPLACE(REPLACE(REPLACE(structure_reserve,'$',''),',',''),'-',''))AS structure_reserve,
SUM(es_reserve+structure_reserve) AS total
FROM job_tbl
WHERE
job_status NOT IN(2,4,6,7,9) AND
insurance_carrier !=0 AND
FROM_UNIXTIME(date_of_loss,'%m') = MONTH(NOW()) AND
FROM_UNIXTIME(date_of_loss,'%Y') = YEAR(NOW())
GROUP BY insurance_carrier
I get the results from es_reserve and structure_reserve but the total is 0.
BTW the fields contain items like $2,300- that's the reason for the replace
Any help would be appreciated!!!
EDIT: here is the results this produces
Array
(
[ID] => 14
[es_reserve] => 5000
[structure_reserve] => 35000
)
Array
(
[ID] => 15
[es_reserve] => 2500
[structure_reserve] => 2500
)
Array
(
[ID] => 41
[es_reserve] => 2500
[structure_reserve] => 2500
)
Array
(
[ID] => 44
[es_reserve] => 2500
[structure_reserve] =>
)
Here is what I would like it to produce
Array
(
[ID] => 14
[es_reserve] => 5000
[structure_reserve] => 35000
[total] => 40000
)
Array
(
[ID] => 15
[es_reserve] => 2500
[structure_reserve] => 2500
[total] => 5000
)
Array
(
[ID] => 41
[es_reserve] => 2500
[structure_reserve] => 2500
[total] => 5000
)
Array
(
[ID] => 44
[es_reserve] => 2500
[structure_reserve] =>
[total] => 2500
)
Total column is doing sum on original column value , the alias name defined in the select won't be used in the same select.
You can repeat replace statement while doing total column computation
SUM( REPLACE(REPLACE(REPLACE(es_reserve,'$',''),',',''),'-','')
+ REPLACE(REPLACE(REPLACE(structure_reserve,'$',''),',',''),'-',''))
Total,
instead of
SUM(es_reserve+structure_reserve) as total
The query becomes, with order by as asked in comment.
SELECT insurance_carrier as ID, SUM(REPLACE(REPLACE(REPLACE(es_reserve,'$',''),',',''),'-','')) AS es_reserve, SUM(REPLACE(REPLACE(REPLACE(structure_reserve,'$',''),',',''),'-',''))AS structure_reserve, SUM( REPLACE(REPLACE(REPLACE(es_reserve,'$',''),',',''),'-','') + REPLACE(REPLACE(REPLACE(structure_reserve,'$',''),',',''),'-','')) as Total FROM job_tbl WHERE job_status NOT IN(2,4,6,7,9) AND insurance_carrier !=0 AND FROM_UNIXTIME(date_of_loss,'%m') = MONTH(NOW()) AND FROM_UNIXTIME(date_of_loss,'%Y') = YEAR(NOW()) GROUP BY insurance_carrier
order by SUM( REPLACE(REPLACE(REPLACE(es_reserve,'$',''),',',''),'-','') + REPLACE(REPLACE(REPLACE(structure_reserve,'$',''),',',''),'-','')) desc
OR
use it as subquery
SELECT T.*, SUM(es_reserve+structure_reserve) AS total
FROM
(
SELECT
insurance_carrier as ID, SUM(REPLACE(REPLACE(REPLACE(es_reserve,'$',''),',',''),'-','')) AS es_reserve,
SUM(REPLACE(REPLACE(REPLACE(structure_reserve,'$',''),',',''),'-',''))AS structure_reserve
FROM job_tbl
WHERE
job_status NOT IN(2,4,6,7,9) AND
insurance_carrier !=0 AND
FROM_UNIXTIME(date_of_loss,'%m') = MONTH(NOW()) AND
FROM_UNIXTIME(date_of_loss,'%Y') = YEAR(NOW())
GROUP BY insurance_carrier
) T
Here is my query (I replaced table names with generic ones). I am trying to do a union all on two different queries in order to group them all by date so that results with similar dates come out as one row.
I am getting the "Every derived table must have its own alias" error when attempting to execute. What am I typing in wrong?
I have researched this but couldn't find the answer. Every selected field has an alias? Or is the issue in the first SELECT?
SELECT SUM(val), id, dat, title FROM (
SELECT table1.product_id as id, SUM(table1.qty) as val, DATE_FORMAT(table1.created, '%Y-%m-1') as dat, table2.title as title
FROM table1
LEFT JOIN table3 ON table1.event_id = table3.id
LEFT JOIN table2 ON table1.product_id = table2.id
WHERE table1.user_id = $user_id AND table3.active != 2 AND table3.temp = 0 AND table2.active != 2
GROUP BY dat
UNION ALL
SELECT table4.product_id as id, SUM(table4.qty) as val, DATE_FORMAT(table4.created, '%Y-%m-1') as dat, table2.title as title
FROM table4
LEFT JOIN table5 ON table4.festival_id = table5.id
LEFT JOIN table2 ON table4.product_id = table2.id
WHERE table4.user_id = $user_id AND table5.active != 2 AND table2.active != 2
GROUP BY dat
)
GROUP BY id
ORDER BY dat ASC
Here is what I am attempting to do:
My original result:
Array
(
[0] => stdClass Object
(
[id] => 1
[val] => 1
[dat] => 2012-05-1
[title] => Test Product
)
[1] => stdClass Object
(
[id] => 1
[val] => 8
[dat] => 2012-06-1
[title] => Test Product
)
[2] => stdClass Object
(
[id] => 2
[val] => 4
[dat] => 2012-06-1
[title] => Test Product 2
)
[3] => stdClass Object
(
[id] => 3
[val] => 6
[dat] => 2012-06-1
[title] => Test Product 3
)
[4] => stdClass Object
(
[id] => 1
[val] => 10
[dat] => 2012-05-1
[title] => Test Product
)
[5] => stdClass Object
(
[id] => 1
[val] => 8
[dat] => 2012-06-1
[title] => Test Product
)
[6] => stdClass Object
(
[id] => 2
[val] => 3
[dat] => 2012-06-1
[title] => Test Product 2
)
[7] => stdClass Object
(
[id] => 3
[val] => 3
[dat] => 2012-06-1
[title] => Test Product 3
)
)
So if they have a similar date and ID, I need those to be just one result. Like so:
Array
(
[0] => stdClass Object
(
[id] => 1
[val] => 11
[dat] => 2012-05-1
[title] => Test Product
)
[1] => stdClass Object
(
[id] => 1
[val] => 8
[dat] => 2012-06-1
[title] => Test Product
)
[2] => stdClass Object
(
[id] => 2
[val] => 7
[dat] => 2012-06-1
[title] => Test Product 2
)
[3] => stdClass Object
(
[id] => 3
[val] => 9
[dat] => 2012-06-1
[title] => Test Product 3
)
)
Please let me know if you need anything else. Thanks in advance.
Try this:
SELECT SUM(val), id, dat, title FROM (
SELECT table1.product_id as id, SUM(table1.qty) as val, DATE_FORMAT(table1.created, '%Y-%m-1') as dat, table2.title as title
FROM table1
LEFT JOIN table3 ON table1.event_id = table3.id
LEFT JOIN table2 ON table1.product_id = table2.id
WHERE table1.user_id = $user_id AND table3.active != 2 AND table3.temp = 0 AND table2.active != 2
GROUP BY dat
UNION ALL
SELECT table4.product_id as id, SUM(table4.qty) as val, DATE_FORMAT(table4.created, '%Y-%m-1') as dat, table2.title as title
FROM table4
LEFT JOIN table5 ON table4.festival_id = table5.id
LEFT JOIN table2 ON table4.product_id = table2.id
WHERE table4.user_id = $user_id AND table5.active != 2 AND table2.active != 2
GROUP BY dat
) AS t
GROUP BY id, dat
ORDER BY dat ASC
As the error suggests, every view/derived table must have an alias..
Edit: This will give you records with distinct id/dat pair. Seems this is what is you are after.