SQL服务器:根据多个条件在组中搜索
SQL Server : search among groups based on multiple conditions
我有以下 Product
table。我正在尝试根据多个标准查找产品。
这是我的示例数据:
+-----------+------------+------------------+----------------+-----------------+-------+
| productid | barcode | product_name | is_product_new | premium_service | price |
+-----------+------------+------------------+----------------+-----------------+-------+
| 1 | 1122334455 | rubber duck | 0 | 0 | 3,00 |
| 2 | 1122334455 | rubber duck | 1 | 0 | 4,00 |
| 3 | 1122334455 | rubber duck | 1 | 0 | 5,00 |
| 4 | 1122334455 | rubber duck | 1 | 1 | 6,00 |
| 5 | 2233445566 | barbie doll | 1 | 0 | 10,00 |
| 6 | 2233445566 | barbie doll | 0 | 0 | 8,00 |
| 7 | 3344556677 | actionman figure | 1 | 1 | 22,00 |
| 8 | 3344556677 | actionman figure | 1 | 0 | 18,00 |
| 9 | 3344556677 | actionman figure | 0 | 0 | 6,00 |
| 10 | 3344556677 | actionman figure | 0 | 0 | 5,00 |
+-----------+------------+------------------+----------------+-----------------+-------+
一共有三个产品。
我想在提供优质服务的产品中搜索新价最低、二手价最低和溢价的产品。
我的预期结果是:
+-----------+------------+------------------+------------------+------------------+-----------------+
| productid | barcode | product_name | lowest_old_price | lowest_new price | premium_price |
+-----------+------------+------------------+------------------+------------------+-----------------+
| 1 | 1122334455 | rubber duck | 3.00 | 4.00 | 6.00 |
| 7 | 3344556677 | actionman figure | 5.00 | 18.00 | 22.00 |
+-----------+------------+------------------+------------------+------------------+-----------------+
我尝试编写一个带有 group by 和 having 子句的查询,但我的结果没有任何意义!即使我不确定我需要使用哪个 functions/clause!
需要你的帮助...
嗯。 . .如果我理解正确的话,这是带有 having
子句的条件聚合:
select min(productid) as productid, barcode, product_name
min(case when is_product_new = 0 then price end) as old_price,
min(case when is_product_new = 1 then price end) as new_price,
min(case when premium_service = 1 then price end) as premium_price
from products p
group by productid, barcode, product_name
having max(premium_service) = 1;
如果premium_service
是位,可以转换成整数:
having max(convert(int, premium_service)) = 1
我有以下 Product
table。我正在尝试根据多个标准查找产品。
这是我的示例数据:
+-----------+------------+------------------+----------------+-----------------+-------+
| productid | barcode | product_name | is_product_new | premium_service | price |
+-----------+------------+------------------+----------------+-----------------+-------+
| 1 | 1122334455 | rubber duck | 0 | 0 | 3,00 |
| 2 | 1122334455 | rubber duck | 1 | 0 | 4,00 |
| 3 | 1122334455 | rubber duck | 1 | 0 | 5,00 |
| 4 | 1122334455 | rubber duck | 1 | 1 | 6,00 |
| 5 | 2233445566 | barbie doll | 1 | 0 | 10,00 |
| 6 | 2233445566 | barbie doll | 0 | 0 | 8,00 |
| 7 | 3344556677 | actionman figure | 1 | 1 | 22,00 |
| 8 | 3344556677 | actionman figure | 1 | 0 | 18,00 |
| 9 | 3344556677 | actionman figure | 0 | 0 | 6,00 |
| 10 | 3344556677 | actionman figure | 0 | 0 | 5,00 |
+-----------+------------+------------------+----------------+-----------------+-------+
一共有三个产品。
我想在提供优质服务的产品中搜索新价最低、二手价最低和溢价的产品。
我的预期结果是:
+-----------+------------+------------------+------------------+------------------+-----------------+
| productid | barcode | product_name | lowest_old_price | lowest_new price | premium_price |
+-----------+------------+------------------+------------------+------------------+-----------------+
| 1 | 1122334455 | rubber duck | 3.00 | 4.00 | 6.00 |
| 7 | 3344556677 | actionman figure | 5.00 | 18.00 | 22.00 |
+-----------+------------+------------------+------------------+------------------+-----------------+
我尝试编写一个带有 group by 和 having 子句的查询,但我的结果没有任何意义!即使我不确定我需要使用哪个 functions/clause!
需要你的帮助...
嗯。 . .如果我理解正确的话,这是带有 having
子句的条件聚合:
select min(productid) as productid, barcode, product_name
min(case when is_product_new = 0 then price end) as old_price,
min(case when is_product_new = 1 then price end) as new_price,
min(case when premium_service = 1 then price end) as premium_price
from products p
group by productid, barcode, product_name
having max(premium_service) = 1;
如果premium_service
是位,可以转换成整数:
having max(convert(int, premium_service)) = 1