Postgres选择完整匹配的行集

时间:2017-02-28 15:51:06

标签: sql postgresql

我正试图从postgresSQL的以下数据中获得最佳匹配。

数据集示例:(以下数据集属于父级或者我会说我在名为tbl_parent_meta_data的表中保留动态父属性)

Filters: Brand=BMW, Wheels=17 & Model=B2017

根据上面的过滤器,SQL应该返回parentId只匹配过滤器,即:9080& 9082

+----+----------+-------------+---------------+
| Id | AdId     | MetaDataKey | MetaDataValue |
+----+----------+-------------+---------------+
|  1 |     9080 | Brand       | BMW           |
|  2 |     9080 | Wheels      | 17            |
|  3 |     9080 | Model       | B2017         |
|  4 |     9081 | Brand       | BMW           |
|  5 |     9081 | Wheels      | 18            |
|  6 |     9081 | Model       | B2016         |
|  7 |     9082 | Brand       | Audi          |
|  8 |     9082 | Wheels      | 19            |
|  9 |     9081 | Model       | A2016         |
| 10 |     9082 | Brand       | BMW           |
| 11 |     9082 | Wheels      | 17            |
| 12 |     9082 | Model       | B2017         |
+----+----------+-------------+---------------+

我使用的查询:这对我有用,我需要替代方案。

SELECT axd.ad_id adId
FROM ads_extradata axd
LEFT JOIN ads a on a.id = axd.ad_id
LEFT JOIN categories_translations ct ON ct.id = a.category_translation_id
LEFT JOIN categories_meta_data cmd ON cmd.id = axd.category_metadata_id
LEFT JOIN languages l ON l.id = ct.language_id
WHERE cmd.deleted = false AND ct.deleted = false AND a.deleted = false     
AND axd.deleted = false
AND  a.category_translation_id = '" . $categoryTranslation->getId() . "'
GROUP BY axd.ad_id
HAVING SUM(
CASE 'Brand' WHEN axd.extra_value::text THEN 10 ELSE 0 END +
CASE 'Wheels' WHEN axd.extra_value::text THEN 10 ELSE 0 END + 
CASE 'Model' WHEN axd.extra_value::text THEN 10 ELSE 0 END) >= 30

1 个答案:

答案 0 :(得分:1)

假设您可以提取过滤器并应用于您的查询

然后你可以做

ROI              Min        Max         Mean        Stdev
mrc_ranch_house -20.208261  6.025762    -8.866403   5.289712
river_1         -20.187374  -6.694543   -12.227586  2.66464
river_2         -18.365091  -5.820825   -13.164463  2.851231