我想只選擇包含某個屬性的特定行。這裏是我正在使用的數據樣本:選擇具有一定屬性的分組行
src_id cand_source ------ ----------- 201609-004d7bgNDFXuIrQPXwsXrOptt2PdTdeXsjV5RJ6_mEQ mcp 201609-004d7bgNDFXuIrQPXwsXrOptt2PdTdeXsjV5RJ6_mEQ mc2 201609-00WmbmuIp3cwAcTNTbrgb9tTVR0AKNf-RvjXcHWPEEQ mc2 201609-00WmbmuIp3cwAcTNTbrgb9tTVR0AKNf-RvjXcHWPEEQ mc2 201609-00WmbmuIp3cwAcTNTbrgb9tTVR0AKNf-RvjXcHWPEEQ mc2 201609-00WmbmuIp3cwAcTNTbrgb9tTVR0AKNf-RvjXcHWPEEQ mc2 201609-00WmbmuIp3cwAcTNTbrgb9tTVR0AKNf-RvjXcHWPEEQ mc2 201609-00WmbmuIp3cwAcTNTbrgb9tTVR0AKNf-RvjXcHWPEEQ mc2 201609-01My_orS795Hmomry3-JiCiBVimarRzRGQ9Cnornp8Q mcp 201609-01My_orS795Hmomry3-JiCiBVimarRzRGQ9Cnornp8Q mcp 201609-01My_orS795Hmomry3-JiCiBVimarRzRGQ9Cnornp8Q mc2 201609-01My_orS795Hmomry3-JiCiBVimarRzRGQ9Cnornp8Q mcp 201609-01My_orS795Hmomry3-JiCiBVimarRzRGQ9Cnornp8Q mc2 201609-01noPFGBCqbH9jUB9MHNqPynjqW8cr24LJY917vSGTs mc2 201609-01noPFGBCqbH9jUB9MHNqPynjqW8cr24LJY917vSGTs mc2 201609-02ISoPEX0VVkQ0ogot49Q-e7K39Zyk2vdN1rB4Q-kl0 mc2 201609-02ISoPEX0VVkQ0ogot49Q-e7K39Zyk2vdN1rB4Q-kl0 mc2 201609-02LVZ8UqAaz7JCp3RAOTiIE7zH2mveiSQPBo6I6dHDc mc2 201609-02LVZ8UqAaz7JCp3RAOTiIE7zH2mveiSQPBo6I6dHDc mc2 201609-03dLH32kaKYVwIj4HiT1tZjCNgqgXiG-fvezX3S9QI4 mc2 201609-03dLH32kaKYVwIj4HiT1tZjCNgqgXiG-fvezX3S9QI4 mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-0421Jatpsk9T8GOD1M_GvDrnyV4dA41IL5tDeuTxGwU mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mcp 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04HzM6NBIx_6QN91xzF9_p0RGfAQcRMeEhVFEPFZ8p4 mc2 201609-04JzR3AMxsfQvAeq1MAgjCtMhcaqt2Z_WNmuUlYLrLM mc2 201609-04JzR3AMxsfQvAeq1MAgjCtMhcaqt2Z_WNmuUlYLrLM mcp
我想要做的是隻選擇具有至少一個cand_source
等於mcp
的src_id
秒。以下是我已經試過:
SELECT *
FROM schema.table
WHERE src_id IN (
SELECT src_id
FROM schema.table
WHERE batch_id = ?
GROUP BY src_id
HAVING count(cand_source = 'mcp') > 1
)
ORDER BY src_id,
match_score DESC
然而,這一直給我回的src_id
S作任何cand_source
小號相當於mcp
集羣。
有人指出,我只是過分複雜的事情。這裏的解決方案:
SELECT *
FROM schema.table
WHERE src_id IN (
SELECT DISTINCT src_id
FROM schema.table
WHERE batch_id = ?
AND cand_source = 'mcp'
)
ORDER BY src_id,
match_score DESC
doh!我過於複雜的事情。感謝爲我簡化了事情。 – liltitus27