How to get sum for different entry in mysql - mysql

I have table in mysql like
| service_code | charges | caller_number | duration | minutes |
+--------------+---------+---------------+----------+---------+
| 10 | 15 | 8281490235 | 00:00:00 | 1.0000 |
| 11 | 12 | 9961621709 | 00:00:00 | 0.0000 |
| 10 | 15 | 8281490235 | 01:00:44 | 60.7333 |
| 11 | 2 | 9744944316 | 01:00:44 | 60.7333 |
+--------------+---------+---------------+----------+---------+
from this table I want to get charges*minutes for each separate caller_number.
I have done like this
SELECT sum(charges*minutes) as cost from t8_m4_bill groupby caller_number
but I am not getting expected output. Please help?

SELECT caller_number,sum(charges*minutes) as cost
from t8_m4_bill
group by caller_number
order by caller_number

Related

MySql Average and Group By Usage

Report Table
+----+-------------------+--------+---------------------+
| id | mac_address |quantity| zaman |
+----+-------------------+--------+---------------------+
| 12 | 26-C0-B7-1E-7A-8C | 001 | 2017-12-07 10:22:09 |
| 14 | 26-C0-B7-1E-7A-8C | 001 | 2017-12-07 10:28:44 |
| 15 | 26-C0-B7-1E-7A-8D | 001 | 2017-12-07 10:44:54 |
| 19 | 26-C0-B7-1E-7A-AC | 001 | 2017-12-08 10:11:00 |
| 20 | 26-C0-B7-1E-7A-AD | 002 | 2017-12-08 10:32:12 |
| 24 | 26-C0-B7-1E-7A-8D | 001 | 2017-12-09 10:29:54 |
| 25 | 26-C0-B7-1E-7A-8E | 002 | 2017-12-09 10:39:11 |
I want to find average of quantity between days according to this equation:
Sum of quantity(9)/Number of day(3) = 3
So far I have this query:
SELECT ROUND(AVG(quantity),2) AS quantity, DATE(zaman) as DateOnly
FROM report WHERE DATE(zaman) BETWEEN ? AND ?
GROUP BY DateOnly
How can I achieve this?
try using sum of the quantity divided by number of days
select sum(quantity) /( datediff(max(zaman), min(zaman))) as quantity
FROM report WHERE DATE(zaman) BETWEEN ? AND ?
Try this:
SELECT ROUND((SUM(Quantity)*1.0)/datediff(MAX(Zaman), MIN(Zaman)),0)
FROM report
WHERE zaman BETWEEN ? AND ?

How to do a ORDER BY inside a INNER JOIN to get last status update?

I need to get records of 2 tables using filters and sorting.
Tables: ra_pedidos, ra_pedido_has_pedido_status
It worked perfectly in MySQL5.0, but now in 5.5 it does not bring the correct result anymore.
I need in the same line to bring the date of the first status and last change of status of each request and ignore the canceled ones.
Example search (ra_pedido_has_pedido_status):
SELECT id_pedido, id_pedido_status, data
FROM ra_pedido_has_pedido_status
WHERE id_pedido IN (86291, 86745, 86622);
Result:
+-----------+------------------+---------------------+
| id_pedido | id_pedido_status | data |
+-----------+------------------+---------------------+
| 86291 | 1 | 2017-10-30 11:16:12 |
| 86291 | 2 | 2017-10-30 14:14:53 |
| 86291 | 3 | 2017-10-31 08:18:47 |
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86622 | 2 | 2017-11-04 12:47:33 |
| 86622 | 3 | 2017-11-06 08:24:20 |
| 86622 | 90 | 2017-11-07 08:40:55 |
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-----------+------------------+---------------------+
Now report SQL:
[EDIT] Explain: JOIN get first match row, then the ORDER BY on SELECT inside this join force to get last row. Note that id_pedido 86622 should not list because it is canceled (last id_pedido_status = 90), and this is filtered in HAVING
SELECT o.id_pedido AS id, pus.id_pedido_status, pus.data
FROM ra_pedido AS o
INNER JOIN
(
SELECT ra_pedido_has_pedido_status.*
FROM ra_pedido_has_pedido_status
ORDER BY data DESC
) AS pus ON o.id_pedido = pus.id_pedido
WHERE o.id_pedido IN (86291, 86745, 86622)
GROUP BY o.id_pedido
HAVING id_pedido_status < 90
ORDER BY data DESC;
This is the result using MySQL 5.0, the expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86291 | 11 | 2017-11-07 12:08:04 |
| 86745 | 2 | 2017-11-07 11:09:46 |
+-------+------------------+---------------------+
And this is the result using MySQL 5.5, not expected:
+-------+------------------+---------------------+
| id | id_pedido_status | data |
+-------+------------------+---------------------+
| 86745 | 1 | 2017-11-07 10:59:51 |
| 86622 | 1 | 2017-11-04 12:23:21 |
| 86291 | 1 | 2017-10-30 11:16:12 |
+-------+------------------+---------------------+
What do I need to change in SQL?
Thanks and sorry my english...

Group condition in last data mysql

I have a data like this :
Table LOT
+-------+--------+
|Lot_id | Prod_id|
+-------+--------+
| LOT-1 | Prd-1 |
| LOT-1 | Prd-2 |
| LOT-1 | Prd-3 |
| LOT-2 | Prd-4 |
+-------+--------+
Table Process
+-------+--------+--------+------------+----------+
|proc_id|proc_cat|proc_seq|proc_prod_id|t_proc_qty|
+-------+--------+--------+------------+----------+
| 1 | Proc-A | 1 | Prd-1 | 100 |
| 2 | Proc-H | 2 | Prd-1 | 100 |
| 3 | Proc-D | 3 | Prd-1 | 100 |
| 4 | Proc-A | 1 | Prd-2 | 100 |
| 5 | Proc-H | 2 | Prd-2 | 100 |
| 6 | Proc-D | 3 | Prd-2 | 20 |
| 7 | Proc-Q | 4 | Prd-2 | 20 |
| 8 | Proc-A | 1 | Prd-3 | 100 |
| 9 | Proc-H | 2 | Prd-3 | 100 |
| 10 | Proc-D | 3 | Prd-3 | 50 |
| 11 | Proc-O | 1 | Prd-4 | 80 |
| 12 | Proc-F | 2 | Prd-4 | 80 |
| 13 | Proc-H | 3 | Prd-4 | 80 |
+-------+--------+--------+------------+----------+
And i want data like this if i want select just LOT=LOT-1.
table LOT joined to table Process and data is accumulated sum(t_proc_qty) from last proc_seq each proc_prod_id and group by proc_cat and order by proc_seq
+--------+--------+----------+
|proc_cat|proc_seq|t_proc_qty|
+--------+--------+----------+
| Proc-D | 3 | 150 |->accumulated from Prd-1 and prd-3 in last process is seq 3
| Proc-Q | 4 | 20 |->accumulated from Prd-2 in last process is seq 4
+--------+--------+----------+
What queries I use in MySQL ?
I stucked in query
SELECT proc_cat, proc_seq, SUM(t_proc_qty)
FROM Process
LEFT JOIN Lot ON proc_prod_id=Prod_id
WHERE Lot_id='LOT-1'
GROUP BY proc_prod_id
ORDER BY proc_seq DESC LIMIT 1
this schema for trial query SQLFiddle
From table Process you want the records with the highest proc_id per proc_prod_id:
select *
from process
where not exists
(
select *
from process later
where later.proc_prod_id = process.proc_prod_id
and later.proc_id > process.proc_id
);
From this data you want an aggregate per proc_cat and proc_sec. And you also want to consider only prod_id for 'LOT-1' in table LOT.
The complete query:
select proc_cat, proc_seq, sum(t_proc_qty)
from process
where proc_prod_id in (select prod_id from lot where lot_id = 'LOT-1')
and not exists
(
select *
from process later
where later.proc_prod_id = process.proc_prod_id
and later.proc_id > process.proc_id
)
group by proc_cat, proc_seq
order by proc_cat, proc_seq;
SQL fiddle: http://sqlfiddle.com/#!9/1fa3fd/5

Displaying records from database by quarter in mysql

I have a table named nca_totals.
Table: nca_totals
+----------+-----------+------------+--------------+
| total_id | nca_total | nca_date | account_type |
+----------+-----------+------------+--------------+
| 13 | 10450 | 2015-01-21 | DBP-TRUST |
| 14 | 5000 | 2015-02-05 | DBP-TRUST |
| 15 | 7000 | 2015-04-02 | DBP-TRUST |
| 16 | 4000 | 2015-05-02 | DBP-TRUST |
+----------+-----------+------------+--------------+
Now I want to display all the data by quarter base on its date. Let's say I want to display all the records who belong to 1st Quarter like this:
+----------+-----------+------------+--------------+
| total_id | nca_total | nca_date | account_type |
+----------+-----------+------------+--------------+
| 13 | 10450 | 2015-01-21 | DBP-TRUST |
| 14 | 5000 | 2015-02-05 | DBP-TRUST |
+----------+-----------+------------+--------------+
This date belongs to the 1st quarter of the year (Jan, Feb, March). I only have this query to select the date and return its quarter number as:
SELECT QUARTER('2015-01-11'); /* returns 1 */
How can I combine that query to display all the records by quarter ? Can anyone help ? Thanks.
select *
from nca_totals
where QUARTER(nca_date) = 1
SELECT
CEIL(MONTH(`nca_date`) / 3) AS `quarter`
FROM `nca_totals`;

Listing results with unique column

I want to list top 6 race records with unique holder only. I mean a holder gets in the list shouldn't be listed with his another record. I currently use the query below to list top 6 times.
mysql> select * from racerecords order by record_time asc, date asc;
+----+---------+------------+-------------+---------------------+----------+
| id | race_id | holder | record_time | date | position |
+----+---------+------------+-------------+---------------------+----------+
| 2 | 10 | Stav | 15 | 2014-08-11 19:43:49 | 1 |
| 1 | 10 | Jennifer | 15 | 2014-08-13 19:43:19 | 1 |
| 4 | 10 | Jennifer | 16 | 2014-08-02 19:44:27 | 1 |
| 5 | 10 | Osman | 17 | 2014-08-04 19:44:57 | 1 |
| 7 | 10 | Gokhan | 18 | 2014-08-15 19:45:37 | 1 |
| 3 | 10 | MotherLode | 25 | 2014-08-01 19:44:11 | 1 |
+----+---------+------------+-------------+---------------------+----------+
6 rows in set (0.00 sec)
As you can see the holder "Jennifer" is listed twice. I want mySQL to skip her after she got in the list. The result I want to be generated is:
+----+---------+------------+-------------+---------------------+----------+
| id | race_id | holder | record_time | date | position |
+----+---------+------------+-------------+---------------------+----------+
| 2 | 10 | Stav | 15 | 2014-08-11 19:43:49 | 1 |
| 1 | 10 | Jennifer | 15 | 2014-08-13 19:43:19 | 1 |
| 5 | 10 | Osman | 17 | 2014-08-04 19:44:57 | 1 |
| 7 | 10 | Gokhan | 18 | 2014-08-15 19:45:37 | 1 |
| 3 | 10 | MotherLode | 25 | 2014-08-01 19:44:11 | 1 |
+----+---------+------------+-------------+---------------------+----------+
I tried everything. GROUP BY holder generates wrong results. It gets the very first record of the holder, even though is not the best. In this table it generates an output like above because id:1 is the first record I inserted for Jennifer.
How can I generate output a result like above?
Desired result can be achieved through this query but it performance intensive. I have reproduced the result in SQLFilddle http://sqlfiddle.com/#!2/f8ee7/3
select * from racerecords
where
(HOLDER, RECORD_TIME) in (
select HOLDER,min(RECORD_TIME) from racerecords
group by HOLDER)
Seems you have missed to include the Where clause in the sub-query. Try this
select * from racerecords
where
(HOLDER, RECORD_TIME) in (
select HOLDER,min(RECORD_TIME) from racerecords where race_id =17
group by HOLDER )
And race_id =17
Order by RECORD_TIME
you should use distinct clause
SELECT DISTINCT column_name,column_name
FROM table_name;
looks this http://www.w3schools.com/sql/sql_distinct.asp