Sponsored
Sponsored
This approach involves using SQL to join the Product and Sales tables on the product_id column. We filter the sales to include only those made in the first quarter of 2019 and use GROUP BY
to ensure the product was sold only during this period. We left join again to ensure there are no sales for these products outside this period.
Time Complexity: O(n + m), where n is the number of rows in the Product table and m is the number in the Sales table. Space Complexity: O(n + m) for storing filter results.
1function salesAnalysis(product, sales) {
2 const firstQuarterSales = sales.filter(s => new Date(s.sale_date) >= new Date('2019-01-01') && new Date(s.sale_date) <= new Date('2019-03-31'));
3 const otherSales = sales.filter(s => new Date(s.sale_date) < new Date('2019-01-01') || new Date(s.sale_date) > new Date('2019-03-31'));
4 const firstQuarterIds = new Set(firstQuarterSales.map(s => s.product_id));
5 const otherIds = new Set(otherSales.map(s => s.product_id));
6 const resultIds = Array.from(firstQuarterIds).filter(id => !otherIds.has(id));
7 return product.filter(p => resultIds.includes(p.product_id));
8}
This JavaScript function filters sales data and checks for product IDs that are exclusively sold in the first quarter of 2019.
This approach involves using a subquery to find product IDs that have sales exclusively in the first quarter of 2019. We first identify sales in this period and ensure there are no other sales for these products outside this period using NOT EXISTS with filtered sales.
Time Complexity: O(n*m), Space Complexity: O(1) when using database indexing.
1SELECT p.product_id, p.product_name
2FROM Product p
3WHERE EXISTS (
4
This SQL solution uses a subquery to verify that the product was sold in Q1 2019 and ensures no sales exist for the product outside this period.