我想根据条件选择数据:我有一个表,其中包含给定月份的医生键和相应的质量得分。我想选择质量得分为 1,2 的不同医生的数量。一个月内,可能会有更多的医生键条目,并相应地分配质量(1-7 级)。我只想选择那些具有质量 (1,2) 的医生的数量,如果同一位医生在给定月份的质量 >2,我不想计算那个医生。我想要按产品和月份的信息
问问题
63 次
3 回答
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 |
+------------------+---------+-------+
如果这仍然不是您想要的,请给出更具体的表结构和数据示例。
于 2013-08-06T17:06:58.307 回答
0
实际上我希望数据像下面的输出:
+--------------+---------+-------+
| encount_1to2 | product | MONTH |
+--------------+---------+-------+
| 2 | b | 8 |
+--------------+---------+-------+
对于标准 SALES_REP_QUALITY <= 2,是否有可能在选择不同的地理医师键时,考虑到它与标准匹配,它可能会从前 2 个中选择?这就是我使用按产品和月份分组的最大函数的Thanix方法的原因,以便在一个月内将聚合函数应用于每个产品
于 2013-08-07T04:51:52.190 回答
0
尝试这个:
SELECT count(DISTINCT physician_key)
FROM my_table
WHERE month = desired_month
AND max(quality) = 2
GROUP BY month
于 2013-08-06T16:15:19.567 回答