I have this table:
idTransactions idCampaignsList idMemberCard amountOriginal amountFinal dateTransaction
1 2 1 50.00 100.00 2012-10-31 12:45:41
2 3 1 0.00 -50.00 2012-10-31 12:47:25
3 2 2 255.00 255.00 2012-10-31 17:19:07
4 1 2 95.00 95.00 2012-11-02 20:38:36
5 3 2 0.00 -400.00 2012-11-02 20:39:50
24 1 4 10.00 2.00 2012-11-03 11:16:3
With this query
SELECT SUM(amountOriginal) AS euro,
SUM(amountFinal) AS deducted,
EXTRACT(YEAR_MONTH FROM(dateTransaction)) AS period
FROM transactions
INNER JOIN campaignsList ON campaignsList.idCampaignsList = transactions.idCampaignsList
INNER JOIN customers ON customers.idCustomer = campaignsList.idCustomer
WHERE customers.idCustomer = 14
AND
transactions.idCampaignsList = 2
GROUP BY period
ORDER BY period
I obtain this result
euro deducted period
305.00 305.00 201210
14860.46 -22758.50 201211
1845.00 -34710.00 201212
For last 12 month, sum of "charged" and discharged.
Now, idCampaignsList could be 1, 2, also 500, it depend on how many "campaigns" have my idCustomer (retrieved via JOIN).
I'd like have a query dinamic, that, "for each" idCampaignsList, print me sum of amountOriginal and amountFinal.
To intend, from previos table, i would like to have
idCampaignsList SUM(amountOriginal) SUM(amountFinal) period
1 50 50 201210
2 255 255 201210
2 95 -305 201211
4 10 2 201211
So, for every period, sum columns for every distinct idCampaignsList, where idCampaignsList is dinamically (SELECT idCampaignsList FROM myOtherTable where idCustomer = 14)
I'd like have a query dinamic, that, "for each" idCampaignsList, print
me sum of amountOriginal and amountFinal.
I think the several For each that you mean, is a GROUP BY transactions.idCampaignsList.
Try to add the transactions.idCampaignsList to the SELECT list, remove the predicate transactions.idCampaignsList = 2 from the WHERE clause and list that column in the GROUP BY clause as well, like so:
SELECT
transactions.idCampaignsList
SUM(amountOriginal) AS euro,
SUM(amountFinal) AS deducted,
EXTRACT(YEAR_MONTH FROM(dateTransaction)) AS period
FROM transactions
INNER JOIN campaignsList
ON campaignsList.idCampaignsList = transactions.idCampaignsList
INNER JOIN customers
ON customers.idCustomer = campaignsList.idCustomer
WHERE customers.idCustomer = 14
GROUP BY period, transactions.idCampaignsList
ORDER BY period
Related
Hello there are two tables
Interval
id
is_full
1
1
2
0
3
0
entry_penalty
interval_id
entry_id
amount
2
14
55
3
14
7
3
14
1
1
15
4
1
15
8
2
15
11
So i am trying to display Sum of all entry_penalties per interval, twist is even if there is no relation between entry_penalty and interval table i should display full course interval sum per entry_id (related to is_full field).
For example total results should be in this case
interval_id
entry_id
amount
1
14
63
2
14
55
3
14
8
1
15
23
2
15
11
I have tried with sub query but it ignores to do calculation when there is no relation between entry_penalties and interval tables regarding is_full column.
My code so far.
SELECT
ep.interval_id,
IF (
i.is_full,
(
SELECT SUM(ep2.amount) * 1000 FROM entry_penalty as ep2
WHERE ep2.entry_id = ep.entry_id
),
SUM(ep.amount) * 1000
) as penalty_time,
ep.entry_id
FROM entry_penalty ep
INNER JOIN \`interval\` i ON i.id = ep.interval_id
WHERE ep.entry_id IN (:entryIds)
GROUP BY interval_id, entry_id`
I would propose to deal with the two cases (full, not full) separately, and then use union all to combine the two results:
SELECT i.id, ep.entry_id, SUM(ep.amount)
FROM `interval` i,
entry_penalty ep
WHERE i.is_full
GROUP BY i.id, ep.entry_id
UNION ALL
SELECT i.id, ep.entry_id, SUM(ep.amount)
FROM entry_penalty ep
INNER JOIN `interval` i
ON ep.interval_id = i.id
AND NOT i.is_full
GROUP BY i.id, ep.entry_id
ORDER BY 2, 1
See it run on dbfiddle.uk, where it outputs:
id
entry_id
SUM(ep.amount)
1
14
63
2
14
55
3
14
8
1
15
23
2
15
11
I want to find the revenue generated by a particular event.
If I would say, event1 pitched on 8/1/2020 to customers A and B, event 2 pitched on 8/15/2020 to customer B & C, event 3 pitched on 8/30/2020. Then to find the revenue generated by event1, we need to find A customer and B customer pitched again for that month or not. If yes then consider the transaction date just before the date when the customer is pitched again. In the given example, A customer pitched again on 08/30/2020 and B customer pitched on 8/15/2020then then to calculate for event1 we need to consider the transaction of customer A till 8/29/2020 from the 8/1/2020 and Customer B till 8/14/2020 from the 8/1/2020.
Event Table:
EventID CID Date
123 1 01-12-2020
123 2 01-12-2020
123 3 01-12-2020
345 2 05-12-2020
345 4 05-12-2020
456 1 07-12-2020
456 4 07-12-2020
567 1 08-12-2020
Transaction Table:
UID Tran_Date Amount
1 03-12-2020 10
1 04-12-2020 20
1 07-12-2020 30
1 09-12-2020 40
2 03-12-2020 10
2 07-12-2020 30
2 07-12-2020 40
2 09-12-2020 30
3 07-12-2020 30
3 07-12-2020 40
3 09-12-2020 30
Output Table:
EventID CID Sum
123 1 30
456 1 30
567 1 40
123 2 10
456 2 100
123 3 100
One option uses window function lead() to get the date of the "next" event, then brings the transactions with a join, and finally aggregates:
select
e.eventid,
e.cid,
coalesce(sum(t.amount), 0) total_amount
from (
select e.*, lead(date) over(partition by cid order by date) lead_date
from events e
) e
left join transactions t
on t.uid = e.cid
and t.tran_date >= e.date
and (t.tran_date < e.lead_date or e.lead_date is null)
group by e.eventid, e.cid
Note that window functions are available in MySQL 8.0 only. In earlier versions, you can emulate lead() with a correlated subquery.
Currently I am honestly at loss what I am doing wrong. It is a rather simple query I think.
Tables:
operations:
id processedon clientid
1 2018-01-01 9
2 2018-03-16 9
3 2018-04-21 9
4 2018-04-20 9
5 2018-05-09 9
items:
id operation_id quantity unitprice
1 1 10 2
2 1 5 3
3 2 20 4
4 3 10 2
5 4 8 4
6 4 10 4
7 5 2 2
The expected result of the operation/query is:
month total_value
1 35
3 80
4 92
5 4
That is quantity * unitprice based. For some reason, it only returns month=4
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON (`items`.`operation_id` = `operations`.`id`)
GROUP BY 'month'
ORDER BY 'month'
According to the info provided the join should be
INNER JOIN operations ON items.operation_id = operations.id
Eg
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM `items`
INNER JOIN `operations` ON `items`.`operation_id` = `operations`.`id`
GROUP BY month(`operations`.`processedon`)
ORDER BY `month`
There is no efficiency gain by using a column alias in the group by clause, I prefer to avoid using them except perhaps in the order by clause.
The following query will give you the required answer
SELECT
month(`operations`.`processedon`) AS `month`,
SUM((`items`.`quantity` * `items`.`unitprice`)) AS `total_value`
FROM items
INNER JOIN operations ON (items.operation_id = operations.id)
GROUP BY month(operations.processedon)
ORDER BY month(operations.processedon)
You need to specify month correctly since it is not an existing column.
You'll get the following result
month total_value
1 35
3 80
4 92
5 4
I've the two tables orders
id article amount
1 1 1
2 2 50
and prices
id article min_amount price
1 1 1 42.99
2 2 1 5.06
3 2 5 4.55
4 2 10 4.3
5 2 25 4.05
6 2 100 2.66
The prices tables contains IDs of articles and a minimum amount you would have to buy to get a bulk discount (which would change the price for the order). I would like to join prices into orders, so that the result looks like:
id article amount price
1 1 1 42.99
2 2 50 4.05
The order id 2 is above the minimum (25) to get the article for 4.05€, but still below 100 at which you would get a bigger discount, so the query would to have pick the next-lower value.
I've tried this query so far
SELECT
orders.id AS id,
orders.article,
orders.amount,
prices.price,
(orders.amount - prices.min_amount) AS discount_diff
FROM orders
LEFT JOIN prices ON (prices.article = orders.article) AND (prices.min_amount <= orders.amount)
which gives this result
id article amount price discount_diff
1 1 1 42.99 0
2 2 50 5.06 49
2 2 50 4.55 45
2 2 50 4.3 40
2 2 50 4.05 25
You can find this example on "js"fiddle: http://sqlfiddle.com/#!9/1b2bf/8
The query you need is this:
SELECT orders.id AS id,
orders.article,
orders.amount,
prices.price
FROM orders
INNER JOIN prices ON ( prices.article = orders.article
and prices.min_amount <= orders.amount)
INNER JOIN ( SELECT orders.article,
orders.amount,
min(prices.price) minprince
FROM orders
INNER JOIN prices ON (prices.article = orders.article
AND prices.min_amount <= orders.amount)
GROUP BY orders.article,
orders.amount) b
ON ( prices.article = b.article
AND orders.amount = b.amount
AND prices.price = b.minprince)
See it here: http://sqlfiddle.com/#!9/1b2bf/27
I have a query that involves searching database over a range of 30 days. Queries, both with correct output and wrong output are below:
CORRECT RESULTS:
SELECT
affiliates.member_id,
IFNULL( COUNT(orders.deal_id) , 0 ) AS deals_count,
IFNULL( SUM(orders.quantity) , 0 ) AS deals_quanity
FROM affiliates
LEFT JOIN deals ON affiliates.member_id = deals.member_id
LEFT JOIN orders ON deals.deal_id = orders.deal_id
LEFT JOIN customers_orders_link ON orders.order_id = customers_orders_link.order_id
AND DATE(customers_orders_link.datetime) BETWEEN '2011-06-01' AND '2011-07-01'
AND customers_orders_link.order_status = 'Delivered'
GROUP BY affiliates.member_id;
EXPECTED & RECEIVED: (Correct)
MemberID COUNT SUM
1 11 16
2 0 0
WRONG RESULTS:
//Notice the change in the date range
SELECT
affiliates.member_id,
IFNULL( COUNT(orders.deal_id) , 0 ) AS deals_count,
IFNULL( SUM(orders.quantity) , 0 ) AS deals_quanity
FROM affiliates
LEFT JOIN deals ON affiliates.member_id = deals.member_id
LEFT JOIN orders ON deals.deal_id = orders.deal_id
LEFT JOIN customers_orders_link ON orders.order_id = customers_orders_link.order_id
AND DATE(customers_orders_link.datetime) BETWEEN '2011-10-01' AND '2011-10-31'
AND customers_orders_link.order_status = 'Delivered'
GROUP BY affiliates.member_id
EXPECTED:
MemberID COUNT SUM
1 0 0
2 0 0
BUT I RECEIVE: (INCORRECT OUTPUT)
MemberID COUNT SUM
1 11 16
2 0 0
The first query is producing correct results whereas the second query is producing incorrect results. Even if I use a date in the past as the range, I still receive the same Incorrect Output. Its as if the query is completely ignoring the date range specification. So this case of ignoring the date range specification seems to be the problem.
How can I make the query "see" and "obey" the date range specification and actually receive the Expected Output for the 2nd query listed above?
EDIT 1:
//Table: Orders
order_id deal_id quantity price
1 1 2 40.00
1 2 1 15.00
2 1 1 20.00
3 9 1 5.00
4 1 2 40.00
4 9 2 10.00
5 1 1 20.00
5 9 1 5.00
6 1 2 40.00
6 9 2 10.00
7 1 1 20.00
8 11 1 1.00
//Table: customers_orders_link
order_id customer_id order_status datetime
1 4 Cancelled 2011-06-05 20:26:45
2 4 Delivered 2011-06-05 20:38:28
3 4 Pending Payment 2011-06-05 20:56:50
4 4 Pending Payment 2011-06-09 17:03:08
5 4 Pending Payment 2011-06-09 17:12:23
6 4 Pending Payment 2011-06-09 17:19:57
7 4 Pending Payment 2011-06-09 17:40:59
8 4 Pending Payment 2011-06-10 03:55:17
I solved it myself using a totally different method.
I don't know what your data looks like, but I suspect your LEFT JOIN customers_orders_link is to blame. If you only want to tally COUNT() and SUM() when the conditions of that table are met, it should be a standard JOIN in place of a LEFT JOIN.