Вы можете использовать этот запрос ...
Это работает, даже если у вас есть более 3 записей данных на продукт.
SELECT Max(productdetails.pname) as ProductName
FROM (SELECT *,
Lag(price, 1) OVER (partition BY pid ORDER BY pid, year ) AS LagPrice,
price AS CurrentPrice,
Lead (price, 1) OVER (partition BY pid ORDER BY pid, year) AS LeadPrice
FROM product) t1
INNER JOIN productdetails ON productdetails.pid = t1.pid
WHERE lagprice IS NOT NULL AND leadprice IS NOT NULL
GROUP BY t1.pid
HAVING Max(CASE WHEN leadprice < currentprice OR lagprice > currentprice THEN 1 ELSE 0 END) < 1
Или используйте этот запрос (аналогично запросу Арулкумара ).
SELECT Max(productdetails.pname) AS ProductName
FROM (SELECT pid,
CASE WHEN Lead(price) OVER (partition BY pid ORDER BY pid, year) >= price THEN 0 ELSE 1
END AS Decreased
FROM product) tmp
INNER JOIN productdetails ON productdetails.pid = tmp.pid
GROUP BY tmp.pid
HAVING Sum(tmp.Decreased) <= 1
Схемы таблиц и данные образца (добавлено больше данных образца)
CREATE TABLE [product](
[pid] [int] NULL,
[price] [money] NULL,
[year] [int] NULL
);
CREATE TABLE [productdetails](
[pid] [int] NULL,
[pname] [varchar](50) NULL
);
INSERT [product] ([pid], [price], [year]) VALUES (1, 10.0000, 2010);
INSERT [product] ([pid], [price], [year]) VALUES (1, 9.0000, 2011);
INSERT [product] ([pid], [price], [year]) VALUES (1, 13.0000, 2012);
INSERT [product] ([pid], [price], [year]) VALUES (2, 30.0000, 2010);
INSERT [product] ([pid], [price], [year]) VALUES (2, 20.0000, 2011);
INSERT [product] ([pid], [price], [year]) VALUES (2, 19.0000, 2012);
INSERT [product] ([pid], [price], [year]) VALUES (3, 8.0000, 2010);
INSERT [product] ([pid], [price], [year]) VALUES (3, 10.0000, 2011);
INSERT [product] ([pid], [price], [year]) VALUES (3, 15.0000, 2012);
INSERT [product] ([pid], [price], [year]) VALUES (3, 20.0000, 2013);
INSERT [product] ([pid], [price], [year]) VALUES (3, 15.0000, 2014);
INSERT [product] ([pid], [price], [year]) VALUES (4, 5.0000, 2010);
INSERT [product] ([pid], [price], [year]) VALUES (4, 10.0000, 2011);
INSERT [product] ([pid], [price], [year]) VALUES (4, 15.0000, 2012);
INSERT [product] ([pid], [price], [year]) VALUES (4, 20.0000, 2013);
INSERT [productdetails] ([pid], [pname]) VALUES (1, N'lg');
INSERT [productdetails] ([pid], [pname]) VALUES (2, N'samsung');
INSERT [productdetails] ([pid], [pname]) VALUES (3, N'apple');
INSERT [productdetails] ([pid], [pname]) VALUES (4, N'mi');
Все данные
+------+----------+------+
| pid | price | year |
+------+----------+------+
| 1 | 10.0000 | 2010 |
| 1 | 9.0000 | 2011 |
| 1 | 13.0000 | 2012 |
| 2 | 30.0000 | 2010 |
| 2 | 20.0000 | 2011 |
| 2 | 19.0000 | 2012 |
| 3 | 8.0000 | 2010 |
| 3 | 10.0000 | 2011 |
| 3 | 15.0000 | 2012 |
| 3 | 20.0000 | 2013 |
| 3 | 15.0000 | 2014 |
| 4 | 5.0000 | 2010 |
| 4 | 10.0000 | 2011 |
| 4 | 15.0000 | 2012 |
| 4 | 20.0000 | 2013 |
+------+----------+------+
Результат запроса
+-------------+
| ProductName |
+-------------+
| mi |
+-------------+
Онлайн-демонстрация: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=c55c02790f1a6f99bb8d560ab8d3149b
Ссылка
- запрос Арулкумара: https://stackoverflow.com/revisions/55984816/2