I have two tables, Tickets
and Discount
. I have to count how many times the combination of a discount ID can be applied to orders in the Ticket
table:
Tickets
table:
ITEMID | CATEGORYID
-------+-----------
1 | 11
1 | 11
1 | 11
2 | 11
3 | 12
4 | 13
4 | 13
5 | 14
Discounts
table:
DiscountID | ITEMID | CATEGORYID
-----------+--------+-----------
1 | 1 | 11
1 | 4 | 13
2 | 1 | 11
2 | 6 | 15
3 | 1 | 11
3 | 5 | 14
Result: the DiscountID
1 can be applied to the Ticket
table twice, and DiscountID
3 can be applied once.
I currently manage to do this via loop in a stored procedure, it is slow as I have to loop through each discount items with all ticket items manually.
I wonder if there is a better way of doing it via SQL with combination of JOIN
and INTERSECT
. I couldn't get my head around this, I have to match more than 1 row in the discount table at the same time.
BETTER EXPLANATION:
Original Table without any discount: Tickets
table:
ITEMID | CATEGORYID
-------+-----------
1 | 11
1 | 11
1 | 11
2 | 11
3 | 12
4 | 13
4 | 13
5 | 14
Apply Discount ID 1:
DiscountID | ITEMID | CATEGORYID
-----------+--------+-----------
1 | 1 | 11
1 | 4 | 13
Discount ID 1 counted twice because:
ITEMID | CATEGORYID
-------+-----------
1 | 11
1 | 11
4 | 13
4 | 13
Now onto Discount ID 2:
DiscountID | ITEMID | CATEGORYID
-----------+--------+-----------
2 | 1 | 11
2 | 6 | 15
With counted DiscountID 1 Items removed:
ITEMID | CATEGORYID
-------+-----------
1 | 11
2 | 11
3 | 12
5 | 14
Count for discount ID 2 is 0 because only one record of Dicount ID 2 can be matched in Ticket table 1 | 11
While
6 | 15 is not available in Ticket table. As Discount ID 2 isn't matched nothing is removed from Ticket table
Now onto Discount ID 3:
DiscountID | ITEMID | CATEGORYID 3 | 1 | 11
3 | 5 | 14
ITEMID | CATEGORYID
-------+-----------
1 | 11
2 | 11
3 | 12
5 | 14
Discount ID 3 is counted once and now with counted records remove will be
ITEMID | CATEGORYID
-------+-----------
2 | 11
3 | 12
As I do not know how to execute above via SQL, I do not know how I can output:
DISCOUNTID | APPLIED
-----------+-----------
1 | 2
3 | 1
This will do the trick.
select distinct d.*
from discounts d
inner join tickets t on t.itemId = d.itemid
and t.categoryId = d.categoryId
It will give you
DiscountID | ITEMID | CATEGORYID
-----------+--------+-----------
1 | 1 | 11
1 | 4 | 13
2 | 1 | 11
3 | 5 | 14
You can then sum up all the columns as necessary by
select discountid, count(*) as apply_discount_x_times from ( select distinct d.discountid, d.itemId, d.categoryId from discounts d inner join tickets t on t.itemId = d.itemid and t.categoryId = d.categoryId ) x group by discountid
Giving you
DiscountID | apply_discount_x_times -----------+-------- 1 | 2 2 | 1 3 | 2
select distinct d.*, CA.cnt
from discounts d
CROSS APPLY
(SELECT count(0) cnt from tickets t2
join discounts d2
on d.DiscountId = d2.discountid
and d2.itemid = t2.itemid
and d2.categoryid = t2.categoryid) CA
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.