Last active
August 29, 2015 14:06
-
-
Save mborodov/56ae9342b420ce7196a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET @broker=3, @product=1, @buy_status=3, @free=0; | |
SELECT price FROM ( | |
( | |
SELECT t1.price FROM underrighting_product_tariffs as t1 | |
LEFT JOIN ( SELECT COUNT(id) AS purchase_count FROM main_purchase | |
WHERE status = @buy_status AND broker = @broker | |
AND product = @product) | |
AS purchase | |
ON purchase.purchase_count >= t1.`from` AND purchase.purchase_count <= t1.`to` | |
WHERE t1.product=1 | |
AND purchase.purchase_count IS NOT NULL | |
AND NOT EXISTS (SELECT t2.price FROM underrighting_product_broker_price as t2 | |
WHERE t2.broker=@broker AND t2.product=@product) | |
) | |
UNION | |
( | |
SELECT t2.price FROM underrighting_product_broker_price as t2 | |
WHERE t2.broker=@broker AND t2.product=@product | |
AND t2.price IS NOT NULL | |
) | |
) AS global WHERE price != @free |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment