如何根据另一列的值过滤一列?

时间:2017-10-15 16:32:57

标签: mysql sql

我为长篇文章道歉。它是一些案例陈述,使它真的很长。

我有两个问题。第一个是 - 如何从表中获取所有值,还可以在自联接中对其自身进行过滤?具体来说,我正在计算销售人员每天拨打的电话数量,而且我的输出在12点和凌晨1点一直停止。

第二个问题是 - 如何根据另一列的值过滤一列?

我的一些销售人员都在纽约,所以我想说什么时候创造出像约翰尼史密斯那样的人。然后从TIME中减去3。不知道怎么做!

这是我的榜样 -

这就是我的查询......

SELECT  CASE
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 1 THEN '1am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 2 THEN '2am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 3 THEN '3am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 4 THEN '4am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 5 THEN '5am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 6 THEN '6am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 7 THEN '7am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 8 THEN '8am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 9 THEN '9am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 10 THEN '10am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 11 THEN '11am'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 12 THEN '12pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 13 THEN '1pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 14 THEN '2pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 15 THEN '3pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 16 THEN '4pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 17 THEN '5pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 18 THEN '6pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 19 THEN '7pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 20 THEN '8pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 21 THEN '9pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 22 THEN '10pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 23 THEN '11pm'
           WHEN (EXTRACT(HOUR
                         FROM task1.time)) = 0 THEN '12am'
       END AS hour,
       COUNT(task2.Activity_ID__Case_Safe_) AS calls
FROM sdr_data.by_task AS task1
LEFT JOIN sdr_data.by_task AS task2
  ON task1.Activity_ID__Case_Safe_ = task2.Activity_ID__Case_Safe_
  AND task2.task_type LIKE 'Call'
  AND task2.status LIKE 'Completed'
  AND task2.created_date > DATE_SUB(CURRENT_DATE(), INTERVAL 1
                                    WEEK)
LEFT JOIN
  (SELECT account_id__CaseSafe_
   FROM sdr_data.by_task
   WHERE account_id__CaseSafe_ IN
       (SELECT account_id__CaseSafe_
        FROM {{@dummy_accounts}})) AS subquery
  ON task2.account_id__CaseSafe_ = subquery.account_id__CaseSafe_
RIGHT JOIN
  (SELECT created_by,
          Activity_ID__Case_Safe_
   FROM sdr_data.by_task
   WHERE created_by IN
       (SELECT full_name
        FROM {{@active_salespeople}}) ) AS subquery2
  ON task1.Activity_ID__Case_Safe_ = subquery2.Activity_ID__Case_Safe_
WHERE task1.created_by IS NOT NULL
  AND subquery.account_id__CaseSafe_ IS NULL
GROUP BY 1
ORDER BY CASE
             WHEN HOUR LIKE '12am' THEN 1
             WHEN HOUR LIKE '1am' THEN 2
             WHEN HOUR LIKE '2am' THEN 3
             WHEN HOUR LIKE '3am' THEN 4
             WHEN HOUR LIKE '4am' THEN 5
             WHEN HOUR LIKE '5am' THEN 6
             WHEN HOUR LIKE '6am' THEN 7
             WHEN HOUR LIKE '7am' THEN 8
             WHEN HOUR LIKE '8am' THEN 9
             WHEN HOUR LIKE '9am' THEN 10
             WHEN HOUR LIKE '10am' THEN 11
             WHEN HOUR LIKE '11am' THEN 12
             WHEN HOUR LIKE '12pm' THEN 13
             WHEN HOUR LIKE '1pm' THEN 14
             WHEN HOUR LIKE '2pm' THEN 15
             WHEN HOUR LIKE '3pm' THEN 16
             WHEN HOUR LIKE '4pm' THEN 17
             WHEN HOUR LIKE '5pm' THEN 18
             WHEN HOUR LIKE '6pm' THEN 19
             WHEN HOUR LIKE '7pm' THEN 20
             WHEN HOUR LIKE '8pm' THEN 21
             WHEN HOUR LIKE '9pm' THEN 22
             WHEN HOUR LIKE '10pm' THEN 23
             WHEN HOUR LIKE '11pm' THEN 24
         END

对于上下文,第一个连接是过滤掉任务,所以我只是完成调用。第二个加入是摆脱坏/假帐户,第三个加入是确保它只从"活跃销售人员"中提取姓名。列表。

以下是我的源数据的示例 -

Created By  Task Type   Time    Status  Activity ID (Case Safe)
Rudolph Ronald  Call    2015-12-21 12:56:00 Completed   61AA
Joey Jalla  Call    2015-12-21 12:56:00 Completed   61BB
Mike McCrystal  Call    2015-12-21 12:56:00 Completed   61CC
Sally Susie Email   2015-12-21 12:56:00 Open    61DD
Sally Susie Email   2015-12-21 12:56:00 Open    61EE
Sally Susie Meeting 2015-12-21 12:56:00 Completed   61FF

我的输出看起来像这样 -

hour    calls
2am 5
3am 14
4am 8
5am 20
6am 52
7am 55
8am 145
9am 90
10am    106
11am    93
12pm    96
1pm 53
2pm 54
3pm 43
4pm 20
5pm 8
6pm 4
7pm 17
8pm 9
9pm 12
11pm    6

但我希望它看起来像 -

hour    calls
12am    2
1am 4
2am 5
3am 14
4am 8
5am 20
6am 52
7am 55
8am 145
9am 90
10am    106
11am    93
12pm    96
1pm 53
2pm 54
3pm 43
4pm 20
5pm 8
6pm 4
7pm 17
8pm 9
9pm 12
11pm    6

1 个答案:

答案 0 :(得分:0)

SELECT DATE_FORMAT(task1.time, "%l%p") hour,
       COUNT(task2.Activity_ID__Case_Safe_) AS calls
YOUR REST OF THE QUERY
GROUP BY 1
ORDER BY hour

1)您可以用

替换case语句
DATE_FORMAT(task1.time, "%l%p") ---This gives hour in AM/PM

并且按别名排序,而不是按

顺序编写逻辑

2)在您的查询中,外部查询中有一个条件(subquery.account_id__CaseSafe_ IS NULL)

但是样本数据中的时间12 AM时,大小写安全不为空。所以它被过滤掉了。

3)如果您想要跨时区更改时间,请使用

if name = "Johnny Smith", 
     DATE_FORMAT(date_add(task1.time,  INTERVAL 3 HOUR) + 3, "%l%p") , 
     DATE_FORMAT(task1.time + 3, "%l%p"))