i have two tables: invoice and orderlist
Invoice contains
+----------------+---------------+-------------+------------+
| orderInvoice | orderCustomer | orderTime | orderTotal |
+----------------+---------------+-------------+------------+
| 00001 | Nick | 2012-11-29 | 30.00 |
| 00002 | Andrew | 2012-11-29 | 15.00 |
| 00003 | West | 2012-11-29 | 80.00 |
+----------------+---------------+-------------+------------+
orderlist contains
+----------------+------------------+------+--------+
| orderInvoice | item | size | Qty | price |
+----------------+------------------+------+--------+
| 00001 | Coke | Can | 20 | 0.50 |
| 00001 | Coke | Bottle | 10 | 2.00 |
| 00002 | Sprite | Can | 30 | 0.50 |
| 00003 | Coke | Bottle | 40 | 2.00 |
+----------------+------------------+------+--------+
i want to output like this:
+---------------------------------------------------+
| Most Popular Soda |
+---------------------------------------------------+
| Coke Bottle 50 |
| Sprite Can 30 |
| Coke Can 20 |
+---------------------------------------------------+
| 2012-11-29 |
+---------------------------------------------------+
I tried to query it with concat() as ITEM and then count(ITEM) . it looks like i can use them together in a single query.
------------UPDATE----------------
Thank you guys for post the answers. I figured it out the answer(can't do it without your guys help). I will leave the answer there in case others might looking for it as well
SELECT
CONCAT(
orderlist.item,
orderlist.size
) AS item,
orderlist.orderInvoice,
SUM(orderlist.Qty) AS Qty
FROM
orderlist
INNER JOIN invoice ON nvoice.orderInvoice = orderlist.orderInvoice
WHERE
Invoice.orderTime = '2012-11-29'
GROUP BY
item
ORDER BY
Qty DESC
try this
select item,sum(qty) as quantity from orderlist group by item;
Try this:
Select item, size, SUM(qty) quantity
from
orderlist
group by item, size
order by quantity desc
Related
I've been scratching my head on a way to get this working for quite some time now, and as it's so specific I can't seem to find the answer elsewhere on StackOverflow. Any answers or suggestions would be appreciated.
I'm working to produce a report on that will inform a user of stock management and consumption. Calculating this with the values of current_stock and daily_stock_consumption is easy... But when we add shipments incoming into the process things become very complicated. Getting MYSQL to return the first day that each stock reaches 0 is leaving me blank.
For instance, let's say we are selling fruit and we have three tables fruit, fruit_sold and fruit_orders
fruit
|---------------------|------------------|
| id | fruit |
|---------------------|------------------|
| 1 | Apples |
|---------------------|------------------|
| 2 | Oranges |
|---------------------|------------------|
| 3 | pears |
|---------------------|------------------|
fruit_sold
|---------------------|------------------|------------------|------------------|
| id | fruit_id | sold_per_day | day_sold |
|---------------------|------------------|------------------|------------------|
| 1 | 1 | 101 | 1585695600 |
|---------------------|------------------|------------------|------------------|
| 2 | 2 | 445 | 1585695600 |
|---------------------|------------------|------------------|------------------|
| 3 | 3 | 214 | 1585782000 |
|---------------------|------------------|------------------|------------------|
| 4 | 1 | 512 | 1585782000 |
|---------------------|------------------|------------------|------------------|
fruit_orders
|---------------------|------------------|------------------|
| id | arriving | fruit_id |
|---------------------|------------------|------------------|
| 1 | 1592607600 | 1 |
|---------------------|------------------|------------------|
| 2 | 1586905200 | 3 |
|---------------------|------------------|------------------|
| 3 | 1590534000 | 2 |
|---------------------|------------------|------------------|
| 4 | 1588287600 | 3 |
|---------------------|------------------|------------------|
| 5 | 1593126000 | 1 |
|---------------------|------------------|------------------|
| 6 | 1592607600 | 2 |
|---------------------|------------------|------------------|
So far I have
SELECT SUM(fruit_sold.sold_per_day) / 2 AS consumption, fruit.fruit
FROM fruit_sold
LEFT JOIN fruit
ON fruit_sold.fruit_id = fruit.id
GROUP BY fruit_sold.fruit_id
How would I also return the number of days until the stock runs out (taking into account arriving orders and their arrival dates) for each fruit?
We want to return the following:
|---------------------|------------------|------------------|------------------|
| id | fruit | consumption | days_until_gone | |
|---------------------|------------------|------------------|------------------|
| 1 | apples | 306.5 | 2 |
|---------------------|------------------|------------------|------------------|
| 2 | oranges | 222.5 | 16 |
|---------------------|------------------|------------------|------------------|
| 3 | pears | 107 | 3 |
|---------------------|------------------|------------------|------------------|
You can get the numbers per day using union all, aggregation, and window functions:
select fruit_id, day_sold, sum(sold_per_day), sum(orders),
sum(sum(sold_per_day - orders)) over (partition by fruit_id order by day_sold) as net
from ((select fruit_id, day_sold, sold_per_day, 0 as orders
from fruit_sold
) union all
(select fruit_id, as day_sold, 0 as sold_per_day, count(*) as orders
from fruit_sold
group by fruit_id, day_sold
)
) f
group by fruit_id, day_sold;
I'm not 100% how this applies to your sample data, but it seems to basically provide the information that you want.
I am working on a small database that records rentals of different properties for different periods of time to different clients. I leave a summary example of the structures of my tables below.
+--------------+
| customer |
|--------------|
| id |
| name |
+--------------+
+--------------+
| property |
|--------------|
| id |
| number |
| address |
+--------------+
+--------------+
| rental |
|--------------|
| id |
| date_init |
| date_end |
| month_payment|
| customer_id |
| property_id |
+--------------+
What I am trying to find out now through a consultation is the following: in my rental table I keep the client, property and amount that I agree to cancel each month for the rental, so there are clients who rent different properties, during the year. How can I know how much money my clients have generated during a certain period of time, for example if I have the following records:
customer
+--------------+
| id | name |
|--------------|
| 1 | jhon doe|
| 2 | alex gs |
| 3 | martha |
+--------------+
property
+------------------------------------+
| id | number | address |
--------------------------------------
| 1 | 5643 | chicago |
| 2 | 1023 | toronto |
| 3 | 3445 | atlanta |
+------------------------------------+
rental
+-------------------------------------------------------------------+
| id | customer_id | property_id | date_init | date_end | amount |
---------------------------------------------------------------------
| 1 | 1 | 1 | 2019-01-05 | 2019-06-05 |3000 |
| 2 | 2 | 2 | 2019-04-10 | 2019-10-10 |1800 |
| 3 | 1 | 3 | 2019-02-14 | 2019-11-14 |1000 |
+-------------------------------------------------------------------+
then given as a parameter a period of time for example: 2019-01-01 to 2019-12-30 get only the records that match and have the following result:
+---------------------+
| customer | total |
|----------------------
| jhon doe | 24,000 |
| alex gs | 10,800 |
+---------------------+
In this case, the John Doe client has rented 2 properties the first for 5 months for an amount of 3000 total of 15000 and the other property for 9 months to 1000 total of 9000, so is it possible to make a query with this type of data? I don't have a query as an example yet, since I don't know how to deal with this problem. I am working on it, as soon as I have something I will update my question, thank you!
edited--
You need to use SUM() and a JOIN then specify the fields you want returned.
SELECT c.name AS customer, SUM(r.amount) AS total
FROM rentals r
INNER JOIN customer c ON c.id = r.customer_id
WHERE r.date_end >= $START AND r.date_end <= $END
GROUP BY r.customer_id
http://www.sqlservertutorial.net/sql-server-aggregate-functions/sql-server-sum/
I have two mysql table.
Table: bill
id | billtype | amount | advid | paydate |adjid | adjdate |
1 | electric | 10000 | 123 | 2017-01-01 | 50 | 2017-01-03 |
2 | Water | 5000 | 124 | 2017-02-01 | 0 | 0000-00-00 |
3 | Shirt | 500 | 125 | 2017-03-01 | 0 | 0000-00-00 |
Table: advance
id | advid | amount | balance | purpose |
1 | 123 | 50000 | 20000 | Bill |
2 | 124 | 70000 | 10000 | Bill |
3 | 125 | 55000 | 15000 | Uniform |
4 | 124 | 60000 | 10000 | Bill |
I want to create a drop down menu so that to select those 'advance' which are not adjusted yet (adjid=0 and adjdate=0000-00-00) in Table: bill and that drop down menu will also contain the total value of advance for same advance id (advid) like below:
<option>Bill-130000</option>
<option>Uniform-55000</option>
As total 130000 (70000+60000) advance is taken against advance id 124, so the Total amount of Advance in Option menu should be 130000 in case of Bill. But I am failed to calculate total amount of advance accurately:
SELECT sum(a.amount), purpose FROM bill as b, advance as a WHERE b.paydate!='0000-00-00' AND b.adjid!=0 AND a.advid=b.advid GROUP BY a.advid
Total amount in <option></option> is not coming actual.
What would be the right query for this purpose?
You could try
SELECT SUM(a.amount) AS amount,
MAX(purpose) AS purpose
FROM advance a
WHERE a.advid IN (
SELECT b.advid
FROM bill b
WHERE b.paydate = '0000-00-00'
AND b.adjid = 0)
GROUP BY a.advid
I have this table of orders
| ORDER_ID | PRODUCT | CUSTOMER | QTY | DATE
---------------------------------------------
| 1 | shoes | Nick | 1 | 01/01/2016
| 2 | shirts | Nick | 5 | 02/02/2016
| 3 | shoes | Paul | 10 | 03/03/2016
| 4 | shirts | Paul | 20 | 04/04/2016
So, How can I achieve this report result with ONE Select Statement?
| Date_of_Order | Customer | Quantity | PRODUCT_TOTAL_SALES |
-----------------------------------------------------------------
| 01/01/2016 | Nick | 1 | shoes : 11 |
| 02/02/2016 | Nick | 10 | shirts : 25 |
| 03/03/2016 | Paul | 5 | shoes : 11 |
| 04/04/2016 | Paul | 20 | shirts : 25 |
I know how to use concat(column1, ' ', column2) to create a combined column but I haven't succeed to add a sum for a grouped item there. When I try with left join I get the sum for a product ...BUT its always the whole sum and its not related to the dates of the order so when I try to filter the results on my query for a certain period I still get 11 for shoes and 25 for shirts...
You can group by multiple columns and get the sum for the smallest group.
If you want the daily sales, then instead of GROUP BY product use GROUP BY product, date
SELECT
o.`date` AS Date_of_Order,
SUM(o.qty) as Total_Quantity,
CONCAT(o.product, ':', SUM(o.qty))
FROM
orders o
GROUP BY product, `date`
ORDER BY `date`
Simple additional SELECT from same table can do that for entire period:
SELECT
o.`date` AS Date_of_Order,
o.Customer,
o.qty as Quantity,
(SELECT
CONCAT(oo.product, ':', SUM(oo.qty))
FROM
orders oo
WHERE
oo.product = o.product
) PRODUCT_TOTAL_SALES
FROM
orders o
Output:
+---------------+----------+----------+---------------------+
| Date_of_Order | Customer | Quantity | PRODUCT_TOTAL_SALES |
+---------------+----------+----------+---------------------+
| 01/01/2016 | Nick | 1 | shoes:11 |
| 02/02/2016 | Nick | 5 | shirts:25 |
| 03/03/2016 | Paul | 10 | shoes:11 |
| 04/04/2016 | Paul | 20 | shirts:25 |
+---------------+----------+----------+---------------------+
4 rows in set
If you want to filter by certain period, you must include it in both:
SELECT
o.`date` AS Date_of_Order,
o.Customer,
o.qty as Quantity,
(SELECT
CONCAT(oo.product, ':', sum(oo.qty))
FROM
orders oo
WHERE
oo.product = o.product
AND STR_TO_DATE(oo.`date`,'%d/%m/%Y') BETWEEN '2016-01-01' AND '2016-03-03'
) PRODUCT_TOTAL_SALES
FROM
orders o
WHERE
STR_TO_DATE(o.`date`,'%d/%m/%Y') BETWEEN '2016-01-01' AND '2016-03-03'
Output:
+---------------+----------+----------+---------------------+
| Date_of_Order | customer | Quantity | PRODUCT_TOTAL_SALES |
+---------------+----------+----------+---------------------+
| 01/01/2016 | Nick | 1 | shoes:11 |
| 02/02/2016 | Nick | 5 | shirts:5 |
| 03/03/2016 | Paul | 10 | shoes:11 |
+---------------+----------+----------+---------------------+
3 rows in set
I want to add a survey to a website. And a good survey needs a reporting. Some basic reports are done. Now I want to put some cream on the coffee ...
The table with sample data:
mysql> select * from u001;
+----+----------+------------+-------+---------------------+
| id | drink | sex | age | date |
+----+----------+------------+-------+---------------------+
| 1 | Beer | m | 30-39 | 2012-10-17 23:17:52 |
| 2 | Milk | f | 10-19 | 2012-10-18 00:15:59 |
| 3 | Milk | f | 20-29 | 2012-10-18 23:33:07 |
| 4 | Tea | m | 30-39 | 2012-10-20 22:47:08 |
| 5 | Water | f | 20-29 | 2012-10-20 22:47:30 |
| 6 | Milk | m | 30-39 | 2012-10-20 22:51:22 |
+----+----------+------------+-------+---------------------+
6 rows in set (0.00 sec)
I want to get a result that counts how many women/men likes Tea/Beer/etc.
A desired result like this:
+-------+-----+---------+
| drink | sex | counted |
+-------+-----+---------+
| Beer | m | 1 |
| Milk | f | 2 |
| Tea | m | 1 |
| Water | f | 1 |
| Milk | m | 1 |
+-------+-----+---------+
Have anyone some suggestions or solutions?
Thanks in advance.
SELECT drink, sex, COUNT(id) counted
FROM u001
GROUP BY drink, sex
SQLFiddle Demo
select drink, sex, count(id) from u001 group by drink, sex