1

我有两张桌子:

  1. deskcases| CaseID | Resolved At(Date) | Labels |

  2. ringcentral| Date | Type | Action Result |

我的目标是在前 7 天获得以下输出:

| Date | Count of TotalResolvedCases | Count of Total Calls |

我需要在两个单独的表中计算案例和呼叫,然后按日期加入和分组计数。我还在处理两个表中不同的日期格式,因此古怪的 group by 和 join 函数。

我还需要让加入包括前 7 天的所有日期(deskcases有所有日期,所以我认为 INNER JOIN 应该在这里工作吗?)

要关闭,我尝试在下面运行以下查询,但在一两分钟后它只是搅动而没有结果。关于如何使它工作的任何想法?预先感谢您的帮助!:-)

(运行 MySQL 5.0.96)

SELECT
DATE_FORMAT(
    deskcases.`Resolved At`,
    '%Y-%m-%d'
)AS Date1,
(
    SELECT
        COUNT(deskcases.`Case #`)
    FROM
        deskcases
    WHERE
        deskcases.Labels NOT LIKE '%SPAM%'
    AND deskcases.Labels NOT LIKE '%Online Orders%'
    AND deskcases.Labels NOT LIKE '%Internal SPAM%'
    AND deskcases.`Resolved At` > NOW()- INTERVAL 8 DAY
)AS TotalResolvedCases,
(
    SELECT
        COUNT(ringcentral.Counter)
    FROM
        ringcentral
    WHERE
        `ringcentral`.`Type` = 'Voice'
    AND `Action Result` = 'Accepted'
    AND ringcentral.`Date` > NOW()- INTERVAL 8 DAY
)AS TotalCalls
FROM
 deskcases
INNER JOIN ringcentral ON DATE_FORMAT(
 deskcases.`Resolved At`,
 '%Y-%m-%d'
)= ringcentral.Date
GROUP BY
 Date1
ORDER BY
 Date1 ASC

编辑

我可以稍微编辑一下以给我一些结果,但似乎我正在计算所有案例和使用我的子选择查询的调用,并且总数没有按日期分组。这是代码:

SELECT
DATE_FORMAT(
    deskcases.`Resolved At`,
    '%Y-%m-%d'
)AS Date1,
(
    SELECT
        COUNT(deskcases.`Case #`)
    FROM
        deskcases
    WHERE
        deskcases.Labels NOT LIKE '%SPAM%'
    AND deskcases.Labels NOT LIKE '%Online Orders%'
    AND deskcases.`Case Status` LIKE '%Resolved%'
)AS TotalResolvedCases,
(
    SELECT
        COUNT(ringcentral.Counter)
    FROM
        ringcentral
    WHERE
        `ringcentral`.`Type` = 'Voice'
    AND `Action Result` = 'Accepted'
)AS TotalCalls
FROM
  deskcases
LEFT OUTER JOIN ringcentral ON DATE_FORMAT(
  deskcases.`Resolved At`,
  '%Y-%m-%d'
)= ringcentral.Date
WHERE deskcases.`Resolved At` > NOW()- INTERVAL 8 DAY
GROUP BY
  Date1
ORDER BY
  Date1 ASC
4

1 回答 1

0

在这种情况下,您绝对不应该按时加入。

例如,您可能有以下数据:

deskcases                           ringcentral
---------                           -----------
date      | whatever_d              date      | whatever_r
----------------------              ----------------------
today       x                       yesterday   u
yesterday   y
yesterday   z

现在,当你加入他们时,你会得到:

joinedTable
-----------
date      | whatever_d  |  whatever_r
-------------------------------------
today       x 
yesterday   y              u
yesterday   z              u

糟糕,您从铃声表中获得了 2 次条目,但您只想要 1 次。

那么,如何获取数据?要么编写 2 个语句,要么将它们与UNION

SELECT
DATE(`Resolved At`) AS Date1,
"Deskcases" AS fromTable,
COUNT(*) AS TotalNumber
FROM
        deskcases
    WHERE
        deskcases.Labels NOT LIKE '%SPAM%'
    AND deskcases.Labels NOT LIKE '%Online Orders%'
    AND deskcases.Labels NOT LIKE '%Internal SPAM%'
    AND deskcases.`Resolved At` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2
UNION ALL
SELECT
`Date` AS Date1,
"ringcentral" AS fromTable,
COUNT(*) AS TotalNumber
FROM
        ringcentral
    WHERE
        `ringcentral`.`Type` = 'Voice'
    AND `Action Result` = 'Accepted'
    AND ringcentral.`Date` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2

另请注意,我更改NOW()CURDATE(). 在deskcases 表中,您的日期列似乎是一个时间戳。当您从“2013-03-20 10:06:00”中减去 8 天时,您将得到“2013-03-12 10:06:00”,因此您将丢失“2013-03-12”之间的所有行00:00:00' 和 '2013-03-12 10:05:59'。我也只是使用了DATE()函数而不是DATE_FORMAT(). DATE_FORMAT()在很多方面都是一个非常好的和有用的功能,但在这种情况下,你必须在使用时输入更少的字符DATE():)

更新:

SELECT 
Date1, 
SUM(CASE WHEN fromTable = 'Deskcases' THEN TotalNumber ELSE 0 END) AS TotalDeskcases,
SUM(CASE WHEN fromTable = 'ringcentral' THEN TotalNumber ELSE 0 END) AS TotalRingcentral 
FROM (
SELECT
DATE(`Resolved At`) AS Date1,
'Deskcases' AS fromTable,
COUNT(*) AS TotalNumber
FROM
        deskcases
    WHERE
        deskcases.Labels NOT LIKE '%SPAM%'
    AND deskcases.Labels NOT LIKE '%Online Orders%'
    AND deskcases.Labels NOT LIKE '%Internal SPAM%'
    AND deskcases.`Resolved At` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2
UNION ALL
SELECT
`Date` AS Date1,
'ringcentral' AS fromTable,
COUNT(*) AS TotalNumber
FROM
        ringcentral
    WHERE
        `ringcentral`.`Type` = 'Voice'
    AND `Action Result` = 'Accepted'
    AND ringcentral.`Date` > CURDATE()- INTERVAL 8 DAY
GROUP BY 1, 2
) sq
GROUP BY Date1
于 2013-03-20T09:09:40.893 回答