Wondering if someone can point me in the right direction.
I am trying to build some reports, querying a sales table, I have a issue where using SUM and Group by, but still wanting to use the original lines.
For example:
Select StockCode, SUM('Sell' * Qty) as Total from 'Sales'
Group by StockCode
What i would like is to display the results as a SUM, But i want to also retain the Qty.
So the output would be something like:
StockCode Qty Total.
I can obviously run some PHP to do the calculation easy enough, but i was trying to complete as much as possible in SQL Queries to avoid unnecessary cluttered code.
Is there a simple way to do this, or would you advise just doing the calculations in PHP.
Table Example:
StockCode Qty Sell
1234 2 1.99
5468 1 0.99
2456 2 2.99
1234 3 1.99
5648 1 2.99
2546 2 4.99
2456 3 2.99
Sell is Per Item
Example:
StockCode Qty Sell Total
1234 2 1.99 3.98
5468 1 0.99 0.99
2456 2 2.99 5.98
1234 3 1.99 5.97
5648 1 2.99 2.99
2546 2 4.99 9.98
2456 3 2.99 8.97
Results example:
1234 5 1.99 9.95
5468 2 0.99 1.98
2456 5 2.99 14.95
If I understand correctly you need to do this:
SELECT StockCode, Sell, SUM(Qty) AS Quantity_Sold_At_This_Price, SUM(Sell * Qty) AS Quantity_x_Price
FROM sales
GROUP BY StockCode, Sell
The result:
| StockCode | Sell | Quantity_Sold_At_This_Price | Quantity_x_Price |
| 1234 | 1.99 | 5 | 9.95 |
| 2456 | 2.99 | 5 | 14.95 |
| 2546 | 4.99 | 2 | 9.98 |
| 5468 | 0.99 | 1 | 0.99 |
| 5648 | 2.99 | 1 | 2.99 |
it may be problem in "quote", you can try this
Select StockCode, SUM(`Sell` * `Qty`) as Total from `Sales`
Group by StockCode
Related
Consider the following 2 MySQL tables:
Table - Commission
id
employee_id
from_date
to_date
orders_from
orders_to
commission
1
1
2021-05-01
2021-05-05
1
10
1
2
1
2021-05-06
2021-05-10
1
10
1
3
1
2021-05-11
2021-05-15
1
10
1
4
1
2021-05-01
2021-05-05
11
20
2
5
1
2021-05-06
2021-05-10
11
20
2
6
1
2021-05-11
2021-05-15
11
20
2
7
1
2021-05-01
2021-05-05
21
30
3
8
1
2021-05-06
2021-05-10
21
30
3
9
1
2021-05-11
2021-05-15
21
30
3
Table - Orders
id
employee_id
order_date
price
An employee will get a commission based on the number of his orders during each date bracket in the "commission" table.
So for example for the first 10(1-10) orders from May 1st till May 5th he'll get 1% for each order and then for the next 10(11-20) he'll get 2% for each of those and so on.
I'm going to have a form where the manager will be able to select 2 dates from a calendar in order to see what the employee should be paid.
I want to create a MySQL query that will give me all the date brackets from the table "commission" that include the 2 dates the manager has selected?
So lets say that we select the following period:
from 2021-05-03 to 2021-05-09
How will I get all just the rows for those dates? Which in this case would be ids:1,2,4,5,7,8.
P.S. Data and use case presented here are simplified for better readability/understanding
EDIT
After Thomas G's comment
Lets say that employee with id 1 has the following orders
| id | employee_id | order_date | price |
| :--- | :---------- | :--------- | :---- |
| 1 | 1 | 2021-05-03 | 100 |
| 2 | 1 | 2021-05-07 | 100 |
| 3 | 1 | 2021-05-13 | 100 |
Now I want to check what to pay that employee for the period 2021-05-03 - 2021-05-09.
In that period only the orders 1 and 2 are applicable to be paid.
If we check the commission table we can see that for this period we need records 1 and 2.
How can get rows 1 and 2 from the commission table, using the 2 dates provided, 2021-05-03 and 2021-05-09?
I have two tables. The first table is a list of lots - table name lot. There are a total of 3 lots that are managed by three people and the fields are: lot.lotID, lot.LotName
Every day, these lots will collect eggs, but sometimes, the people collecting will forget to make the entry at the end of the day. These entries go into a table called deposit. This table has deposit.DepID, deposit.DepDate, deposit.LotID and deposit.DepAmount
I need to list every lot each day and get each deposit amount. If there is none made, instead of not displaying a record, it should show that the record is a NULL value or 0. For instance if no entry is inserted into the database for lot 2 on 2018-10-11, I should see this if I query two days' worth of data:
LotID | LotName | Date | DepAmount
1 | Sarah | 2018-10-09 | 67
2 | Dave | 2018-10-09 | 84
3 | Mike | 2018-10-09 | 78
1 | Sarah | 2018-10-10 | 100
2 | Dave | 2018-10-10 | 0
3 | Mike | 2018-10-10 | 49
Alternately, it is okay if lot 2 on 10-10 says:
2 | Dave | 2018-10-10 | {null}
Can someone give me a bit of direction here? It seems like it would be insanely simple, but I can't find much on the subject. Thanks in advance!!
PS - Here are the two tables:
lot:
LotID Name
1 Jim
2 Mary
3 Jeff
4 Steve
5 Clinton
6 George
7 Jennifer
and deposit:
DepDate DepAmount UserID LotID
2018-10-09 07:09:13 150.00 1
2018-10-09 07:21:22 345.00 2
2018-10-09 19:18:33 283.00 3
2018-10-09 19:37:51 100.00 4
2018-10-09 14:11:47 357.00 8 5
2018-10-09 14:21:43 5324.00 8 6
2018-10-09 14:27:46 564.50 8 7
2018-10-10 14:32:29 3543.75 6 2
2018-10-10 23:12:40 234.00 10 3
2018-10-10 07:09:13 52.00 1
2018-10-11 07:09:13 234.00 3
2018-10-10 07:09:13 764.00 4
2018-10-10 07:09:13 123.00 6
2018-10-10 07:09:13 764.00 7
This is a tricky problem as a straight LEFT JOIN will not work due to a person having values for dates other than the one which has no value in deposit. So we need to first CROSS JOIN lot with a list of all the dates in deposit to get a set of all people and all dates i.e.
SELECT l.LotId, l.LotName, dt.Date
FROM lot l
CROSS JOIN (SELECT DISTINCT DepDate AS Date
FROM deposit) dt
Output:
LotId LotName Date
1 Sarah 2018-10-09
2 Dave 2018-10-09
3 Mike 2018-10-09
1 Sarah 2018-10-10
2 Dave 2018-10-10
3 Mike 2018-10-10
Then we can LEFT JOIN this result with the deposit table to get our list of deposits for each day, using COALESCE to convert NULL values to 0:
SELECT l.LotId, l.LotName, dt.Date, COALESCE(d.DepAmount, 0) AS DepAmount
FROM lot l
CROSS JOIN (SELECT DISTINCT DepDate AS Date
FROM deposit) dt
LEFT JOIN deposit d ON d.LotId = l.lotId AND d.DepDate = dt.Date
ORDER BY Date, l.LotId
Output:
LotId LotName Date DepAmount
1 Sarah 2018-10-09 67
2 Dave 2018-10-09 84
3 Mike 2018-10-09 78
1 Sarah 2018-10-10 100
2 Dave 2018-10-10 0
3 Mike 2018-10-10 49
I have this scheme:
+----+--+--------+--------------------+
| ID | Amount | paydate |
+----+-----------+--------------------+
| 1 | 200 |2016-11-05 |
+----+-----------+--------------------+
| 2 | 3000 |2016-11-10 |
+----+-----------+--------------------+
| 3 | 2500 |2016-11-11 |
+----+-----------+--------------------+
| ID | 100 |2016-11-21 |
+----+-----------+--------------------+
| 1 | 200 |2016-11-22 |
+----+-----------+--------------------+
| 2 | 3000 |2016-11-23 |
+----+-----------+--------------------+
| 3 | 2500 |2016-11-29 |
+----+-----------+--------------------+
How can I get the total Amount grouped by every 10 days like from the first of every month to the 10th then from 11th to 20th and from 21st to the end of the month?
to be shown like this :
+-----------+------------------------+
| Amount | paydate |
+-----------+------------------------+
| 3200 |2016-11-1 to 2016-11-10 |
+-----------+------------------------+
| 2500 |2016-11-11 to 2016-11-20|
+-----------+------------------------+
| 5800 |2016-11-21 to 2016-11-31|
+-----------+------------------------+
I tried
SELECT
SUM(Amount) AS Amount,
year(Facture.paydate) AS Annee,
month(Facture.paydate) AS Mois
FROM Facture
GROUP BY year(Facture.paydate), month(serFacture.paydate)
but this does not give me the result I need.
select sum(Amount) as sum_amount
,case
when day(paydate) <= 10 then concat(DATE_FORMAT(paydate,'%Y-%m-01'),' to ',DATE_FORMAT(paydate,'%Y-%m-10'))
when day(paydate) <= 20 then concat(DATE_FORMAT(paydate,'%Y-%m-11'),' to ',DATE_FORMAT(paydate,'%Y-%m-20'))
else concat(DATE_FORMAT(paydate,'%Y-%m-21'),' to ',DATE_FORMAT(paydate,'%Y-%m-31'))
end as paydate_period
from t
group by paydate_period
;
sum_amount paydate_period
3200 2016-11-01 to 2016-11-10
2500 2016-11-11 to 2016-11-20
5800 2016-11-21 to 2016-11-31
Here is an example query:
select
case
when day(date_field) between 1 and 10 then "01 to 10"
when day(date_field) between 11 and 20 then "11 to 20"
when day(date_field) between 21 and 31 then "21 to 31"
end as the_range,
date_format(date_field, "%m%Y") as the_month,
count(*)
from
the_table
group by
the_range, the_month
order by
the_month, the_range;
You can adapt the query so you display your result the way you need.
I have two tables like this:
survey:
survey_id | store_code | timestamp
product_stock:
survey_id | product_code | production_month | value
How can I get latest value, based on survey timestamp and grouped by store_code, product_code, and production_month?
for example if I have
survey_id | store_code | timestamp
1 store_1 2015-04-20
2 store_1 2015-04-22
3 store_2 2015-04-21
4 store_2 2015-04-22
survey_id | product_code | production_month | value
1 product_1 2 15
2 product_1 2 10
1 product_1 3 20
1 product_2 2 12
3 product_2 2 23
4 product_2 2 17
It'd return result like this
survey_id | store_code | time_stamp | product_code | production_month | value
2 store_1 2015-04-22 product_1 2 10
1 store_1 2015-04-20 product_1 3 20
1 store_1 2015-04-20 product_2 2 12
4 store_2 2015-04-22 product_2 2 17
and it needs to be as fast as possible, seeing the database is quite large in size
UPDATED - please run query again
Here is my answer:
SELECT survey.survey_id, survey.store_code, survey.timestamp, product_stock.survey_id, product_stock.product_code, product_stock.production_month, product_stock.value
FROM survey
INNER JOIN product_stock
ON survey.survey_id = product_stock.survey_id
WHERE survey.timestamp = (SELECT MAX(timestamp)
FROM survey)
GROUP BY survey.store_code,product_stock.product_code,product_stock.production_month;
I have these two tables:
Reception
id reception_date tin_lot company_id client_id quantity weight
1 2013-12-03 00:00:00 1 1 1 10 1980.00
2 2013-12-03 00:00:00 2 1 1 1 150.00
3 2013-12-13 00:00:00 3 1 2 10 4500.00
4 2013-12-13 00:00:00 4 2 5 5 2300.00
Payment
id payment_date amount reception_id
1 2013-12-03 00:00:00 500.0 1
2 2013-12-03 00:00:00 1200.0 3
The result I want to obtain is the following:
Expected result
id reception_date tin_lot client_id weight payment_made
1 2013-12-03 00:00:00 1 1 1980.00 500.0
2 2013-12-03 00:00:00 2 1 150.00 0.0
3 2013-12-13 00:00:00 3 2 4500.00 1200.0
4 2013-12-13 00:00:00 4 5 2300.00 0.0
I'm trying this query:
select rec.id
rec.reception_date,
rec.tin_lot,
rec.client_id,
rec.weight,
pay.payment_made
from liquidation.reception rec, liquidation.payment pay
where pay.recepcion_id=rec.id
But it doesn't list the receptions with no payment.
Please help me. Thanks in advance.
you need to Left Join the payment table:
from liquidation.reception rec
left join liquidation.payment pay on ( pay.recepcion_id=rec.id)
That is because you need to learn to use left outer join and proper join syntax. Just don't use a comma in a from clause any more.
Here is the query you want:
select rec.id, rec.reception_date, rec.tin_lot, rec.client_id, rec.weight,
coalesce(pay.payment_made, 0) as payment_made
from liquidation.reception rec left outer join
liquidation.payment pay
on pay.recepcion_id = rec.id;