I have a table order_details like this
id | SKU | quantity_purchased | discount_price
---------------------------------------------------
1 | abc | 1 | 10.0
2 | abc | 90 | 00
2 | abc | 9 | 00
3 | xyz | 1 | 50.0
3 | xyz | 2 | 50.0
4 | xyz | 100 | 00
4 | xyz | 100 | 00
-----------------------------------------------
My query is
select
(select sum(quantity_purchased) from order_details where discount_price > 0.00) as qty_discount,
(select sum(quantity_purchased) from order_details where discount_price = 0.00)as qty_original,
sku
from order_details
GROUP BY sku
my required result is
SKU | quantity_original | quantity_discount
---------------------------------------------------
abc | 1 | 99
xyz | 3 | 200
-----------------------------------------------
that is, I need two columns for same sku after calculation,
I am unable to establish the logic, I have tried using GROUP BY in nested query, but it doesn't work...
any help is highly appreciated..
thanks
UPDATE:
trying to do via this but still a failure,
select
(select sum(quantity_purchased) from order_details where discount_price > 0.00 ) as qty_discount,
(select sum(quantity_purchased) from order_details where discount_price = 0.00 )as qty_original,
sku
from order_details
where sku = (select distinct sku from order_details)
GROUP BY sku
You can use conditional aggregation for this:
select sku,
sum(case when discount_price != 0 then quantity_purchased
else 0
end) quantity_original,
sum(case when discount_price = 0 then quantity_purchased
else 0
end) quantity_discount
from order_details
group by sku
SQL Fiddle Demo
Results:
| SKU | quantity_original | quantity_discount |
|-----|-------------------|-------------------|
| abc | 1 | 99 |
| xyz | 3 | 200 |
Related
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;
Table:
| id | productId | orderIndex | rejected |
------------------------------------------
| 1 | 1 | 0 | 1 |
| 2 | 1 | 1 | 0 |
| 3 | 1 | 2 | 0 |
| 4 | 2 | 0 | 0 |
| 5 | 2 | 1 | 1 |
| 6 | 3 | 0 | 0 |
How can I select one row per productId with minimum orderIndex that not rejected?
Expected result:
| id | productId | orderIndex | rejected |
------------------------------------------
| 2 | 1 | 1 | 0 |
| 4 | 2 | 0 | 0 |
| 6 | 3 | 0 | 0 |
I tried this query, but don't recieved correct result:
SELECT id, productId, min(orderIndex)
FROM table
WHERE rejected = 0
GROUP BY productId
This one don't work also:
SELECT id, productId, min(orderIndex)
FROM (
SELECT id, productId, orderIndex
FROM table
WHERE rejected = 0
) t
GROUP BY productId
You can start by selecting the minimum orderIndex of products that are not rejected like this:
SELECT productId, MIN(orderIndex)
FROM myTable
WHERE rejected = 0
GROUP BY productId;
Once you have that, you can join it with your original table on the condition that productId and minOrderIndex match:
SELECT m.id, m.productId, m.orderIndex
FROM myTable m
JOIN(
SELECT productId, MIN(orderIndex) AS minOrderIndex
FROM myTable
WHERE rejected = 0
GROUP BY productId) tmp ON tmp.productId = m.productId AND tmp.minOrderIndex = m.orderIndex;
My query makes the assumption that there are no duplicate (productId, orderIndex) pairs. As long as those don't exist, this will work just fine. Here is an SQL Fiddle example.
http://sqlfiddle.com/#!9/0196f/2
SELECT DISTINCT t.*
FROM table1 t
INNER JOIN (
SELECT productId, min(orderIndex) minIdx
FROM table1
WHERE rejected = 0
GROUP BY productId
) t1
ON t.productId = t1.productId
AND t.orderIndex = t1.minIdx;
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;
I am trying to come up with a single query which will take the following table (named sales):
user_id | order_total | order_date |
1 | 100 | 2012-01-01 |
1 | 200 | 2013-06-04 |
1 | 150 | 2012-01-08 |
2 | 100 | 2015-02-01 |
3 | 105 | 2014-10-27 |
And will return the following:
user_id | order_total | num_orders | last_order |
1 | 450 | 3 | 2013-06-04 |
3 | 105 | 1 | 2014-10-27 |
2 | 100 | 1 | 2015-02-01 |
So far I have come up with the following SQL to get the result:
SELECT
DISTINCT a.user_id,
SUM(order_total) AS order_total,
COUNT(*) AS num_orders,
b.order_date as last_order
FROM
`sales` AS a,
(
SELECT
order_date,
user_id
FROM `sales`
ORDER BY order_date DESC
) AS b
WHERE a.user_id = b.user_id
GROUP BY user_id
ORDER BY order_total DESC
The problem, however is that it returns:
user_id | order_total | num_orders | last_order |
1 | 1350 | 9 | 2013-06-04 |
3 | 105 | 1 | 2014-10-27 |
2 | 100 | 1 | 2015-02-01 |
Is there some way to prevent the sub-query from affecting the results of Sum and Count? Or am I going about this the wrong way?
why are you using a subselect?
SELECT user_id,
SUM(order_total) AS order_total,
MAX(order_date) AS lastOrder,
COUNT(*) AS num_orders
FROM table
GROUP BY user_id
ORDER BY order_total DESC
Is there any way to return distinct values with blank/null data from a table join. Best to explain with my example below.
Table "orders"
order_id | order_total
1 | 10
2 | 20
3 | 50
Table "order_items"
item_id | order_id | name | qty_ordered | base_price | row_total
1 | 1 | Product | 1 | 10 | 10
2 | 2 | Product | 1 | 10 | 10
3 | 2 | Product2 | 1 | 10 | 10
4 | 3 | Product | 2 | 10 | 20
5 | 3 | Product2 | 3 | 10 | 30
I'm trying to produce a result set that looks like this.
order_id | item_id | name | qty_ordered | base_price | row_total | order_total
1 | 1 | Product | 1 | 10 | 10 | 10
2 | 2 | Product | 1 | 10 | 10 | 20
null | 3 | Product2 | 1 | 10 | 10 | null
3 | 4 | Product | 2 | 10 | 20 | 50
null | 5 | Product2 | 3 | 10 | 30 | null
I only want the order_id and order_total once per order. I figure this is possible with some sort of join/distinct/sub query but alas nothing I've tried has worked so far.
Use:
SELECT x.order_id,
x.item_id,
x.name,
x.qty_ordered,
x.base_price,
x.row_total,
x.order_total
FROM (SELECT CASE
WHEN #order = o.order_id THEN NULL
ELSE o.order_id
END AS order_id,
oi.item_id,
oi.name,
oi.qty_ordered,
oi.base_price,
oi.row_total,
o.order_total,
CASE
WHEN #order = o.order_id THEN NULL
ELSE o.order_total
END AS order_total,
#order := o.order_id
FROM ORDER_ITEMS oi
JOIN ORDERS o ON o.order_id = oi.order_id
JOIN (SELECT #order := -1) r
ORDER BY o.order_id, oi.item_id) x
SELECT * FROM order_items
LEFT JOIN orders
ON (
order_items.order_id=orders.order_id
AND
order_items.item_id=(
SELECT MIN(item_id)
FROM order_items a
WHERE a.order_id=order_items.order_id
)
)
This should work because the nested query always returns the same MIN(item_id) for each order, and it only joins for that item.
But this is a very, very ugly piece of sql. Don't do this.