我想根据条件选择数据:我有一个包含physician_key的表和给定月份的相应质量得分。我想选择质量得分为1,2的不同医生的数量。 对于一个月,可能会有更多的医生钥匙条目,因此分配的质量(按比例1-7)。我想只选择那些有质量(1,2)的医生的数量,如果同一个医生在给定月份的质量> 2,我不想算那个医生。我想要按产品和月份的信息
答案 0 :(得分:1)
我创建了一个示例表,因为你没有提供一个:
mysql> select * from sales_mkt_rep_qual;
+-------------------+---------+-------+-------------------+
| GEO_PHYSICIAN_KEY | product | month | SALES_REP_QUALITY |
+-------------------+---------+-------+-------------------+
| 1 | a | 8 | 1 |
| 1 | a | 8 | 2 |
| 1 | a | 8 | 3 |
| 2 | b | 8 | 2 |
| 2 | b | 8 | 1 |
| 2 | b | 9 | 2 |
| 1 | a | 9 | 2 |
| 2 | b | 9 | 3 |
| 3 | a | 9 | 2 |
+-------------------+---------+-------+-------------------+
您评论中的查询确实会出错:
SELECT COUNT(DISTINCT GEO_PHYSICIAN_KEY) AS encount_1to2,
product,MONTH
FROM sales_mkt_rep_qual
WHERE MAX(SALES_REP_QUALITY) = 2 ;
ERROR 1111 (HY000): Invalid use of group function
如果您将其更改为:
SELECT DISTINCT geo_physician_key AS encount_1to2, product, month
FROM sales_mkt_rep_qual
WHERE (geo_physician_key,month,product)
NOT IN (
SELECT geo_physician_key, month, product
FROM sales_mkt_rep_qual
WHERE sales_rep_quality >2 );
你看到了详细的结果:
+--------------+---------+-------+
| encount_1to2 | product | month |
+--------------+---------+-------+
| 2 | b | 8 |
| 1 | a | 9 |
| 3 | a | 9 |
+--------------+---------+-------+
不,你可以介绍一下计数:
SELECT COUNT(distinct geo_physician_key ) AS no_of_physicians,product, month
FROM sales_mkt_rep_qual
WHERE (geo_physician_key,month,product)
NOT IN (
SELECT geo_physician_key, month, product
FROM sales_mkt_rep_qual WHERE sales_rep_quality >2 )
GROUP BY month, product;
+------------------+---------+-------+
| no_of_physicians | product | month |
+------------------+---------+-------+
| 1 | b | 8 |
| 2 | a | 9 |
+------------------+---------+-------+
如果仍然不是您想要的,请提供更具体的表结构和数据示例。
答案 1 :(得分:0)
试试这个:
SELECT count(DISTINCT physician_key)
FROM my_table
WHERE month = desired_month
AND max(quality) = 2
GROUP BY month
答案 2 :(得分:0)
实际上我希望数据类似于下面的输出:
+--------------+---------+-------+
| encount_1to2 | product | MONTH |
+--------------+---------+-------+
| 2 | b | 8 |
+--------------+---------+-------+
并且对于标准SALES_REP_QUALITY< = 2,是否有可能在选择不同的地理医师密钥时,它可能会选择前2个,因为它符合标准?这就是我使用Thanix方法的最大功能与产品和月份的原因,因此聚合函数适用于一个月内的每个产品