I have two tables, one with the main data and a second table with historical values.
Table stocks
+----------+-------+-----------+
| stock_id | symbol| name |
+----------+-------+-----------+
| 1 | AAPL | Apple |
| 2 | GOOG | Google |
| 3 | MSFT | Microsoft |
+----------+-------+-----------+
Table prices
+----------+-------+---------------------+
| stock_id | price | date |
+----------+-------+---------------------+
| 1 | 0.05 | 2015-02-24 01:00:00 |
| 2 | 2.20 | 2015-02-24 01:00:00 |
| 1 | 0.50 | 2015-02-23 23:00:00 |
| 2 | 1.90 | 2015-02-23 23:00:00 |
| 3 | 2.10 | 2015-02-23 23:00:00 |
| 1 | 1.00 | 2015-02-23 19:00:00 |
| 2 | 1.00 | 2015-02-23 19:00:00 |
+----------+-------+---------------------+
I need a query that returns:
+----------+-------+-----------+-------+
| stock_id | symbol| name | diff |
+----------+-------+-----------+-------+
| 1 | AAPL | Apple | -0.45 |
| 2 | GOOG | Google | 0.30 |
| 3 | MSFT | Microsoft | NULL |
+----------+-------+-----------+-------+
Where diff is the result of subtracting from the newest price of a stock the previous price.
If one or less prices are present for a particular stock I should get NULL.
I have the following queries that return the last price and the previous price but I don't know how to join everything
/* last */
SELECT price
FROM prices
WHERE stock_id = '1'
ORDER BY date DESC
LIMIT 1
/* previous */
SELECT price
FROM prices
WHERE stock_id = '1'
ORDER BY date DESC
LIMIT 1,1
Using MySQL 5.5
This will return the expected result set:
SELECT stock_id, symbol, name,
SUM(CASE WHEN row_number = 1 THEN price END) -
SUM(CASE WHEN row_number = 2 THEN price END) AS diff
FROM (
SELECT #row_number:=CASE WHEN #stock=stock_id THEN #row_number+1
ELSE 1
END AS row_number,
#stock:=stock_id AS stock_id,
price, date, symbol, name
FROM (SELECT p.stock_id, s.symbol, s.name, p.price, p.date
FROM prices AS p
INNER JOIN stocks AS s ON p.stock_id = s.stock_id
ORDER BY stock_id, date DESC) AS t
) u
GROUP BY u.stock_id
SQL Fiddle Demo
This should do it:
SELECT s1.symbol,
s1.name,
COALESCE ((SELECT price
FROM prices p1
WHERE p1.stock_id = s1.stock_id
ORDER BY dateTime DESC
LIMIT 1), 0) -
COALESCE ((SELECT price
FROM prices p2
WHERE p2.stock_id = s1.stock_id
ORDER BY dateTime DESC
LIMIT 1,1), 0) AS diff
FROM stocks s1;
Related
I have two table incomes and expenses, I want to includes the none matching columns to the third table so I can be able to sum and group incomes and expenses with months and sum the Income column to get total and Expense Column total
incomes table expenses table
----------------------------------- ------------------------------------
| incomeId | date | amount | | expenseId | date | amount |
+---------------------------------+ +----------------------------------+
| 1 | 2/4/2020 | 3000 | | 1 | 8/4/2020 | 3000 |
| 2 | 9/4/2020 | 9000 | | 2 | 23/4/2020 | 3500 |
| 3 | 15/9/2020 | 15000 | | 1 | 9/3/2020 | 2000 |
| 4 | 7/3/2020 | 7000 | ------------------------------------
------------------------------------
Expected table Results
------------------------------------
| Month | Income | Expense |
+----------------------------------+
| March | 7000 | 2000 |
| April | 12000 | 6500 |
| September | 15000 | |
+==================================+
| Total | 34000 | 8500 |
====================================
SQL
SELECT
Income.Month,
Income.Income,
Expense.Expense
FROM(
SELECT
DATE_FORMAT(date,'%M') AS Month,
SUM(amount) AS Income
FROM income
GROUP BY DATE_FORMAT(date, '%M')
) AS Incomes
JOIN
(
SELECT
DATE_FORMAT(date,'%M') AS Month,
SUM(amount) AS Expense
FROM expenses
GROUP BY DATE_FORMAT(date, '%M')
) AS Expense
ON Expense.Month = Income.Month
If you want to allow missing dates on both tables, you can emulate a full join with union all:
select year(date) yr, month(date) mn, sum(income) income, sum(expense) expense
from (
select date, amount income, null expense from incomes
union all
select date, null, amount from expenses
) t
group by year(date), month(date)
with rollup
Consider I have the following rows in the table
| id | user_id | amount | date |
------------------------------------------------
| 1 | 1 | 100 | 2019-09-30 |
------------------------------------------------
| 2 | 2 | 100 | 2019-09-30 |
------------------------------------------------
| 3 | 1 | 100 | 2019-09-30 |
------------------------------------------------
| 4 | 3 | 100 | 2019-10-01 |
------------------------------------------------
| 5 | 1 | 75 | 2019-10-01 |
------------------------------------------------
| 6 | 3 | 100 | 2019-10-01 |
------------------------------------------------
| 7 | 1 | 35 | 2019-10-01 |
------------------------------------------------
I am trying find a way to get all the rows with user_id = 1 where the sum(amount) < 300 and date <= '2019-10-01'.
What I am trying to do is to only process records that meet a certain threshold sum. I am not quite sure where to start.
Expected Result
| id | user_id | amount | date |
------------------------------------------------
| 1 | 1 | 100 | 2019-09-30 |
------------------------------------------------
| 3 | 1 | 100 | 2019-09-30 |
------------------------------------------------
| 5 | 1 | 75 | 2019-10-01 |
------------------------------------------------
Here is what I have tried so far
SELECT id, SUM(amount) as total_sum
FROM table
WHERE date <= '2019-10-01' AND user_id = 1
ORDER BY date ASC
HAVING total_sum <= 300
I don't get the desired output based on the above query.
MySQL Version currently using: 5.7.25
I did look at this question MySQL select records with sum greater than threshold assuming they are trying to do the same thing, but this isn't what I am looking at
It is a Rolling Sum problem. In MySQL 8.0.2 and above, you can solve this using Window functions with Frames. In older versions, we can do the same using User-defined Session variables.
We first calculate the rolling sum using Session variables.
Then, use the result-set in a Derived table, and find the id where total sum crosses the "barrier" of 300. Barrier is reached when the New rolling Sum is greater than 300. We set the barrier value to 1 at this point, 0 for rows before it, and 2 and more, for the rows afterwards.
We will only consider the rows where barrier is 0.
Try (works for all MySQL versions):
Query #1
SELECT dt.id,
dt.user_id,
dt.amount,
dt.date
FROM
(
SELECT
t.id,
t.user_id,
t.amount,
t.date,
#barrier := CASE
WHEN
(#tot_qty := #tot_qty + t.amount) > 300
THEN (#barrier + 1)
ELSE 0
END AS barrier
FROM
your_table AS t
CROSS JOIN (SELECT #tot_qty := 0,
#barrier := 0) AS user_init
WHERE t.user_id = 1
AND t.date <= '2019-10-01'
ORDER BY t.user_id, t.date, t.id
) AS dt
WHERE dt.barrier = 0
ORDER BY dt.user_id, dt.date, dt.id;
Result
| id | user_id | amount | date |
| --- | ------- | ------ | ---------- |
| 1 | 1 | 100 | 2019-09-30 |
| 3 | 1 | 100 | 2019-09-30 |
| 5 | 1 | 75 | 2019-10-01 |
View on DB Fiddle
If you don't like to use Session Variables (some experienced SO users dislike them vehemently), you can utilize a technique based on "Self-Join" and then use GROUP BY with HAVING to filter out.
General idea is that we left join to get previous rows for the specific user_id, and then aggregate to get the rolling sum, and then filtering using Having clause.
Query
SELECT
t1.*
FROM
your_table AS t1
LEFT JOIN your_table AS t2
ON t2.user_id = t1.user_id
AND t2.date <= t1.date
AND t2.id <= t1.id
WHERE t1.user_id = 1
AND t1.date <= '2019-10-31'
GROUP BY t1.user_id, t1.date, t1.id, t1.amount
HAVING COALESCE(SUM(t2.amount),0) < 300;
Result
| id | user_id | amount | date |
| --- | ------- | ------ | ---------- |
| 1 | 1 | 100 | 2019-09-30 |
| 3 | 1 | 100 | 2019-09-30 |
| 5 | 1 | 75 | 2019-10-01 |
View on DB Fiddle
You can benchmark both the approaches and decide which one is suitable.
For this query, you will need the composite index: (user_id, date)
I have a table like this:
created_date | id | status | completed_date
2019-03-20 | 1 | Open |
2019-03-20 | 2 | Open |
2019-03-19 | 3 | Comp | 2019-03-21
2019-03-21 | 4 | Comp | 2019-03-22
2019-03-22 | 5 | Comp | 2019-03-22
2019-03-18 | 6 | Open |
I want to find count of all the IDs that were created before '2019-03-21' and had a status of 'Open' OR they were created before '2019-03-21' and even had a 'Comp' status but they were completed after '2019-03-21'.
Below is the query I have:
SELECT
CAST(CREATED AS DATE),
COUNT(DISTINCT id)
FROM testtable
WHERE
(CAST(CREATED AS DATE) <= '2019-03-21' AND status = 'Open')
OR (
CAST(CREATED AS DATE) <= '2019-03-21' AND status='Comp'
AND CAST(COMPLETED AS DATE) > '2019-03-21'
)
It gives the correct result. i.e., on 21st, 4 IDs were open. But now I want this information for the last 4 days. How do I modify this query to do that??
The output should be:
created_date | count(ID)
2019-03-21 | 4
2019-03-20 | 4
2019-03-19 | 2
2019-03-18 | 1
Please help!!
Here is a solution that works correctly with your sample data. It generates a list of dates using the distinct values that can be found in column created_date, and then LEFT JOINs it with the table. The JOIN conditions carry the logic your described. It seems to me like you do not need to check the status of the records, since, apparently, any record that has a non-NULL completed_date is in status Comp.
SELECT
dt.created_date,
COUNT(t.id)
FROM
(SELECT DISTINCT created_date FROM mytable) dt
LEFT JOIN mytable t
ON t.created_date <= dt.created_date
AND (t.completed_date IS NULL OR t.completed_date > dt.created_date)
GROUP BY dt.created_date
This Demo on DB Fiddle with your sample data returns:
| created_date | COUNT(t.id) |
| ------------ | ----------- |
| 2019-03-18 | 1 |
| 2019-03-19 | 2 |
| 2019-03-20 | 4 |
| 2019-03-21 | 4 |
| 2019-03-22 | 3 |
I have a table called transactions which contains sellers and their transactions: sale, waste, and whenever they receive products. The structure is essentially as follows:
seller_id transaction_date quantity reason product unit_price
--------- ---------------- -------- ------ ------- ----------
1 2018-01-01 10 import 1 100.0
1 2018-01-01 -5 sale 1 100.0
1 2018-01-01 -1 waste 1 100.0
2 2018-01-01 -3 sale 4 95.5
I need a daily summary of each seller, including the value of their sales, waste and starting inventory. The problem is, the starting inventory is a cumulative sum of quantities up until the given day (the imports at the given day is also included). I have the following query:
SELECT
t.seller_id,
t.transaction_date,
t.SUM(quantity * unit_price) as amount,
t.reason as reason,
(
SELECT SUM(unit_price * quantity) FROM transactions
WHERE seller_id = t.seller_id
AND (transaction_date <= t.transaction_date)
AND (
transaction_date < t.transaction_date
OR reason = 'import'
)
) as opening_balance
FROM transactions t
GROUP BY
t.transaction_date,
t.seller_id
t.reason
The query works and I get the desired results. However, even after creating indices for both the outer and the subquery, it takes way too much time (about 30 seconds), because the opening_balance query is a dependant subquery which is calculated for each row over and over again.
How can i optimize, or rewrite this query?
Edit: the subquery had a small bug with a missing WHERE condition, i fixed it, but the essence of the question is the same. I created a fiddle with example data to play around:
https://www.db-fiddle.com/f/ma7MhufseHxEXLfxhCtGbZ/2
Following approach utilizing User-defined variables can be more performant than using the Correlated Subquery. In your case, a temp variable was used to account for the calculation logic, which also get outputted. You can ignore that.
You can try the following query (can add more explanation if needed):
Query
SELECT dt.reason,
dt.amount,
#bal := CASE
WHEN dt.reason = 'import'
AND #sid <> dt.seller_id THEN dt.amount
WHEN dt.reason = 'import' THEN #bal + #temp + dt.amount
WHEN #sid = 0
OR #sid = dt.seller_id THEN #bal
ELSE 0
end AS opening_balance,
#temp := CASE
WHEN dt.reason <> 'import'
AND #sid = dt.seller_id
AND #td = dt.transaction_date THEN #temp + dt.amount
ELSE 0
end AS temp,
#sid := dt.seller_id AS seller_id,
#td := dt.transaction_date AS transaction_date
FROM (SELECT seller_id,
transaction_date,
reason,
Sum(quantity * unit_price) AS amount
FROM transactions
WHERE seller_id IS NOT NULL
GROUP BY seller_id,
transaction_date,
reason
ORDER BY seller_id,
transaction_date,
Field(reason, 'import', 'sale', 'waste')) AS dt
CROSS JOIN (SELECT #sid := 0,
#td := '',
#bal := 0,
#temp := 0) AS user_vars;
Result (note that I have ordered by seller_id first and then transaction_date)
| reason | amount | opening_balance | temp | seller_id | transaction_date |
| ------ | ------ | --------------- | ----- | --------- | ---------------- |
| import | 1250 | 1250 | 0 | 1 | 2018-12-01 |
| sale | -850 | 1250 | -850 | 1 | 2018-12-01 |
| waste | -100 | 1250 | -950 | 1 | 2018-12-01 |
| import | 950 | 1250 | 0 | 1 | 2018-12-02 |
| sale | -650 | 1250 | -650 | 1 | 2018-12-02 |
| waste | -450 | 1250 | -1100 | 1 | 2018-12-02 |
| import | 2000 | 2000 | 0 | 2 | 2018-12-01 |
| sale | -1200 | 2000 | -1200 | 2 | 2018-12-01 |
| waste | -250 | 2000 | -1450 | 2 | 2018-12-01 |
| import | 750 | 1300 | 0 | 2 | 2018-12-02 |
| sale | -600 | 1300 | -600 | 2 | 2018-12-02 |
| waste | -450 | 1300 | -1050 | 2 | 2018-12-02 |
View on DB Fiddle
do thing something like this ?
SELECT s.* ,#balance:=#balance+(s.quantity*s.unit_price) AS opening_balance FROM (
SELECT t.* FROM transactions t
ORDER BY t.seller_id,t.transaction_date,t.reason
) s
CROSS JOIN ( SELECT #balance:=0) AS INIT
GROUP BY s.transaction_date, s.seller_id, s.reason;
SAMPLE
MariaDB [test]> select * from transactions;
+----+-----------+------------------+----------+------------+--------+
| id | seller_id | transaction_date | quantity | unit_price | reason |
+----+-----------+------------------+----------+------------+--------+
| 1 | 1 | 2018-01-01 | 10 | 100 | import |
| 2 | 1 | 2018-01-01 | -5 | 100 | sale |
| 3 | 1 | 2018-01-01 | -1 | 100 | waste |
| 4 | 2 | 2018-01-01 | -3 | 99.5 | sale |
+----+-----------+------------------+----------+------------+--------+
4 rows in set (0.000 sec)
MariaDB [test]> SELECT s.* ,#balance:=#balance+(s.quantity*s.unit_price) AS opening_balance FROM (
-> SELECT t.* FROM transactions t
-> ORDER BY t.seller_id,t.transaction_date,t.reason
-> ) s
-> CROSS JOIN ( SELECT #balance:=0) AS INIT
-> GROUP BY s.transaction_date, s.seller_id, s.reason;
+----+-----------+------------------+----------+------------+--------+-----------------+
| id | seller_id | transaction_date | quantity | unit_price | reason | opening_balance |
+----+-----------+------------------+----------+------------+--------+-----------------+
| 1 | 1 | 2018-01-01 | 10 | 100 | import | 1000 |
| 2 | 1 | 2018-01-01 | -5 | 100 | sale | 500 |
| 3 | 1 | 2018-01-01 | -1 | 100 | waste | 400 |
| 4 | 2 | 2018-01-01 | -3 | 99.5 | sale | 101.5 |
+----+-----------+------------------+----------+------------+--------+-----------------+
4 rows in set (0.001 sec)
MariaDB [test]>
SELECT
t.seller_id,
t.transaction_date,
SUM(quantity) as amount,
t.reason as reason,
quantityImport
FROM transaction t
inner join
(
select sum(ifnull(quantityImport,0)) quantityImport,p.transaction_date,p.seller_id from
( /* subquery get all the date and seller distinct row */
select transaction_date ,seller_id ,reason
from transaction
group by seller_id, transaction_date
)
as p
left join
( /* subquery get all the date and seller and the import quantity */
select sum(quantity) quantityImport,transaction_date ,seller_id
from transaction
where reason='Import'
group by seller_id, transaction_date
) as n
on
p.seller_id=n.seller_id
and
p.transaction_date>=n.transaction_date
group by
p.seller_id,p.transaction_date
) as q
where
t.seller_id=q.seller_id
and
t.transaction_date=q.transaction_date
GROUP BY
t.transaction_date,
t.seller_id,
t.reason;
How can we SUM amount for each activity only on same date and output a row for each date? This query is not working.
SELECT SUM(amount), type, date FROM table GROUP BY DISTINCT date;
Table
+----+------------+-----------+---------+
| id | date | activity | amount |
+----+------------+-----------+---------+
| 1 | 2017-12-21 | Shopping | 200 |
| 2 | 2017-12-21 | Gift | 240 |
| 3 | 2017-12-23 | Give Away | 40 |
| 4 | 2017-12-24 | Shopping | 150 |
| 5 | 2017-12-25 | Give Away | 120 |
| 6 | 2017-12-25 | Shopping | 50 |
| 7 | 2017-12-25 | Shopping | 500 |
+----+------------+-----------+---------+
Required Result
+------------+-----------+------+-----------+
| date | Shopping | Gift | Give Away |
+------------+-----------+------+-----------+
| 2017-12-21 | 200 | 240 | |
| 2017-12-23 | | | 40 |
| 2017-12-24 | 150 | | |
| 2017-12-25 | 550 | | 120 |
+------------+-----------+------+-----------+
Use:
select `date`,
sum(if (activity='Shopping', amount, null)) as 'Shopping',
sum(if (activity='Gift', amount, null)) as 'Gift',
sum(if (activity='Give Away', amount, null)) as 'Give Away'
from table
group by `date`
You can try this. It returns exact result that you want
SELECT t.date,
SUM(t.shopping_amount) AS shopping,
SUM(t.gift_amount) AS gift,
SUM(t.give_away_amount) AS give_away
FROM
(
SELECT p.`date`, p.`activity`, p.`amount` AS shopping_amount,
0 AS gift_amount, 0 AS give_away_amount
FROM products p
WHERE p.`activity` = 'Shopping'
UNION
SELECT p.`date`, p.`activity`, 0 AS shopping_amount,
p.amount AS gift_amount, 0 AS give_away_amount
FROM products p
WHERE p.`activity` = 'Gift'
UNION
SELECT p.`date`, p.`activity`, 0 AS shopping_amount,
0 AS gift_amount, p.amount AS give_away_amount
FROM products p
WHERE p.`activity` = 'Give Away'
) t
GROUP BY t.date
Hmmm, you can't pivot your results into column headers unless you know all possible values as demonstrated by slaasko but you can get the results using sql into a form which can be pivoted using your display tool ( e.g. slice of BI tool).
SELECT SUM(amount), activity, date FROM table GROUP BY date, activity;