I have the following data table from which I would like to sum the values of the field 'pts' for each 'pid' as follows:
The sum of the top 3 values per 'cont' plus the values of any other 'cont' per 'pid'. The results should be presented in DESC order by 'total'
+--------+-----+------+
| pid | pts | cont |
+--------+-----+------+
| 121693 | 40 | 1 |
| 121693 | 80 | 2 |
| 121693 | 120 | 1 |
| 121693 | 100 | 1 |
| 121693 | 500 | 1 |
| 121694 | 20 | 1 |
| 121694 | 0 | 2 |
| 121694 | 30 | 3 |
| 121695 | 0 | 1 |
| 121695 | 30 | 2 |
| 121695 | 0 | 1 |
+--------+-----+------+
In this example the query should return something like this
+--------+-------+
| pid | total |
+--------+-------+
| 121693 | 800 |
| 121694 | 50 |
| 121695 | 30 |
+--------+-------+
Is this possible?
Thanks in advance.
SELECT DISTINCT pid, SUM(Pts) AS Total
FROM your tablename
GROUP BY Pid
ORDER BY TOTAL
(Requires testing and minor fixes on small syntax)
Related
I have a table like this and i want to output without duplication of the same user. If i use group by it shows only one record on the same column. iam also using left join for location and user name. A little help
+------+---------+----------+---------+
| user | work id | location | time |
+------+---------+----------+---------+
| 1 | 42 | 1 | 2hr |
| 1 | 42 | 1 | 10min |
| 1 | 42 | 1 | 30min |
| 2 | 42 | 1 | 4hr |
| 2 | 42 | 1 | 2.30min |
| 1 | 50 | 2 | 4min |
| 1 | 50 | 2 | 5min |
| 2 | 20 | 3 | 3hr |
| 1 | 20 | 3 | 6hr |
+------+---------+----------+---------+
Iam looking for this
+------+---------+----------+
| user | work id | location |
+------+---------+----------+
| 1 | 42 | 1 |
| 1 | 50 | 2 |
| 1 | 20 | 3 |
| 2 | 42 | 1 |
| 2 | 20 | 3 |
+------+---------+----------+
You simply need a distinct clause here -
SELECT DISTINCT user
,workid
,location
FROM YOUR_TABLE
ORDER BY user
,location
I have the following table
MySQL [distributor]> select * from orderitems;
+-----------+------------+---------+----------+------------+
| order_num | order_item | prod_id | quantity | item_price |
+-----------+------------+---------+----------+------------+
| 20005 | 1 | BR01 | 100 | 5.49 |
| 20005 | 2 | BR03 | 100 | 10.99 |
| 20006 | 1 | BR01 | 20 | 5.99 |
| 20006 | 2 | BR02 | 10 | 8.99 |
| 20006 | 3 | BR03 | 10 | 11.99 |
| 20007 | 1 | BR03 | 50 | 11.49 |
| 20007 | 2 | BNBG01 | 100 | 2.99 |
| 20007 | 3 | BNBG02 | 100 | 2.99 |
| 20007 | 4 | BNBG03 | 100 | 2.99 |
| 20007 | 5 | RGAN01 | 50 | 4.49 |
| 20008 | 1 | RGAN01 | 5 | 4.99 |
| 20008 | 2 | BR03 | 5 | 11.99 |
| 20008 | 3 | BNBG01 | 10 | 3.49 |
| 20008 | 4 | BNBG02 | 10 | 3.49 |
| 20008 | 5 | BNBG03 | 10 | 3.49 |
| 20009 | 1 | BNBG01 | 250 | 2.49 |
| 20009 | 2 | BNBG02 | 250 | 2.49 |
| 20009 | 3 | BNBG03 | 250 | 2.49 |
+-----------+------------+---------+----------+------------+
18 rows in set (0.098 sec)
I am able to summarize the total price of a specified order as;
MySQL [distributor]> select order_num, sum(quantity*item_price)
as total_price from orderitems where order_num = 20008;
+-----------+-------------+
| order_num | total_price |
+-----------+-------------+
| 20008 | 189.60 |
+-----------+-------------+
1 row in set (0.026 sec)
How could I aggregate the total-price of each order and display them all together?
A SELECT statement clause that divides the query result into groups of rows, usually for the purpose of performing one or more aggregations on each group. The SELECT statement returns one row per group.
So in your all things are fine just need group by order_num
select order_num, sum(quantity*item_price) as total_price from orderitems
group by order_num
Use GROUP BY:
select order_num, sum(quantity*item_price) as total_price
from orderitems group by order_num
If you want to show total price of all orders which order details, you can remove order num from select:-
select sum(quantity*item_price) as total_price from orderitems;
However, if you are looking to show total price of all orders against each order, you can use below query :-
select order_num, sum(sum(quantity*item_price)) OVER() as total_price
from orderitems
group by order_num;
I'm trying to get the following result in mysql with below table: I
have tried to use sum (case when) but it give me the result for an
individual name only ,I want in the report the the total cost for each
user.
note: the ID column is not important you can ignore it .
mysql> select * from calls_records;
+----+-------+------------+------+
| id | month | name | cost |
+----+-------+------------+------+
| 1 | 1 | osama | 40 |
| 2 | 1 | rahman | 40 |
| 3 | 1 | ahmed | 30 |
| 4 | 1 | ali albann | 10.5 |
| 5 | 2 | osama | 10 |
| 6 | 2 | ali albann | 30 |
| 7 | 2 | ahmed | 10 |
| 8 | 2 | rahman | 10 |
+----+-------+------------+------+
expected result
+-----------+---------------------------+
| name | total_cost_for_each_user |
+------------+---------------------------+
| ahmed | 50 |
| ali albann | 40.5 |
| osama | 50 |
| rahman | 50 |
+------------+---------------------------+
query
select name, sum(cost) as totalcost
from calls_records
group by name
;
output
+------------+-----------+
| name | totalcost |
+------------+-----------+
| ahmed | 40 |
| ali albann | 40.5 |
| osama | 50 |
| rahman | 50 |
+------------+-----------+
sqlfiddle
i want to find solution for my query problem. I need to find the SUM of all priceProduct*quantity and separated with each of productcategory. I have already made a query, but it takes longer time to executed it. this is my query,
SELECT
pb.ProductCategoryID,
pb.ProductCategoryDescription,
(SELECT
SUM((SELECT pd.HPP FROM `price details` pd WHERE pd.ProductID = pdt.ProductID ORDER BY pd.PriceDetailID DESC LIMIT 1)*
(SELECT StockProductBallance FROM `stock product` sp WHERE sp.ProductID = pdt.ProductID ORDER BY sp.StockProductID DESC LIMIT 1))
FROM product pdt
WHERE pdt.ProductCategoryID = pb.ProductCategoryID
) AS Total
FROM `product category` pb
GROUP BY pb.ProductCategoryID
this my example table
table product:
+------+-------+
| id_p | id_pc |
+------+-------+
| 1 | 3 |
| 2 | 4 |
| 3 | 3 |
| 4 | 4 |
+------+-------+
table productcategory:
+-------+---------+
| id_pc | pc_name |
+-------+---------+
| 3 | new_pc |
| 4 | old_pc |
+-------+---------+
table price details:
+---------------+------+-----+
| PriceDetailID | id_p | hpp |
+---------------+------+-----+
| 1 | 1 | 100 |
| 2 | 1 | 110 |
| 3 | 1 | 120 |
| 4 | 2 | 200 |
| 5 | 2 | 210 |
| 6 | 2 | 220 |
+---------------+------+-----+
table stockProduct:
+-----------------+------+---------------+
| id_stockProduct | id_p | stockballance |
+-----------------+------+---------------+
| 1 | 1 | 10 |
| 2 | 1 | 11 |
| 3 | 1 | 12 |
| 4 | 2 | 20 |
| 5 | 2 | 21 |
| 6 | 2 | 22 |
+-----------------+------+---------------+
Really need your help guys, for better query..
+--------+---------+-------+--------+
| billID | orderId | price | date |
+--------+---------+-------+--------+
| 1 | 1 | 100 | 1.3.12 |
| 2 | 1 | 230 | 1.4.12 |
| 3 | 1 | 300 | 1.5.12 |
| 4 | 2 | 1000 | 1.3.12 |
| 5 | 2 | 160 | 1.4.12 |
| 6 | 3 | 400 | 1.3.12 |
+--------+---------+-------+--------+
I want to create view that have column that sum all price have same orderID but with date earlier than rows date. Like this:
+--------+---------+-------+--------+--------------+
| billID | orderId | price | date | add-on price |
+--------+---------+-------+--------+--------------+
| | | | | |
| 1 | 1 | 100 | 1.3.12 | 100 |
| 2 | 1 | 230 | 1.4.12 | 330 |
| 3 | 1 | 300 | 1.5.12 | 630 |
| 4 | 2 | 1000 | 1.3.12 | 1000 |
| 5 | 2 | 160 | 1.4.12 | 1160 |
| 6 | 3 | 400 | 1.3.12 | 400 |
+--------+---------+-------+--------+--------------+
You can user a correlated subquery for this:
select t.*,
(select sum(t2.price)
from table t2
where t2.orderId = t.orderId and t2.date <= t.date
) as CumulativePrice
from table t;