Hello! I ask for help for a beginner, there are 3 tables: two of them are with data on goods (at the store and area levels), the third one connects the first two with codes (store and area)
a table that links the above listed by region / store codes (one store can be located only in one region, but there can be several stores in one region) (zone_loc)
There are 2 questions. First: you need to write a request showing the price for goods located in the first (zone = 1) area at the beginning and end of the period (June 1, 2015 through July 20, 2016)
SELECT item, ac_date, price FROM zone WHERE ac_date <= '2015.6.1' AND zone_id = 1 UNION SELECT item, ac_date, price FROM loc a JOIN zone_loc b ON a.loc_id = b.loc_id WHERE a.ac_date <= '2015.6.1' AND b.zone_id = 1 UNION SELECT item, ac_date, price FROM zone WHERE ac_date = '2016.7.20' AND zone_id = 1 UNION SELECT item, ac_date, price FROM loc a JOIN zone_loc b ON a.loc = b.loc WHERE a.ac_date = '2016.7.20' AND b.zone_id = 1; the problem is that not all products changed prices right on the last day of the period (2016.7.20), so when changing from
WHERE ac_date = '2016.7.20' on
WHERE ac_date BETWEEN '2015.6.2' AND'2016.7.20' intermediate values are included in the sample, which by the end of the period have changed their price. How to change the request so that in the sample at the end of the period only products with final prices are displayed? And how can I change a query without using union?
There is one more question, you need to create such a request, which would show the price of goods at the store level (table loc), which is not at the level of the area to which the store is attached. Here, as I understand it, there must be a correspondence by dates (among other things, the product id, zone to loc). According to this task, I don’t have any good practices at all, there is a thought that this should be with the store goods table (loc) through the communication table (zone_loc) with the area goods table (zone), and then through the EXISTS operator to find those records that are not in the zone table
SELECT a.item, a.loc_id, c.zone_id, a.ac_date, a.price FROM loc a JOIN zone_loc b ON a.loc_id = b.loc_id JOIN zone c ON b.zone_id = c.zone_id WHERE a.item = c.item AND a.ac_date = c.ac_date; But it’s not at all, and I don’t know how to continue using EXISTS (if I use it here at all) and come to the right decision. I would be grateful for any help in these matters!
